Nodemailer ECONNREFUSED - node.js

I am trying to use nodemailer to send an email but I keep getting this error:
Here is my code:
const nodemailer = require('nodemailer');
...
app.post('/api/SendEmail', (req, res) => {
var transporter = nodemailer.createTransport({
auth: {
user: 'x#gmail.com',
pass: 'x'
}
});
var mailOptions = {
from: 'x#gmail.com',
to: 'x#incasu.co.za',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(err, info) {
if (err) return res.status(400).send(err);
return res.status(200).send({response: 'success'});
});
})
Could someone please help me with this. I basically followed this process: https://www.w3schools.com/nodejs/nodejs_email.asp
UPDATE:
I added service: 'gmail', in the transporter as suggested by Janith Kasun:
service: 'gmail',
auth: {
user: 'x#gmail.com',
pass: 'x'
}
My error now changed to this:

I found this answer:
NodeJs Error When Submitting Email using Nodemailer
I have no idea what any of these settings do, but my emails are now being sent correctly with this code:
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});

Related

response: '535-5.7.8 Username and Password not accepted. Learn more at\n'

My Node js code is sending proper email on localhost but in backend there showing email and password not valid ?
const nodemailer = require("nodemailer");
const sendEmail = async (subject, message, send_to, sent_from, reply_to) => {
// Create Email Transporter
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
// Options for sending email
const options = {
from: sent_from,
to: send_to,
reply_to: reply_to,
subject: subject,
html: message,
};
// Check email sent successfully or not
transporter.sendMail(options, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
}
});
};
module.exports = sendEmail;

Unable to send email through node js, tried few methods

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
secureConnection: false,
port: 587,
tls: {
ciphers: 'SSLv3'
},
requireTLS: true,
auth: {
user: 'mygmail',
pass: 'mypass'
}
});
var mailOptions = {
from: 'mygmail',
to: 'receiver gmail',
subject: 'Sending Email using Nodemailer',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error)
return console.log(error);
console.log('Email sent: ' + info.response);
});
Above is my code for sending email through node js but I keep encounter a timeout error as below
{ Error: connect ETIMEDOUT 74.125.24.109:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
errno: 'ETIMEDOUT',
code: 'ECONNECTION',
syscall: 'connect',
address: '74.125.24.109',
port: 587,
command: 'CONN' }
I also tried another basic way as for the following link: https://www.w3schools.com/nodejs/nodejs_email.asp but it is not working as well.
I've already turned on allow the less secure app on my google account.
Beside of that, I also tried the method like the one in the following the link: https://jsfiddle.net/burawi/1u9m2mou/ and it is still unable to work, I generated all the client_id, client_secret, access_token, and refresh_token.
Does anyone have any latest guide or solution for sending email through node js?
Thanks
This is a network restriction issue.
Please try the same thing in your mobile network and it will work.
router.get('/mailTest', (req, res) => {
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: "mrmanagementbbsr#gmail.com", // generated ethereal user
pass: "Test#123" // generated ethereal password
}
});
let mailOptions = {
from: "mrmanagementbbsr#gmail.com", // sender address
to: 'nsubhadipta#gmail.com', // list of receivers
subject: "test subject", // Subject line
text: 'demo text'
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
res.json({ status: -1, message: 'Error Occured', error: error });
}
else {
res.json({ status: 1, message: "Email Sent" });
}
});
});
Ensure you turn on your Less secure app access, else your emails will not go through.
Then you might want to try this example:
{
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mymail',
pass: 'mypass'
}
});
let mailOptions = {
from: 'mygmail',
to: 'receiver gmail',
subject: 'Sending Email using Nodemailer',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, (error, info)=>{
if (error) {
console.log(error);
} else {
console.log('Email sent ' + info.response');
}
});
res.json({success: 'whatever message you plan on writing'});
}

How to send base64 string as image in Node.js using nodemailer?

