Error TLSSocket.onConnectSecure trying to send Email from Hotmail with Nodemailer - node.js

I am trying to use nodemailer with hotmail to send Email. testing from postman i am getting this as Error:
Microsoft Windows [Version 10.0.19041.1348]
(c) Microsoft Corporation. All rights reserved.
D:\node_apps\subscription-api>node subApp.js
App running on Port: 8080
D:\node_apps\subscription-api\subApp.js:139
if(error) throw error;
^
Error: self signed certificate in certificate chain
at TLSSocket.onConnectSecure (_tls_wrap.js:1514:34)
at TLSSocket.emit (events.js:400:28)
at TLSSocket._finishInit (_tls_wrap.js:936:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:708:12) {
code: 'ESOCKET',
command: 'CONN'
}
D:\node_apps\subscription-api>
My code is looking thus:
app.get('/api/v1/sendsubexpnotice/:email',function(req,res){
let email = req.params.email;
if(!email){
return res.status(400).send({error: true, message: 'Please provide E-mail'});
}
const transporter = nodemailer.createTransport({
service:'hotmail',
auth:{
user: 'xxxxxxxxxx#hotmail.com',
pass: 'xxxxxxxxxxx'
}
});
const mailOptions = {
from :'xxxxxxxxxxxxxx#hotmail.com',
to: email,
subject: 'Your Subscription is about to Expire!',
text : 'Your Subscription is about to Expire!'
};
transporter.sendMail(mailOptions,function(error,info){
if(error) throw error;
return res.send({error:false, data: results, message: 'OK'});
})
})
What do I do to resolve such error?
How do i get this to send email? kindly advice.

I fixed it.
just did some few minor changes
Code looks like so
app.get('/api/v1/sendsubexpnotice/:email',function(req,res){
let email = req.params.email;
if(!email){
return res.status(400).send({error: true, message: 'Please provide E-mail'});
}
const transporter = nodemailer.createTransport({
service:'gmail',
host: 'smtp.gmail.com',
port:'587',
auth:{
user: '****#gmail.com',
pass: '****'
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});
const mailOptions = {
from :'****#gmail.com',
to: email,
subject: 'Your Subscription is about to Expire!',
text : 'Your Subscription is about to Expire!'
};
transporter.sendMail(mailOptions,function(error,info){
if(error)throw error;
return res.send({error:false, data: info, message: 'OK'});
})
})

Related

Nodemailer smtp timeout

SMTP informationI'm getting this error :-
Error: connect ETIMEDOUT 144.76.97.27:25
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1132:16)
{
errno: -4039,
code: 'ESOCKET',
syscall: 'connect',
address: '144.76.97.27',
port: 25,
command: 'CONN'
}
my code is:-
let transporter = nodemailer.createTransport({
host: "mail.tusharjoshi.tech",
port: 25,
secure: false, // true for 465, false for other ports
auth: {
user: "contact#tusharjoshi.tech", // generated ethereal user
pass: "mypAssword" // generated ethereal password
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});
const mailOptions = {
from: "tusharjoshi.tech",
to: "hellotusharr2318#gmail.com",
subject: `message from ${req.body.name}`,
text: `Message recieved from ${req.body.email}: and message is about: ${req.body.message}`
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
res.send("error");
} else {
console.log("send");
res.send("success");
}
});
did you check this answer? nodemailer SMTP server connection issue (Error: connect ETIMEDOUT) but it works for another system not in mine

Node.js nodemailer Gmail SMTP not working

require("dotenv").config();
"use strict";
var nodemailer = require("nodemailer");
const SMTPTransport = require("nodemailer/lib/smtp-transport");
console.log("Nach attr");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// create reusable transporter object using the default SMTP transport
console.log("vor transporter");
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secureConnection: true, // true for 465, false for other ports
auth: {
user: 'email', // generated ethereal user
pass: 'password', // generated ethereal password
tls: {
rejectUnauthorized: false
}
},
});
transporter.verify().then(console.log).catch(console.error);
let mailOptions = {
from: 'testsender',
to: 'reciever',
subject: 'Test Mail',
text: '123456789'
};
console.log("vor transporter.send");
transporter.sendMail(mailOptions, function (err, data) {
if (err) {
console.log('Error Occurs: ', err);
} else {
console.log('Email sent!');
}
});
//console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321#example.com>
// Preview only available when sending through an Ethereal account
//console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
Console output
Error Occurs: Error: connect ETIMEDOUT 64.233.166.108:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
errno: 'ETIMEDOUT',
code: 'ESOCKET',
syscall: 'connect',
address: '64.233.166.108',
port: 465,
command: 'CONN'
}

