3

我收到收件人错误:无法发送邮件 - 使用 npm 节点邮件程序发送电子邮件时没有收件人定义错误。我正在咨询文档中的示例。考虑使用我的真实电子邮件 abc@gmail.com。

var emailAddress = "abc@gmail.com";
mailOptions = {
    from: "Admin <abc@gmail.com>", // sender address
    to: emailAddress, // list of receivers
    subject: "Hello", // Subject line
    text: "hello", // plaintext body
    html: "<b>Hello</b>" // html body
}

smtpTransport = nodeMailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "abc@gmail.com",
        pass: "123"
    }
});

smtpTransport.sendMail(mailJson, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

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

2 回答 2

1

以下建议会很有用(对于其他用户):

  1. mailJson是您的配​​置称为mailOptions.
  2. 将在一个承诺中返回您的结果,这在您以异步代码发送电子邮件时非常有用。

因此,您的代码将如下所示:

mailOptions = {
    from: "Admin <abc@gmail.com>", // sender address
    to: emailAddress, // list of receivers
    subject: "Hello", // Subject line
    text: "hello", // plaintext body
    html: "<b>Hello</b>" // html body
}

smtpTransport = nodeMailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "abc@gmail.com",
        pass: "123"
    }
});

return new Promise((resolve, reject) => {
        transporter.sendMail(mailOptions, (error, info) => {
            error ? reject(error) : resolve(info);
        });
    });
});
于 2017-06-24T16:05:39.900 回答
0

如果没有适合您的解决方案

转到该链接并且活动不如尝试发送邮件 在此处输入链接描述安全

于 2021-05-01T12:06:57.120 回答