Ram*_*yev 4 javascript imap node.js
我在 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
});
});
}
Run Code Online (Sandbox Code Playgroud)
如果我调用 getSent 方法如下
this.getSent(['ALL'], response => {
});
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 “错误:未知邮箱:已发送(失败)”
我尝试了connection.openBox('[Gmail]/Sent Mail'),但我得到了类似的错误
“错误:未知邮箱:[Gmail]/已发送邮件(失败)”
我用的是谷歌邮箱。
我使用 getBoxes 方法获取了所有文件夹名称。然后我看到文件夹名称是阿塞拜疆语。
connection.getBoxes().then(response => {
var r = response;
});
Run Code Online (Sandbox Code Playgroud)
问题出在谷歌设置上。我用阿塞拜疆语谷歌。如果使用谷歌不同的语言,文件夹名称会自动翻译成该语言。我将谷歌语言更改为英语,问题解决了。这对我很有用。谢谢大家。
我用[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
});
});
}
Run Code Online (Sandbox Code Playgroud)