nodemailer error, getting : Invalid login: 451 4.7.0 Temporary server error. Please try again later.(researched, limited resources on stackoverflow)

After checking similar posts online, still couldn't figure out the issue. I am trying to send email through nodemailer by using my outlook business email. I am getting this error message that I couldn't fix.
Here is error message:
Error: Invalid login: 451 4.7.0 Temporary server error. Please try again later. PRX4 [BY3PR10CA0026.namprd10.prod.outlook.com]
at SMTPConnection._formatError (/Users/zhongzechen/Hello--website--backend/node_modules/nodemailer/lib/smtp-connection/index.js:774:19)
at SMTPConnection._actionAUTHComplete (/Users/zhongzechen/Hello--website--backend/node_modules/nodemailer/lib/smtp-connection/index.js:1518:34)
at SMTPConnection.<anonymous> (/Users/zhongzechen/Hello--website--backend/node_modules/nodemailer/lib/smtp-connection/index.js:1476:18)
at SMTPConnection._processResponse (/Users/zhongzechen/Hello--website--backend/node_modules/nodemailer/lib/smtp-connection/index.js:937:20)
at SMTPConnection._onData (/Users/zhongzechen/Hello--website--backend/node_modules/nodemailer/lib/smtp-connection/index.js:739:14)
at TLSSocket.SMTPConnection._onSocketData (/Users/zhongzechen/Hello--website--backend/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
Tried adding tls to { ciphers: 'SSLv3' }, didn;t work out.
Here is part of my code:
const output = `<h3>Hello</h3>`;
let transporter = nodemailer.createTransport({
host: "smtp-mail.outlook.com",
secure: false,
port: 587,
auth:{
user:"myoutlookemail",
pass:"mypassword"
}
})
let mailOptions ={
from:'myoutlookemail',
to:req.body.email,
subject:"Hello there",
text:"Hello there",
html:output
}
transporter.sendMail(mailOptions,(error,info)=>{
if(error){
console.log('*********ERROR IS HAPPENING*************')
return console.log(error);
}
console.log('Message Sent~~~~~~~~~~~~~~~: %s',info.response);
//console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
});
Try this
Edit as required
const nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport({
service: 'service_provider',
auth: {
user: 'email',
pass: 'password'
}
});
const mailOptions = {
from: 'email',
to: req.body.email,
subject: 'Protocol 5416',
text: "Anything"
};
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});

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'});
}

Error when sending email using nodemailer / smtp.gmail.com

I'm trying to set up nodemailer for an app. I'm receiving an error when I try it.
This is my setup:
email = setting.email;
password = setting.password;
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: '***********#gmail.com', // real email
pass: '*********' // real password
}});
// var transporter = nodemailer.createTransport({
// service: 'gmail',
// auth: {
// user: email, // Your email id
// pass: password // Your password
// }
// });
var mailOptions = {// sender address
from: email,
to: to, // list of receivers
subject: sub, // Subject line
text: text, //, /// plaintext body
html: html
}
//console.log(JSON.stringify(mailOptions));
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.error(error);
} else {
console.log(info.response);
}
;
});
});
} catch (error) {
console.error(error);
}
This is the first time I've tried using nodemailer. I am using real email and password. The error are:
(node:18974) [DEP0025] DeprecationWarning: sys is deprecated. Use util
instead. Magic happens on port 5000 ERROR! ERROR! ERROR! ERROR!
provider_analytic_daily saved. { Error: Connection timeout
at SMTPConnection._formatError (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:528:15)
at SMTPConnection._onError (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:514:16)
at SMTPConnection. (/root/faszz/node_modules/smtp-connection/lib/smtp-connection.js:236:14)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5) code: 'ETIMEDOUT', command: 'CONN' }
functionObj = {};
functionsObj.getSmtpTransport = function() {
var smtpTransport = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "<your email address>",
pass: "<your pass>"
}
});
return smtpTransport;
}
functionsObj.sendMailForPasswordReset = function(to, token) {
var smtpTransport = functionsObj.getSmtpTransport();
var mailOptions = {
to: to,
subject: "Blabla.com Reset Password",
html: "Hi,<br> Click to link for resetting password.<br><a href='https://<blabla.com>/reset/" + urlencode(token) + "'>Click Me !!!</a>"
}
smtpTransport.sendMail(mailOptions, function(error, response) {
if (error) {
return error;
}
else {
return true;
}
});
};
Hi,
The code above worked in my project without any error. But you should allow to third party softwares to use your google account from google.
Here the google's support page for this case:
https://support.google.com/accounts/answer/6010255?hl=en
I recommend that all input should be validated :) For example if you are taking "to" parameter from user. You should validate "to" parameter is a valid email address ?

Resources