2016-08-12 111 views
2

我試圖構建一個應用程序,當某些事件被觸發時發送電子郵件。沒有收到來自emailjs和smtp服務器的電子郵件 - Nodejs

我沒有收到任何錯誤,但沒有收到郵件到我的收件箱。

這是代碼:

SMTP:

var SMTPServer = require('smtp-server').SMTPServer; 
var server = new SMTPServer({ 
    secure: false, authOptional: true 
}); 
server.listen(465); 

emailjs:

var email = require('emailjs'); 

var emailServer = email.server.connect({ 
    host: 'localhost', 
    port: 465, 
    ssl: false 
}); 

emailServer.send({ 
    text: 'Hey howdy', 
    from: 'NodeJS', 
    to: 'Wilson <[email protected]>', 
    cc: '', 
    subject: 'Greetings' 
}, function (err, message) { 
    console.log(err || message); 
}); 

我我的控制檯上看到輸出是:

{ attachments: [], 
    alternative: null, 
    header: 
    { 'message-id': '<[email protected]>', 
    date: 'Fri, 12 Aug 2016 12:50:27 +0300', 
    from: '=?UTF-8?Q?NodeJS?= <>', 
    to: '=?UTF-8?Q?Wilson?= <[email protected]>', 
    cc: '', 
    subject: '=?UTF-8?Q?Greetings?=' }, 
    content: 'text/plain; charset=utf-8', 
    text: 'Hey howdy' } 

任何想法有關少了什麼東西?

感謝

回答

0

我使用nodemailer-smtp-transport,併爲我工作完美,所遵循的是我通過我發送郵件的功能內線配置。希望這可能有所幫助。

var nodemailer = require('nodemailer'), 
smtpTransport = require("nodemailer-smtp-transport"); 

//configuration 
var transporter = nodemailer.createTransport(smtpTransport ({ 
     auth: { 
      user: user, //email of sender 
      pass: pass //password of sender 
     }, 
     host: host, //my email host 
     secureConnection: true, 
     port: 587, 
     tls: { 
      rejectUnauthorized: false 
     }, 
    }));  

    var mailOptions = { 
     from: user, 
     to: to, 
     subject: subject, 
     text: text 
    } 

    transporter.sendMail(mailOptions, function(error, info){ 
     if(error){ 
      console.log(error); 
     }else{ 
      console.log("success"); 
     } 
    }); 
+1

感謝您的回答,我試圖按照你的回答,但仍然沒有奏效。我設置的smtp來自smtp-server模塊,它不需要憑證。如何在沒有憑據的情況下連接它? – bratibrat

+0

我不確定你是否可以發送郵件沒有憑據,我知道在PHP中,你可以發送電子郵件沒有憑據,但不確定與node.js,反正,從來沒有。 –

0

在emailjs文件主機不能本地主機它應該是這樣的:smtp.your-email.com

var server = email.server.connect({ 
    user: "username", 
    password:"password", 
    host: "smtp.your-email.com", 
    ssl:  true 
}); 
+3

我如何知道smtp主機,據我所知,smtp服務器模塊在本地主機上創建它,是真的嗎? – bratibrat

0

我使用一個不是非常重要的Gmail帳戶,而不是我的測試模塊smtp服務器。

首先:需要激活您自擔風險作爲Gmail的文件中所示的使用不夠安全的應用:

https://myaccount.google.com/u/2/lesssecureapps?pageId=none

後:

var email = require("emailjs"); 
var server = email.server.connect({ 
    user: "[email protected]", 
    password:"YOUR_PASSWORD", 
    host: "smtp.gmail.com", 
    ssl:  true 
}); 

server.send({ 
    text: "Hello world", 
    from: "YOUR_NAME <[email protected]>", 
    to:  "FRIEND_NAME <[email protected]>", 
    subject: "Hello" 
}, function(err, message) { console.log(err || message); }); 
相關問題