最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unhandled promise rejection while sending email using Node.js - Stack Overflow

programmeradmin1浏览0评论

I'm using this script for sending email:

var mailer = require('nodemailer');

var smtpTransport = mailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'user',
        pass: 'pass'
    }
});

 smtpTransport.sendMail({       
        sender: 'user',
        to: 'user',
        subject: 'Attachment!',
        body: 'mail content...',
        attachments: [{   
        filename: 'text3.txt',
        path: 'pat/to/file.txt' 
    }]
    }), function(err, success) {
        if (err) {
        }

    }

But I'm getting this error:

Unhandled promise rejection (rejection id: 9): Error: connect ETIMEDOUT

What's wrong?

I'm using this script for sending email:

var mailer = require('nodemailer');

var smtpTransport = mailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'user',
        pass: 'pass'
    }
});

 smtpTransport.sendMail({       
        sender: 'user',
        to: 'user',
        subject: 'Attachment!',
        body: 'mail content...',
        attachments: [{   
        filename: 'text3.txt',
        path: 'pat/to/file.txt' 
    }]
    }), function(err, success) {
        if (err) {
        }

    }

But I'm getting this error:

Unhandled promise rejection (rejection id: 9): Error: connect ETIMEDOUT

What's wrong?

Share Improve this question edited Sep 23, 2018 at 18:06 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jun 28, 2017 at 14:54 OiRcOiRc 1,6224 gold badges26 silver badges62 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Unhandled Promise rejection means that there is some Promise function that doesn't have a .catch. Nodemailer's .sendMail function is a Promise function so you can either have a callback or use it like a Promise.

In your code your fuction(err, sucess) is outside of your .sendMail function. Here is how you could correct it:

 smtpTransport.sendMail({       
    sender: 'user',
    to: 'user',
    subject: 'Attachment!',
    body: 'mail content...',
    attachments: [{   
    filename: 'text3.txt',
    path: 'pat/to/file.txt' 
}]
},(err, success) => {
    if (err) {
    }
});

Well, firstly what is wrong is you have a ETIMEDOUT error ing from your email call. Are you sure you have all the parameters correct?

Secondly use Promises, rather than callback style, and catch the rejection. Like this,

smtpTransport
  .sendMail(...)
  .then(success => console.log('success: ', success))
  .catch(error => console.log('error: ', error))

Maybe is a rejection from GMail, Try Login in to: https//www.google./setti ngs/security/lesssecureapps and TURN ON "Access for less secure apps". Or use OAuth.

发布评论

评论列表(0)

  1. 暂无评论