最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to retrieve "sent" mailbox in imap-simple - Stack Overflow

programmeradmin1浏览0评论

I use imap-simple in node js. I want retrieve sent mailbox of gmail. My code is like below:

 getSent(searchCriteria: string[], callBack: any) {
        imaps.connect(this.imapConfig).then(function (connection) {

            return connection.openBox('SENT').then(function () {
                var fetchOptions = {
                    bodies: ['HEADER', 'TEXT', ''],
                    markSeen: false
                };

                return connection.search(searchCriteria, fetchOptions).then(function (results) {

                    let mails = results.map(res => {
                        return {
                            part: res.parts.find(part => part.which === ''),
                            attributes: res.attributes
                        };
                    });

                    mails = [].concat(...mails);

                    mails = mails.map(mail => {
                        return new Promise((resolve, reject) => {

                            var id = mail.attributes.uid;
                            var idHeader = "Imap-Id: " + id + "\r\n";

                            simpleParser(idHeader + mail.part.body, (error, mail) => {
                                if (error)
                                    reject(error);
                                else {
                                    resolve(new Email({
                                        sentDate: mail.date,
                                        from: mail.from.text,
                                        to: mail.to.text,
                                        messageId: mail.messageId,
                                        body: mail.html,
                                        subject: mail.subject
                                    }));
                                }
                            });
                        })
                    });

                    Promise.all(mails).then(response => {
                        callBack({
                            success: true,
                            emails: response,
                            error: undefined
                        });
                    }, error => {
                        callBack({
                            success: false,
                            emails: [],
                            error: error
                        });
                    });

                }, function (error) {
                    callBack({
                        success: false,
                        emails: [],
                        error: error
                    });
                });
            }, function (error) {
                callBack({
                    success: false,
                    emails: [],
                    error: error
                });
            });
        }, function (error) {
            callBack({
                success: false,
                emails: [],
                error: error
            });
        });
    }

if I call getSent method as below

  this.getSent(['ALL'], response => {
    
  });
 

I get the below error 'Error: Unknown Mailbox: SENT (Failure)'

I try connection.openBox('[Gmail]/Sent Mail'), but i get the similar error as

'Error: Unknown Mailbox: [Gmail]/Sent Mail (Failure)'

I use gmail.

I use imap-simple in node js. I want retrieve sent mailbox of gmail. My code is like below:

 getSent(searchCriteria: string[], callBack: any) {
        imaps.connect(this.imapConfig).then(function (connection) {

            return connection.openBox('SENT').then(function () {
                var fetchOptions = {
                    bodies: ['HEADER', 'TEXT', ''],
                    markSeen: false
                };

                return connection.search(searchCriteria, fetchOptions).then(function (results) {

                    let mails = results.map(res => {
                        return {
                            part: res.parts.find(part => part.which === ''),
                            attributes: res.attributes
                        };
                    });

                    mails = [].concat(...mails);

                    mails = mails.map(mail => {
                        return new Promise((resolve, reject) => {

                            var id = mail.attributes.uid;
                            var idHeader = "Imap-Id: " + id + "\r\n";

                            simpleParser(idHeader + mail.part.body, (error, mail) => {
                                if (error)
                                    reject(error);
                                else {
                                    resolve(new Email({
                                        sentDate: mail.date,
                                        from: mail.from.text,
                                        to: mail.to.text,
                                        messageId: mail.messageId,
                                        body: mail.html,
                                        subject: mail.subject
                                    }));
                                }
                            });
                        })
                    });

                    Promise.all(mails).then(response => {
                        callBack({
                            success: true,
                            emails: response,
                            error: undefined
                        });
                    }, error => {
                        callBack({
                            success: false,
                            emails: [],
                            error: error
                        });
                    });

                }, function (error) {
                    callBack({
                        success: false,
                        emails: [],
                        error: error
                    });
                });
            }, function (error) {
                callBack({
                    success: false,
                    emails: [],
                    error: error
                });
            });
        }, function (error) {
            callBack({
                success: false,
                emails: [],
                error: error
            });
        });
    }

if I call getSent method as below

  this.getSent(['ALL'], response => {
    
  });
 

I get the below error 'Error: Unknown Mailbox: SENT (Failure)'

