我正在使用nodemailer和googleapis从我的Next.js API路由发送电子邮件,但收到以下错误:Error:Missing Credentials for“Plain”
我按照以下教程进行了设置:https://blog.devgenius.io/correct-way-in-setting-up-nodemailer-for-email-service-946f6bfd73e8
我还查看了nodemailer文档,以确保一切都是最新的。3LO身份验证设置的节点邮件文档示例:https://nodemailer.com/smtp/oauth2/#example-3
以下是我的代码:
const handleSendVerificationEmail = async (req: NextApiRequest, res: NextApiResponse) => {
const { body } = req
const { email }: { email: string } = body
const user = process.env.GOOGLE_EMAIL_ADDRESS
const clientId = process.env.GOOGLE_CLIENT_ID_MAIL
const clientSecret = process.env.GOOGLE_CLIENT_SECRET_MAIL
const refreshToken = process.env.GOOGLE_REFRESH_TOKEN
const redirectURI = process.env.GOOGLE_REDIRECT_URI
const oAuth2Client = new google.auth.OAuth2(clientId, clientSecret, redirectURI)
oAuth2Client.setCredentials({ refresh_token: refreshToken })
try {
const accessToken = await oAuth2Client.getAccessToken()
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: '0Auth2',
user,
clientId,
clientSecret,
refreshToken,
accessToken
},
tls: {
rejectUnauthorized: false
}
})
const mailOptions = {
from: `YourTable <${user}>`,
to: email,
subject: 'Hello from gmail using API',
text: 'Hello from gmail email using API',
html: '<h1>Hello from gmail email using API</h1>'
}
const result = await transporter.sendMail(mailOptions)
res.status(200).json({ email })
} catch (error) {
console.log(error)
res.status(500).json({ error: 'Unable to send verification email' })
}
}
export default handleSendVerificationEmail
我还向传输器添加了TLS属性,就像我找到的一些教程中所建议的那样,尽管它没有解决问题。
我还尝试跳过主机、端口和安全属性,代之以服务。再说一次,这并没有起到什么作用。
按照注解中的建议,我按照说明进行了操作,并相应地更改了代码:
const user = process.env.GOOGLE_EMAIL
const pass = process.env.GOOGLE_APP_PASSWORD
const transporter = nodemailer.createTransport({
port: 465,
host: "smtp.gmail.com",
auth: {
user: user,
pass: pass
},
secure: true
})
const mailOptions = {
from: `YourTable <${user}>`,
to: email,
subject: 'Hello from gmail using API',
text: 'Hello from gmail email using API',
html: '<h1>Hello from gmail email using API</h1>'
}
const result = await transporter.sendMail(mailOptions)
但同样的错误再次出现:
Error: Missing credentials for "PLAIN"
at SMTPConnection._formatError (H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
at SMTPConnection.login (H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-connection\index.js:444:38)
at H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-transport\index.js:272:32
at SMTPConnection.<anonymous> (H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-connection\index.js:213:17)
at Object.onceWrapper (node:events:641:28)
at SMTPConnection.emit (node:events:527:28)
at SMTPConnection.emit (node:domain:475:12)
at SMTPConnection._actionEHLO (H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-connection\index.js:1325:14)
at SMTPConnection._processResponse (H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-connection\index.js:953:20)
at SMTPConnection._onData (H:\Dokumente\Programming_Projects\yourtable\node_modules\nodemailer\lib\smtp-connection\index.js:755:14) {
code: 'EAUTH',
command: 'API'
}
2条答案
按热度按时间lnvxswe21#
我也遇到了同样的问题,以下是我如何解决它的。因为我使用的是Gmail,所以我必须启用双因素身份验证,然后将Nodemailer添加到应用程序密码App Password Settings,然后使用生成的密码作为身份验证的密码
rvpgvaaj2#
我把传送器改成这样,终于成功了:
然而,我仍然没有找到通过Google API使其与0Auth 2.0一起工作的解决方案。