SMTP 服务器 - 发送没有身份验证/密码的电子邮件?

时间:2021-05-25 15:14:43

标签: javascript node.js smtp

我最近收到了向我的代码添加功能的请求。

我的代码的最终结果是同一文件夹中的一堆 .csv 文件。 更新要求我通过电子邮件发送这些文件。 可用和强加的方案是通过 SMTP 来完成。

现在我已经阅读了它,并且还检查了#nodeMailer npm 包,但它们都需要登录名和密码,但我的经理说我不需要登录名和密码,没有它也可以发送电子邮件。

我有 SMTP 的 IP 地址和应该发送信件的电子邮件地址。

问题:

  • 如何在不实际登录邮件帐户的情况下发送电子邮件?
  • 如果 nodemailer 不是一个选项,我如何将文件作为内容附加到电子邮件中?
  • 是否有任何 npm 包,或者您能给我提供解决方案或链接,以便我可以继续阅读以实现这个奇迹?

1 个答案:

答案 0 :(得分:0)

好的在检查了一些事情和建议后,我得到了以下信息。如果将来有人遇到同样的问题,我会将其发布为答案。

  • 除非获得贵公司smtp的授权,否则您不能在未经身份验证的情况下发送消息check here
  • 如果您被授权发送电子邮件,但不知何故收到错误,如果您尝试向自己发送电子邮件,第一个可能的错误是没有连接到 VM 或 apiAddresses 冲突 .我已经通过我公司的 VPN 解决了这个问题。
  • 修复此问题后,您可能会遇到的下一个错误是sendmail 错误:自签名证书。为了解决这个问题,我找到了一个答案 here

所以,在这个清单完成后,发送邮件的最终代码应该看起来像这样:

var transporter = nodemailer.createTransport(smtpTransport({
        host: "", // hostname
        secure: false, // use SSL
        port: , // port for secure SMTP
        tls: {
           rejectUnauthorized: false
        }
    }));

 var mailOptions = {
        from: '', // sender address
        to: '', // list of receivers
        cc: '', // Comma separated list or an array
        subject: 'test upgrde nodemailer subject', // Subject line
        html: '<b>Hello world </b>' // html body
    };

transporter.sendMail(mailOptions, function(error, info){
        if(error){
            console.log("/sendmail error");
            console.log(error);
            res.sendStatus(500);
            return;
        }else{
            console.log("Message sent: " + info.response);
            // if you don't want to use this transport object anymore, uncomment following line
            socketTimeout: 30 * 1000 // 0.5 min: Time of inactivity until the connection is closed
            transporter.close(); // shut down the connection pool, no more messages
            res.sendStatus(200);
        }

        // if you don't want to use this transport object anymore, uncomment following line
        transporter.close(); // shut down the connection pool, no more messages
    });