2014-11-03 117 views
3

我想送使用Nodemailer和Gmail郵件,但它不工作...Nodemailer和Gmail

編輯#1:

代碼:

var transporter = nodemailer.createTransport(smtpPool({ 
 
    service: 'gmail', 
 
    auth: { 
 
    user: '***@gmail.com', 
 
    pass: '***' 
 
    }, 
 
    maxConnections: 5, 
 
    maxMessages: 10 
 
})); 
 

 
var mailOptions = { 
 
    from: mail, // sender address 
 
    to: '[email protected]', 
 
    subject: subject, 
 
    text: message, 
 
    html: message 
 
}; 
 

 
transporter.sendMail(mailOptions, function(error, info){ 
 
    if(error){ 
 
    console.log(error); 
 
    }else{ 
 
    request.flash('success', 'Votre message à bien été envoyé.'); 
 
    response.redirect('/contacter-printwithlove'); 
 
    } 
 
});

錯誤:

{ [Error: Invalid login] 
    code: 'EAUTH', 

看來,密碼是不正確的,但在Gmail網絡界面上我的密碼的作品完美...

編輯#2:

enter image description here

+0

請參閱我編輯的答案以包含var smtpPool = require('nodemailer-smtp-pool'); – Ben 2014-11-03 14:57:14

+0

已經包含在我的代碼中,但沒有在帖子中... – tonymx227 2014-11-03 15:13:41

回答

2

您需要傳遞服務='gmail'和用戶/密碼:

var transporter = nodemailer.createTransport({ 
    service: 'Gmail', 
    auth: { 
    user: '[email protected]', 
    pass: <your password> 
    } 
}); 

// Or using SMTP Pool if you need to send a large amount of emails 
var smtpPool = require('nodemailer-smtp-pool'); 
var transporter = nodemailer.createTransport(smtpPool({ 
    service: 'gmail', 
    auth: { 
    user: '[email protected]', 
    pass: <your password> 
    }, 
    maxConnections: 5, 
    maxMessages: 10 
})); 

編輯:

您可能需要「允許訪問您的Google帳戶」。使用瀏覽器登錄到你的Gmail,然後去這個鏈接https://accounts.google.com/DisplayUnlockCaptcha解鎖它。您可能需要使用剛剛登錄的瀏覽器,以便Google知道您要授予哪個帳戶的訪問權限。點擊「允許」按鈕後,您應該看到以下消息:「啓用帳戶訪問權限,請嘗試從您的新設備或應用程序再次登錄您的Google帳戶。」

+0

無法與您的代碼一起工作,請參閱上面的... – tonymx227 2014-11-03 14:55:53

+0

奇怪的是,我目前使用上述兩種方法來服務我們的服務器,並且兩者都在爲我們工作。 – Ben 2014-11-03 22:56:40

+0

好吧,我不明白爲什麼密碼不起作用...這是真實的密碼... – tonymx227 2014-11-04 11:29:47