I try connection.openBox('[Gmail]/Sent Mail'), but i get the similar error as

'Error: Unknown Mailbox: [Gmail]/Sent Mail (Failure)'

I use gmail.

Share edited Nov 29, 2021 at 9:25 Ramil Aliyev 007 asked Jan 27, 2020 at 11:37 Ramil Aliyev 007Ramil Aliyev 007 5,5102 gold badges37 silver badges57 bronze badges 8
  • 2 I could be wrong, but I'm vaguely remembering that the name of the sent folder in Gmail is "Sent Email" rather than "Sent Mail"/"Sent", have you tried that? – Solarflare Commented Jan 27, 2020 at 11:43
  • 1 i already try "Sent Mail" and "Sent", but i get the similar error – Ramil Aliyev 007 Commented Jan 27, 2020 at 12:07
  • 1 If you solved your own question - feel free to post the answer yourself! – leonheess Commented Jan 27, 2020 at 17:31
  • 1 dear @arnt My google account was working at Azerbaijan language, therefore "sent box" ' s name translated Azerbaijan language. If google accout working at Azerbaijan language, box name will be [Gmail]/Göndərilənlər. Or change google's language to english, then sentbox' name will be [Gmail]/Sent Mail with google's language. – Ramil Aliyev 007 Commented Jan 28, 2020 at 13:10
  • 1 thanks you @leonheess . I already posted answer, but i can't accept own answer for now , i will accept that tomorow – Ramil Aliyev 007 Commented Jan 28, 2020 at 13:12
 |  Show 3 more ments

2 Answers 2

Reset to default 6

I got all the folder names using the getBoxes method. Then I saw that folder names e in Azerbaijan language.

connection.getBoxes().then(response => {
                var r = response;
            });

The problem was in google settings. I used Google in Azerbaijan language. If use google different language, folder names are automatically translated into that language. I change Google language to English, problem solved. It way work me. Thanks to everyone.

I use [Gmail]/Sent Mail.

Correct version of my code is below:

getSent(searchCriteria: string[], callBack: any) {
        imaps.connect(this.imapConfig).then(function (connection) {

            return connection.openBox('[Gmail]/Sent Mail').then(function () {
                var fetchOptions = {
                    bodies: ['HEADER', 'TEXT', ''],
                    markSeen: false
                };

                return connection.search(searchCriteria, fetchOptions).then(function (results) {

                    let mails = results.map(res => {
                        return {
                            part: res.parts.find(part => part.which === ''),
                            attributes: res.attributes
                        };
                    });

                    mails = [].concat(...mails);

                    mails = mails.map(mail => {
                        return new Promise((resolve, reject) => {

                            var id = mail.attributes.uid;
                            var idHeader = "Imap-Id: " + id + "\r\n";

                            simpleParser(idHeader + mail.part.body, (error, mail) => {
                                if (error)
                                    reject(error);
                                else {
                                    resolve(new Email({
                                        sentDate: mail.date,
                                        from: mail.from.text,
                                        to: mail.to.text,
                                        messageId: mail.messageId,
                                        body: mail.html,
                                        subject: mail.subject
                                    }));
                                }
                            });
                        })
                    });

                    Promise.all(mails).then(response => {
                        callBack({
                            success: true,
                            emails: response,
                            error: undefined
                        });
                    }, error => {
                        callBack({
                            success: false,
                            emails: [],
                            error: error
                        });
                    });

                }, function (error) {
                    callBack({
                        success: false,
                        emails: [],
                        error: error
                    });
                });
            }, function (error) {
                callBack({
                    success: false,
                    emails: [],
                    error: error
                });
            });
        }, function (error) {
            callBack({
                success: false,
                emails: [],
                error: error
            });
        });
    }

I had the same issue and I had to figure it out myself:

Replace connection.openBox('SENT') with connection.openBox('INBOX.Sent') OR connection.openBox('INBOX/Sent').

Depending on your assigned delimiter (. or / or any other) and you can find out your delimiter by adding

connection.getBoxes().then(response => {
  var r = response;
  console.log(r); // if nothing then try console.log(JSON.stringify(r))
});

I hope this will resolve your issue. Have a nice day!

发布评论

评论列表(0)

  1. 暂无评论