错误: SMTPConnection._formatError的连接超时,不知道出了什么问题,不能发邮件,请帮帮我。我试图使用nodemailer发送邮件,但是我的控制台中一直有这个错误。
Error: Connection timeout
at SMTPConnection._formatError (/home/codabae/Desktop/mailmonster/Backend/v1/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
code: 'ETIMEDOUT',
command: 'CONN'
}这是我的api --我不知道哪里做错了什么,我从mongodb那里得到了细节,并在nodmailer字段中填充了它,我真的不知道做错了什么。
router.post('/', auth, (req, res) => {
const { to, cc, bcc, subject, message, attachment, smtpDetails } = req.body;
if (!to || !subject || !message || !smtpDetails) return res.status(400).send('input cannot be empty')
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '...@gmail.com',
pass: '...'
}
});
let mailOptions = {
from: '...@gmail.com',
to: to,
cc: cc,
bcc: bcc,
subject: subject,
text: `${message}`
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.send('mail not sent')
} else {
console.log('Email sent: ' + info.response);
res.send('mail sent')
}
});
module.exports = router;发布于 2020-10-05 17:00:00
这里要关注的错误消息的一部分是SMTPConnection._formatError。因为传输配置变量不正确,所以收到此错误。您需要以下变量,并为每个变量提供正确的值。
还可以使用其他变量,但根据代码中的字段,下面的配置应该可以正常工作。如果您需要更多的信息,您可以始终引用Nodemailer文档。
//transport configuration for user a site server to send an email.
let transporter = nodemailer.createTransport({
// This is the SMTP mail server to use for notifications.
// GCDS uses this mail server as a relay host.
host: "smtp.gmail.com",
// SMTP is unlike most network protocols, which only have a single port number.
// SMTP has at least 3. They are port numbers 25, 587, and 465.
// Port 25 is still widely used as a **relay** port from one server to another.
// Port for SSL: 465
// Port for TLS/STARTTLS: 587
port: 465,
// if true the connection will use TLS when connecting to server. If false (the
// default) then TLS is used if server supports the STARTTLS extension. In most
// cases set this value to true if you are connecting to port 465. For port 587 or
// 25 keep it false
secure: true, // use TLS
auth: {
// Your full email address
user: process.env.SMTP_TO_EMAIL,
// Your Gmail password or App Password
pass: process.env.SMTP_TO_PASSWORD
}
});您提到使用来自MongoDB的某种信息来填充这些信息。从配置中获取变量和值的位置我看不出来,但如果要从数据库中提取值,则可能需要使用不同的源或更新查询。
注意:另一个需要注意的常见问题涉及使用Gmail密码,如果您的帐户启用了2FA,则不能使用。在这种情况下,您必须通过跟踪谷歌的这些准则生成一个唯一的应用程序密码。
https://stackoverflow.com/questions/60020804
复制相似问题