我使用以下代码创建一个用于发送电子邮件的 SMTPtransporter。该代码在我的计算机上运行得非常好。使用 Yarn 版本“1.22.17”。
import * as nodemailer from 'nodemailer';
import * as SMTPTransport from "nodemailer/lib/smtp-transport";
const poolOptions = {
pool: true,
maxConnections: 1,
maxMessages: 5
}
const smtpOptions = {
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: SMTP_USER,
pass: SMTP_PASSWORD
},
tls:{
ciphers: 'SSLv3',
rejectUnauthorized: false
}
}
const nodemailerOptions: SMTPTransport.Options = {
...poolOptions,
...smtpOptions
}
const transport = nodemailer.createTransport(nodemailerOptions);
发送电子邮件功能:
export function sendMail(
to: string, subject: string, text: string, html: string) {
const mailOptions = {
from: 'Bobo <[email protected]>', to, subject, text, html
};
return new Promise((resolve, reject) => {
transport.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(
`Failed to send email to ${to} with body [${text}]`, error);
reject(new Error('Email sending failed'));
} else {
console.log(
`Email sent to ${to} with subject [${subject}]`, info.response);
resolve();
}
});
});
}
同时在服务器中,每次我尝试发送电子邮件时都会收到以下错误:
{ Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
errno: 'ECONNRESET',
code: 'ECONNECTION',
syscall: 'read',
command: 'CONN' }
它与部署在 ubuntu 服务器中的相同应用程序具有相同的 Yarn 版本。 如果有人可以帮助我,我将非常感激。
注意:: 对于服务器上的部署,我使用 Nginx 将端口 80 上的所有请求转发到 3000(应用程序运行)。并且 SMTP 端口已开放(25,587)
host: "smtp.gmail.com",
port: 465,
secure: true,
a porta que o gmail usa é a 465。
o 安全 é 真实。Desculpe por não saber mais informações a respeito, mas pode ser um bom ponto de partida para começar a Solutionr o Problema.