EC2 实例中的 SMTP 服务器:中继访问被拒绝

问题描述 投票:0回答:1

我正在尝试通过在 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);
});

node.js amazon-web-services amazon-ec2 smtp
1个回答
0
投票

现实情况是您几乎永远无法以这种方式发送电子邮件。如果可以的话,那么地球上的任何人都可以像您从任何计算机上一样从您的 Gmail 地址发送电子邮件。相反,您的“发件人”地址需要来自合格的主机(如SPF 记录中指定)。接下来,接收电子邮件的系统可以使用 DKIM 记录 来验证 Gmail 是否是电子邮件的实际发件人(但事实并非如此)。这两者使用 DMARC 记录联系在一起。

简单的回答是,您不能再模拟“来自”域。我鼓励您查看其他答案,了解一些其他详细信息以及如何使用您现有的电子邮件。

© www.soinside.com 2019 - 2024. All rights reserved.