I have nodejs get request which need to send the base 64 string as image in the email which as shown below
router.post('/sendEmailNotification', function(req, res, next){
var inlineBase64 = require('nodemailer-plugin-inline-base64');
//here i have big image
let img ='/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBx'
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 587,
secure: false,
// secure: true,
secureConnection: false,// true for 465, false for other ports
auth: {
user: 'dhanalakshmi.07k#gmail.com',
pass: '23'
},
tls:{
rejectUnauthorized:false
}
});
transporter.use('compile', inlineBase64({cidPrefix: 'somePrefix_'}));
var mailOptions = {
from: 'dhanalakshmi.06k#gmail.com',
to: 'dhanalakshmi.06k#gmail.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!',
html: '<img src=data:image/png;base64,'+img+'/>'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
res.send(info.response);
}
});
});
after the email send this should received as image but it showing the entire tag with image string please say
html: '<img src=data:image/png;base64,'+img+'/>' how should i send this to get the actual image
Use it like this:
html: '<html><body><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA9QAAADmCAIAAAC77FroAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAO..."></body></html>'

nodemailer attachment text file

I failed to attach a txt file that I created earlier in the script to a mail to be sent via nodemailer. The mail is successfully sent however, it is empty. How would I fix this?
var nodemailer = require ('nodemailer');
let transporter = nodemailer.createTransport({
service: 'gmail',
secure: false,
port: 460,
auth: {
user: 'name#gmail.com',
pass: 'password'
},
tls: {
rejectUnauthorized: false
}
});
let mailOptions = {
from: "name" <'name#gmail.com',
to: 'name#gmail.com',
subject: 'group_1',
attachement: [ {
filename: 'group_1.txt',
path: /Users/derinserbetcioglu/Documents/NodeJS/'group_1.txt' } ]
};
transporter.sendMail(mailOptions, (error, info) => {
if (error){
console.log(error);
}
console.log("the message was successfully sent!")
console.log(info);
})
Try the path as path:
'/Users/derinserbetcioglu/Documents/NodeJS/group_1.txt'
instead of path: /Users/derinserbetcioglu/Documents/NodeJS/'group_1.txt'
And i think it should be attachments not attachment

Nodemailer without Gmail

I'm using NodeJS and I want to configure my email account with Nodemailer.
All the Nodemailer examples that I found are for Gmail...
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user#gmail.com',
pass: 'userpass'
}
});
How can I put another service? In special I bought a email domain in ovh (SSL0.OVH.NET) and I interested to configure this email account.
I tried but I don't found the way to get this...
Thank you!
I create a module Mail for send html mails by SMTP
exports.send = function(email, subject, htmlcontent, callback) {
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var configMail = require('../bin/config').mail;//my json configurations mail
var transporter = nodemailer.createTransport(smtpTransport({
host: configMail.host, //mail.example.com (your server smtp)
port: configMail.port, //2525 (specific port)
secureConnection: configMail.secureConnection, //true or false
auth: {
user: configMail.auth.user, //user#mydomain.com
pass: configMail.auth.pwd //password from specific user mail
}
}));
var mailOptions = {
from: configMail.email,
to: email,
subject: subject,
html: htmlcontent
};
transporter.sendMail(mailOptions, function(err, info){
transporter.close();
if(err) {
callback(err, info);
}
else {
callback(null, info);
}
});
}
The usage:
var Mail = require('../utils/Mail'); //require this module
Mail.send('[EMAIL TO SEND]', '[TITLE]', '[YOUR HTML TEMPLATE MAIL]', function(err, info) {
if(err) {
//error
}
else {
//Email has been sent and you can see all information in var info
}
});
On creating an email id in the domain (mostly through c panel), check the mail configurations for that mail address and look for SMTP details such as port, username, etc. Then enter those details in the nodemailer transporter. For example, my transporter details were:
const transporter = nodemailer.createTransport({
host: "mail.schoolprogramming.tech",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: "demo#schoolprogramming.tech",
pass: "**************"
}
});

Resources