Nodemailer - MS Exchaneg server - Error unable to verify the first certificate - javascript

I am trying to send email from NodeJS using out office MS Exchange Mail server. with below code. And get error
Our Admin said no certificates are needed.
Error:-
$ node test2.js
Error : { Error: unable to verify the first certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1048:34)
at TLSSocket.emit (events.js:182:13)
at TLSSocket._finishInit (_tls_wrap.js:628:8) code: 'ESOCKET', command: 'CONN' }
NodeJS Code:-
"use strict";
const nodemailer = require("nodemailer");
async function main() {
try {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'host',
port: 25,
secure : false, // true for 465, false for other ports
auth: {
user: 'user',
pass: 'password'
}
});
// setup email data
let mailOptions = {
from: 'me#email.com',
to: 'me#email.com',
subject: 'Hey you, awesome!',
html: '<b>This is bold text</b>',
text: 'This is text version!'
};
// send mail with defined transport object
let info = await transporter.sendMail(mailOptions)
console.log("Message sent: %s", JSON.stringify(info));
} catch (error) {
console.log('Error : ', error);
}
}
main(); // For testing

The below code change fixed the issue. Added this to the createTransport()
tls: {rejectUnauthorized: false}
Code:-
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'host',
port: 25,
secure : false, // true for 465, false for other ports
auth: {
user: 'user',
pass: 'password'
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
},
});

Related

Error 421 in NodeMailer and not send mail

I am running a server using Nodemailer.
Sometimes the api service respond me with 421.
"{"errorCode":421,
"erroText":"Error occurs",
"error":{"code":"EENVELOPE","response":"550 5.1.0 <xxx#xxxxxx.it> Connessione da 100.27.27.90 temporaneamente rifiutata / Connection from 100.27.27.90 temporarily rejected","responseCode":550,"command":"MAIL FROM"}}"
This is my code in node
var transporter = nodemailer.createTransport({
host: "smtps.aruba.it",
logger: true,
debug: true,
secure: true,
port: 465,
auth: {
user: "xxx#xxxxx.it",
pass: "xxxxx",
},
tls: {
minVersion: "TLSv1",
ciphers: "HIGH:MEDIUM:!aNULL:!eNULL:#STRENGTH:!DH:!kEDH",
},
});
let mailOptions = {
from: '"xxx" <xxx#xxx.it>',
to: email,
subject: "Riepilogo prenotazione",
html: `...`,
};
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
res.json({
errorCode: 421,
erroText: "Error occurs",
error: err,
});
} else {
res.json({
errorCode: 200,
status: "Appointment Created!",
});
}
});
transporter.close();
If I make 2 or 3 calls in a row, the service gives me error 421 with this description
The SMTP 550 error being returned to nodemailer is saying that server smtps.aruba.it is temporarily rejecting the programs request to send data. I suspect it's either 1)the smtp server or 2)some conflict with your dev environment and localhost.
I've had the most success with nodemailer in conjunction with an email sandbox like mailtrap, which allowed me to "trap" the outgoing mail from my local dev environment for testing.

Nodemailer connection timeout error using Godaddy SMTP server on aws

