我正在尝试通过在 EC2 Ubuntu 实例中安装 postfix 来在 EC2 中设置 SMTP 服务器。但是当我尝试运行 NodeJS 代码来发送邮件时,出现以下错误。
Error: Can't send mail - all recipients were rejected: 454 4.7.1 <[email protected]>: Relay access denied
at SMTPConnection._formatError (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
at SMTPConnection._actionRCPT (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:1654:28)
at SMTPConnection.<anonymous> (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:1607:30)
at SMTPConnection._processResponse (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:969:20)
at SMTPConnection._onData (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:755:14)
at SMTPConnection._onSocketData (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:193:44)
at TLSSocket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10) {
code: 'EENVELOPE',
response: '454 4.7.1 <[email protected]>: Relay access denied',
responseCode: 454,
command: 'RCPT TO',
rejected: [ '<[email protected]>' ],
rejectedErrors: [
Error: Recipient command failed: 454 4.7.1 <[email protected]>: Relay access denied
at SMTPConnection._formatError (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
at SMTPConnection._actionRCPT (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:1640:24)
at SMTPConnection.<anonymous> (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:1607:30)
at SMTPConnection._processResponse (C:\path\burra\node_modules\nodemailer\lib\smtp-connection\index.js:969:20)
at SMTPConnection._onData (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:755:14)
at SMTPConnection._onSocketData (C:\Users\path\node_modules\nodemailer\lib\smtp-connection\index.js:193:44)
at TLSSocket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10) {
code: 'EENVELOPE',
response: '454 4.7.1 <[email protected]>: Relay access denied',
responseCode: 454,
command: 'RCPT TO',
recipient: '<[email protected]>'
}
]
}
我不确定出了什么问题以及应该如何修复。任何帮助,将不胜感激。预先感谢!
我确实设置了 SMTP 服务器并进行了必要的配置,例如启用端口、EC2 中的入站设置。但似乎都不起作用。
这是我一直在尝试运行的代码。
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'EC2_IP', // Replace with your EC2 instance public IP or domain
port: 25,
secure: false,
tls: {
rejectUnauthorized: false
},
auth: {
user: 'EC2_UBUNTU_LOGIN_USERNAME', // Use the system user associated with Postfix
pass: 'EC2_UBUNTU_LOGIN_PASSWORD' // Use the system user's password
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Test Email',
text: 'This is a test email from your Node.js script.'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.error('Error:', error);
}
console.log('Email sent:', info.response);
});