通过用户指定的gmail帐户发送消息



我正在制作一个电子邮件客户端。我希望用户输入他们的谷歌凭据,并能够发送消息到其他电子邮件。我会用nodemailer这样做,但谷歌似乎不允许nodemailer只使用用户名和密码发送消息。下面是我尝试做的一个例子:

const nodemailer = require('nodemailer');
async function main() {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'divine@gmail.com',
pass: '******',
},
});
let info = await transporter.sendMail({
from: '"Divine" <divine@gmail.com>',
to: 'info@example.com',
subject: 'Hello',
text: 'Hello world?',
html: '<b>Hello world?</b>',
});
console.log('Message sent: %s', info.messageId);
}
main().catch(console.error);

我收到的错误是:

Node.js v18.12.1
PS C:UsersdivineProgrammingNode.jsEmailClient> node index
Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials l19-20020a170902f69300b00177efb56475sm24894649plg.85 - gsmtp
at SMTPConnection._formatError (C:UsersdivineProgrammingNode.jsEmailClientnode_modulesnodemailerlibsmtp-connectionindex.js:790:19)
at SMTPConnection._actionAUTHComplete (C:UsersdivineProgrammingNode.jsEmailClientnode_modulesnodemailerlibsmtp-connectionindex.js:1542:34)
at SMTPConnection.<anonymous> (C:UsersdivineProgrammingNode.jsEmailClientnode_modulesnodemailerlibsmtp-connectionindex.js:546:26)
at SMTPConnection._processResponse (C:UsersdivineProgrammingNode.jsEmailClientnode_modulesnodemailerlibsmtp-connectionindex.js:953:20)
at SMTPConnection._onData (C:UsersdivineProgrammingNode.jsEmailClientnode_modulesnodemailerlibsmtp-connectionindex.js:755:14)
at SMTPConnection._onSocketData (C:UsersdivineProgrammingNode.jsEmailClientnode_modulesnodemailerlibsmtp-connectionindex.js:193:44)
at TLSSocket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10) {
code: 'EAUTH',
response: '535-5.7.8 Username and Password not accepted. Learn more atn' +
'535 5.7.8  https://support.google.com/mail/?p=BadCredentials l19-20020a170902f69300b00177efb56475sm24894649plg.85 - gsmtp',
responseCode: 535,
command: 'AUTH PLAIN'
}

是否有一种类型的谷歌认证,我可以使用,或者一些方法为用户配置nodemailer从他们的gmail发送电子邮件而不添加应用令牌?

我也试过允许不安全的应用程序访问我的Gmail作为解决方案,但这也不起作用。

首先去你的谷歌帐户,然后去步骤验证,然后点击和你的电子邮件密码第二步,进入安全选项,点击应用程序密码,然后选择你的设备和Windows,点击生成然后哪个代码生成复制和删除你的旧密码,写你的代码

最新更新