2013-12-20 114 views
3

我收到收件人錯誤:無法發送郵件 - 使用npm節點郵件發送電子郵件時沒有收件人定義的錯誤。我正在參考文檔中的示例。考慮[email protected]與我真正的電子郵件。無法發送郵件節點郵件

var emailAddress = "[email protected]"; 
mailOptions = { 
    from: "Admin <[email protected]>", // 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: "[email protected]", 
     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 
}); 
+0

的電子郵件地址是一個字符串?現在[email protected]缺少引號 –

+0

是的,它是一個字符串 –

+0

此外,您傳遞給sendMail的選項不是mailOptions。這是一個錯字嗎? –

回答

0

以下的建議是有用的(對他人的用戶):

  1. mailJson是你叫mailOptions配置。
  2. 會返回一個承諾您的結果,這是非常有用的,當你在一個異步代碼發送電子郵件。

所以,你的代碼將是如下:

mailOptions = { 
    from: "Admin <[email protected]>", // 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: "[email protected]", 
     pass: "123" 
    } 
}); 

return new Promise((resolve, reject) => { 
     transporter.sendMail(mailOptions, (error, info) => { 
      error ? reject(error) : resolve(info); 
     }); 
    }); 
});