I am trying to send email using nodemailer using godaddy smtp server(secureserver.net).
On my local machine code works fine but when I deploy same code on aws server it gives Error: Connection timeout.
Here is my code
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'Godaddy',
host: 'smtpout.secureserver.net',
secureConnection: true,
port: 465,
auth: {
user: 'xxx#zzzzzz.com',
pass: '*******'
}
});
let mailOptions = {
from: 'xxx#zzzzzz.com',
to: 'aaaa#gmail.com',
subject: 'Test sub',
html: 'Test body'
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I have added port 465/25 in outbound port list for the server
Please let me know any workaround this?
(Solution - Latest) This one has worked successfully.
static transport = nodeMailer.createTransport({
// service: 'Godaddy', <--- Dont add this anymore --->
host: 'smtpout.secureserver.net',
port: 465,
auth: {
user: config.get('application.mail.MAIL_SENDER'),
pass: config.get('application.mail.MAIL_SENDER_PASSWORD')
},
tls: { rejectUnauthorized: false }
});
SendMailService.transport.sendMail(mailOptions, function (error: Error, response: SentMessageInfo) {
if (error) {
loggerService.logError(' Error in mail send ' + error);
}
loggerService.logDebug(' response in mail send ' + response);
callback(error, response);
});
This has worked in my case. I am using GoDaddy professional mail service.
I was also getting connection refused and connection timeout for some changes but this one(above mentioned code had worked).
If still there is an issue then check for DNS records whether it is pointing properly under "My Domain >> DNS >> DNS management". Over there check for A type and MX.

How to send mail through nodemailer

I am trying to send a simple mail through my website to my own other account. But it does not work. I am using node mailer and have followed the documentation precisely.
I have tried checking for any kind of syntax error or external factors.
//Send Mail
router.post('/volunteer', ensureAuthenticated, async(req, res) => {
// const { name, email, city } = req.body;
//Create the transporter
require('dotenv').config();
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
//set up mailOptions
let mailOptions = {
from: 'readsocially001#gmail.com',
to: 'vaibhav.ag.001#gmail.com',
subject: 'Volunteer',
text: 'HEY'
};
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log("some error");
} else {
console.log("SENT");
}
});
res.redirect('/volunteer');
});
It just goes into the if(err) statement and prints some error. The actual error is:
Error: Missing credentials for "PLAIN"
at SMTPConnection._formatError (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:781:19)
at SMTPConnection.login (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:452:38)
at connection.connect (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-transport/index.js:271:32)
at SMTPConnection.once (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:209:17)
at Object.onceWrapper (events.js:286:20)
at SMTPConnection.emit (events.js:198:13)
at SMTPConnection._actionEHLO (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:1309:14)
at SMTPConnection._processResponse (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:940:20)
at SMTPConnection._onData (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:746:14)
at TLSSocket.SMTPConnection._onSocketData (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:189:46)
at TLSSocket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at TLSSocket.Readable.push (_stream_readable.js:224:10)
at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) code: 'EAUTH', command: 'API' }
According to the official doc, you can't use plain credentials: https://nodemailer.com/usage/using-gmail/
To use nodemailer with Gmail you need to create an App and use the oauth like this:
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: 'user#example.com',
accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
}
});
More examples: https://nodemailer.com/smtp/oauth2/#examples
Hope it helps
You can try this.
var nodemailer = require('nodemailer');
var xoauth2 = require('xoauth2');
// listen for token updates (if refreshToken is set)
// you probably want to store these to a db
generator.on('token', function(token){
console.log('New token for %s: %s', token.user, token.accessToken);
});
// login
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: '{username}',
clientId: '{Client ID}',
clientSecret: '{Client Secret}',
refreshToken: '{refresh-token}',
accessToken: '{cached access token}'
})
}
});

Getting error while sending emails from nodemailer from server

I am trying to send emails from my node.js app using nodemailer.
I dont know why but when I try after uploading the files in my cpanel and check, it send error as :
"Error: connect ECONNREFUSED 74.125.24.108:587"
while it works fine in my localhost.
I even enabled to allow less secure apps in my google account.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'myemail#gmail.com',
pass: 'XXXXXXXXX'
},
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: 'myemail#gmail.com',
to: "myclient#gmail.com",
subject: 'Code',
text: "Hello",
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent');
}
});
Getting error :
"Error: connect ECONNREFUSED 74.125.24.108:587"
use this way on server , allow your gmail account for less security
https://support.google.com/cloudidentity/answer/6260879?hl=en
var transporter = nodemailer.createTransport({
service: "gmail",
secure: true,
auth: {
user: 'myemail#gmail.com',
pass: 'XXXXXXXXX'
}
});

javascript - how many emails we can send in nodeJs by using NodeMailer at a time?

I am working on an application which functionality is to sending continuous email using NodeJs and Angular. My problem is when I am sending more than 40 or 50 emails then, it is not delivering to recipient and I am also not getting any kind of error in console
var emailArray = [hello1#gmail.com, hello1#gmail.com ....hello40#gmail.com];
var transporter = nodemailer.createTransport(smtpTransport({
host: 'md-19.webhostbox.net',
port: 465,
ssl: true,
auth: {
user: 'info#abc.com', // bluehost
pass: 'abc12345'
}
}));
var message = {
from: "Name <info#abc.com>",
to: emailArray,
subject: 'subject',
html: '<b>Hello world</b>'
};
transporter.sendMail(message, function(error, info) {
console.log(error || info);
});
The only limit, is the the max tcp connections you can handle.

Categories