我試圖建立nodemailer一個應用程序,我創建。我在嘗試運行代碼時收到錯誤消息。錯誤使用發送電子郵件時nodemailer
這是我的設置:
const nodemailer = require("nodemailer");
const xoauth2 = require("xoauth2");
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: '****@gmail.com',
clientId: '*******************************',
clientSecret: '*****************************',
refreshToken: '*****************************'
})
}
});
var mailOptions = {
from: 'Dave <*******@gmail.com>',
to: '*******@gmail.com',
subject: 'Nodemailer test',
text: 'Hello world'
}
transporter.sendMail(mailOptions, function(err, res) {
if (err) {
console.log(err);
} else {
console.log('Email sent');
}
})
很顯然,我仔細檢查過我的client ID
,secret
和refresh token
。但我得到以下錯誤:
{ 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 y22sm6749013wry.51 - gsmtp at SMTPConnection._formatError (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.j s:557:19) at SMTPConnection._actionAUTHComplete (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/ index.js:1248:34) at SMTPConnection._responseActions.push.str (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-conn ection/index.js:340:26) at SMTPConnection._processResponse (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/ind ex.js:706:20) at SMTPConnection._onData (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:5 09:14) at TLSSocket._socket.on.chunk (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index .js:461:47) at emitOne (events.js:96:13) at TLSSocket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at TLSSocket.Readable.push (_stream_readable.js:134:10) at TLSWrap.onread (net.js:551:20) code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/?p=BadCrede ntials y22sm6749013wry.51 - gsmtp', responseCode: 535, command: 'AUTH PLAIN' }
這是我第一次使用nodemailer試過,我覺得我明顯缺失的一個關鍵部分,但在這裏不知道。
使用真實的電子郵件和密碼新的錯誤:
{ Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvZ
534-5.7.14 vX8SWYwnciMLBvsoC9zLFxi_9pKu2juDkWdPY4cJngQct2L0qjKFr3aF_SlAVCV816xj-8
534-5.7.14 eC36n8fZzITno-GnJdvwRSf6eIXfeU_ohzp07tc4S3LA0x2k9xPRwAjMlsWfNoa1Iz2GwX
534-5.7.14 AyBKjwT8nmD-wpNNK5J_bN9F3OI56XFAfw0NmjxKnUfhHXPoTs0sGCc6eRn_9hgYp2TyFe
534-5.7.14 MAd2gvxDCVp5O9V-yuGa9nrch8ey4> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 f135sm7148225wmd.7 - gsmtp
at SMTPConnection._formatError (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index.js:557:19)
at SMTPConnection._actionAUTHComplete (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index.js:1248:34)
at SMTPConnection._responseActions.push.str (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:340:26)
at SMTPConnection._processResponse (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:706:20)
at SMTPConnection._onData (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:509:14)
at TLSSocket._socket.on.chunk (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:461:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:551:20)
代碼: 'EAUTH', 迴應:「534-5.7.14請通過網絡瀏覽器登錄和\ n534-5.7.14然後再試\ n534-5.7.14瞭解更多\ n534 5.7.14 https://support.google.com/mail/answer/78754 f135sm7148225wmd.7 - gsmtp」, responseCode:534, 命令: 'AUTH PLAIN'}
您能在Gmail使用OAuth來獲得用戶的密碼?如果是這樣,您可以將其插入身份驗證。但我不確定如果用戶沒有直接輸入它,這是不可能的,這不是一種安全的做法。如果你想從用戶的帳戶發送電子郵件到另一個地址,請參閱:https://developers.google.com/gmail/api/v1/reference/users/messages/send – wlh
嗯,我嘗試了上面的方法,然後我仍然遇到了一個錯誤,我將其粘貼到原始文章中 – DaveB1
@ DaveB1您顯示的錯誤表示您正在使用xoauth。你不應該在這裏使用它。你有沒有嘗試輸入實際的密碼? – wlh