我在 Node js 中使用 imap-simple。
我想检索gmail的发送邮箱。我的代码如下:
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
});
});
}
如果我调用 getSent 方法如下
this.getSent(['ALL'], response => {
});
我收到以下错误
'错误:未知邮箱:发送(失败)'
我尝试
connection.openBox('[Gmail]/Sent Mail')
,但我得到了类似的错误'错误:未知邮箱:[Gmail]/已发送邮件(失败)'
我使用 gmail 。
最佳答案
我使用 getBoxes 方法获取了所有文件夹名称。然后我看到文件夹名称是阿塞拜疆语言。
connection.getBoxes().then(response => {
var r = response;
});
问题出在谷歌设置中。我在阿塞拜疆语言中使用了谷歌。如果使用 google 不同的语言,文件夹名称会自动翻译成该语言。我把谷歌语言改成英文,问题解决了。这对我有用。谢谢大家。
我使用
[Gmail]/Sent Mail
。我的代码的正确版本如下:
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
});
});
}
关于javascript - 如何在 imap-simple 中检索 "sent"邮箱,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59930385/