Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

node.js - Why 'nodemailer' does not send any message?

Here is the code:

var nodemailder = require('nodemailer');
var from = nodemailder.createTransport({
    service: "gmail",
    auth:{
        user: 'user***@gmail.com',
        pass: 'pass***'
    }
});
var options = {
     from: 'user***@gmail.com',
     to: 'user***@gmail.com',
     subject: 'sometext',
     text: 'messageContext',
};
from.sendMail(options, function (error, info) {
     if (error) {
            console.log(error);
     } else {
            console.log('Email sent: ' + info.response);
     }
});

If I use Windows - it works, when I tried to start on Ubuntu (16) - I dont get any error message etc., but the mail was not received.

question from:https://stackoverflow.com/questions/65888093/why-nodemailer-does-not-send-any-message

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If that is the case, I believe SMTP service is unable to make connection to connection on port 25.

Running server will look like:

$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 xxxx.de ESMTP Postfix
QUIT
221 2.0.0 Bye
Connection closed by foreign host.

If there is no server listening on this port, it will like this:

$ telnet localhost 25
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

This means your services might not be running or enabled. You need to first enable/run this service on port 25.

Also, according to the nodemail documentation you can use the sendmail binary as well:

'sendmail' alternative

Alternatively if you don't want to use SMTP but the sendmail

command then set property sendmail to true (or as the path to sendmail if the command is not in default path).

nodemailer.sendmail = true;

or

nodemailer.sendmail = '/path/to/sendmail';

If sendmail is set, then SMTP options are discarded. 

for reference, https://nodemailer.com/transports/sendmail/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...