我有一个与 Nodemailer v. 6.9.3 相关的错误。当我启动 nodeJS localhost 时,会出现以下内容:
Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -111,
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN'
}
代码:
const nodeMailer = require('nodemailer')
const html = `
<h1>Hello World</h1>
<p>Wordked!</p>
`;
async function main() {
const transporter = nodeMailer.createTransport({
host: 'localhost',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'Good55555!'
}
})
const info = await transporter.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'Testing, testing, 123',
html: html,
})
console.log("Message sent: " + info.messageId)
}
main()
.catch(e => console.log(e))
我尝试过更改版本、重写代码和更改 gmail 帐户,但没有任何帮助
Error: connect ECONNREFUSED 127.0.0.1:465
您的代码告诉库尝试连接到与您的应用程序在同一台计算机上运行的 SMTP 服务器,端口为 465。
它尝试执行此操作,但您的计算机拒绝连接。
这通常意味着您没有在该端口上运行 SMTP 服务器。
您需要向实际的 SMTP 服务器提供凭据。您提到的 GMail 凭据表明您正在尝试连接到 GMail 的 SMTP 服务器。
GMail 不在您的计算机上运行其 SMTP 服务器。您需要输入他们的服务器地址。
错误在于您创建传输器的方式
const transporter = nodeMailer.createTransport({
host: 'localhost', <-- you can't use localhost here
port: 465,
secure: true,
auth: { // <-- This credential must be authenticated from the service provider
user: '[email protected]',
pass: 'Good55555!'
}
})
尝试查看一些第三方服务提供商以轻松集成。
I faced this issue too. Here is how I solved this
const { AUTH_EMAIL, AUTH_PASS, SMTP_SERVER } = process.env
let transporter = nodemailer.createTransport({
name: "www.example.com", // Add your domain
host: SMTP_SERVER, // Make sure to configure this properly in .env file, // example: mail.example.com
port: 465,
secure: true,
auth: {
user: AUTH_EMAIL,
pass: AUTH_PASS,
}
});
// test transporter
transporter.verify((error, success) => {
if (error){
console.log(error);
} else {
console.log("Ready for sending mails");
console.log(success);
}
});