【问题标题】:Can't send mail node mailer无法发送邮件节点邮件程序
【发布时间】:2014-01-09 06:14:46
【问题描述】:

我收到收件人错误:无法发送邮件 - 使用 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
});

【问题讨论】:

  • 您的电子邮件地址是字符串吗?现在 abc@gmail.com 缺少引号
  • 您传递给 sendMail 的选项也不是 mailOptions。是不是打错字了?
  • @C Blanchard,我现在正在编辑我的整个问题。
  • 也许你是对的,我在一个函数中处理这个并且可能没有将值返回给被调用者。让我用这个检查一下。会回来找你的:)
  • 我没有返回值。它现在正在工作。谢谢你:)

标签: node.js email nodemailer


【解决方案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);
        });
    });
});

【讨论】:

    【解决方案2】:

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

    转到该链接并且活动不如尝试发送邮件安全 enter link description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多