Error: connect ECONNREFUSED 127.0.0.1:465 send mail - javascript

I use the email account of my site contact#monsite.com to send smtp alert messages to the email of my users. but it still gives me this error
Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
errno: 'ECONNREFUSED',
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN'
}
here is my code
// send mail function
exports.sendMailer = (optionsTransporterMail = OptionsTransporterMail, optionsMail = OptionsMail, callback) => {
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport(optionsTransporterMail);
transporter.sendMail(optionsMail, function (err, info) {
callback(err, info)
});
}
sendMailer({
service: 'mail.mywebsite.com',
port: 465,
secure: true,
auth: {
user: 'contact#mywebsite.com',
pass: '12345678'
}
}, {
from: '"My app <contact#mywebsite.com>',
to: user#domain.com,
subject: "Notify",
text: "Hello worlds",
html: "Hello worlds"
}, (err, info) => {
if err console.log(err);
console.log(info)
})
it still gives this error
Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
errno: 'ECONNREFUSED',
code: 'ESOCKET',
syscall: 'connect',
address: '127.0.0.1',
port: 465,
command: 'CONN'
}

I assume you want to use mail.mywebsite.com in order to send an email. In those terms, you have to set up host value in options, because it seems like by default it uses localhost (127.0.0.1).
Please try the following code:
sendMailer({
host: 'mail.mywebsite.com',
port: 465,
secure: true,
auth: {
user: 'contact#mywebsite.com',
pass: '12345678'
}
}, {
from: '"My app <contact#mywebsite.com>',
to: user#domain.com,
subject: "Notify",
text: "Hello worlds",
html: "Hello worlds"
}, (err, info) => {
if err console.log(err);
console.log(info)
})

Related

Switching from mqtts protocol to wss in nodeJS

I can connect to my MQTT broker, using mqtts protocol like this:
mqtt = require('mqtt')
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8883,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
protocol: 'mqtts',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
// initialize the MQTT client
let client = mqtt.connect(options);
// setup the callbacks
client.on('connect', function () {
console.log('Connected');
});
client.on('error', function (error) {
console.log(error);
});
client.on('message', function (topic, message) {
// called each time a message is received
console.log('Received message:', topic, message.toString());
});
// subscribe to topic 'my/test/topic'
client.subscribe('mytest');
// publish message 'Hello' to topic 'my/test/topic'
client.publish('mytest', 'Hello');
This code, yields, as expected:
Connected
Received message: mytest Hello
Now, I would like to connect to this same broker but using wss protocol instead, thus I tried:
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8884,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
protocol: 'wss',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
However, in this case, I got:
Error: connect ECONNREFUSED 127.0.0.1:8884
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8884
}
I have finally managed to connect with these changes:
let options = {
username: 'light29',
password: 'Mqtt29#!',
}
// initialize the MQTT client
let client = mqtt.connect('wss://0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud:8884/mqtt', options);
Still, I do not know why explicitly passing protocol, host, port and path does solve it.
The problem in the original is that protocol in the options should be schema and should container URL schema e.g.
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8883,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
schema: 'mqtts://',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
or
let options = {
host: '0d9d5087c5b3492ea3d302aae8e5fd90.s2.eu.hivemq.cloud',
port: 8883,
path: '/mqtt',
clientId: 'mqttx_2d803d2743',
schema: 'wss://',
username: 'light29',
password: 'Mqtt29#!',
cleanSession: true,
}
protocol is not a valid option.

Nodemailer was sending mails but then it suddenly stopped, now it's giving me this error

This worked like some days ago but then it stopped. I really don't know what's going on. PS: I am on a Mac and it does not look like it's a firewall issue.
exports.sendEmail = (recipient, subject) => {
const mailTransporter = nodeMailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS
}
})
const mailOptions = {
from: process.env.EMAIL_USER,
to: recipient,
subject,
html: htmlTemplate(generatePasswordResetLink(),recipient),
}
return mailTransporter.sendMail(mailOptions, (error, success) => {
if (error) {
console.log(error);
return error;
}
return success;
})
The console.log(error); in the code spits this
{ Error: connect ECONNREFUSED 74.125.133.109:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
errno: 'ECONNREFUSED',
code: 'ESOCKET',
syscall: 'connect',
address: '74.125.133.109',
port: 465,
command: 'CONN' }
And I have tried like everything possible to get it to work. I have also restarted my laptop.
PS:
user: 'a valid gmail account',
pass: 'correct pass for the account'

Zoho with NodeMailer not working error code: 'EDNS'

I try to send an email with Nodemailer and always get the same error:
hostname: 'smtp.zoho.com',
secure: true,
port: 465,
auth: {
user: 'maria#mydomain.my',
pass: 'apppassgenerated'
}
});
I tried too:
service: 'Zoho',
auth: {
user: 'maria#mydomain.my',
pass: 'apppassgenerated'
}
});
I always get: { Error: queryA EREFUSED smtp.zoho.com
at QueryReqWrap.onresolve [as oncomplete] (dns.js:213:19)
errno: 'EREFUSED',
code: 'EDNS',
syscall: 'queryA',
hostname: 'smtp.zoho.com',
command: 'CONN' }
Ok, It was because I was using free plan of Google Functions. If you want to integrate external tools you have to pay 🤷‍♂️

Cannot send email in nodejs using nodemailer

I used nodemailer and my code as follows:
const nodemailer = require('nodemailer');
module.exports = function(obj) {
return new Promise((resolve, reject) => {
console.log('In root to mail send file...');
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'myemail#gmail.com',
pass: 'my password'
}
});
let mailOptions = {
from: '<myemail#gmail.com>', // sender address
to: obj.email, // list of receivers
subject: obj.subject, // Subject line
text: obj.msg, // plain text body
html: obj.html_msg // html body
};
console.log('sending function');
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log('Error due to send mail' + error);
reject(error);
} else {
console.log('Message %s sent: %s', info.messageId, info.response);
resolve(info);
}
});
});
}
When I run the code, I got this error
{
Error: connect ETIMEDOUT 74.125.200.109:465 at
Object.exports._errnoException (util.js:1022:11) at
exports._exceptionWithHostPort (util.js:1045:20 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: 'ECONNECTION',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '74.125.200.109',
port: 465,
command: 'CONN'
}
I don't know why, please help me if anybody know why this error occurred.
I have already set Access for less secure apps: turn on in my gmail account.
You could be behind a network proxy which could be causing a timeout error.

nodemailer google smtp connection closed error

I am trying to send email using nodemailer and nodemailer-smtp-transport in express js. i am creating a custom module to send mails to the user. When ever i am executing the script it is throwing 2 specific errors.
Error 1
D:\Projects\project_name\modules\mailer.js:45
throw error;
^
Error: Connection closed
at SMTPConnection.<anonymous> (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\lib\smtp-transport.js:113:29)
at SMTPConnection.g (events.js:260:16)
at emitNone (events.js:67:13)
at SMTPConnection.emit (events.js:166:7)
at SMTPConnection._destroy (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\lib\smtp-connection.js:558:10)
at SMTPConnection._onEnd (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\lib\smtp-connection.js:538:10)
at Socket.g (events.js:260:16)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
Error 2
{
[Error: getaddrinfo ENOTFOUND smtp.gmail.com smtp.gmail.com:465]
code: 'ECONNECTION',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'smtp.gmail.com',
host: 'smtp.gmail.com',
port: '465',
command: 'CONN' }
My Complete code is
var nodemailer = require('nodemailer');
var smtpTransport = require("nodemailer-smtp-transport")
var constants = require('./../modules/constants');
/*var smtpTransport = nodemailer.createTransport(constants.MAIL_HOST,{
service : "Gmail",
auth: {
user: constants.MAIL_FROM_AUTH,
pass: constants.MAIL_PASSWORD
}
});*/
var smtpTransport = nodemailer.createTransport(smtpTransport({
host : constants.MAIL_HOST,
secureConnection : constants.MAIL_SECURE,
port: constants.MAIL_PORT,
auth : {
user : constants.MAIL_FROM_AUTH,
pass : constants.MAIL_PASSWORD
},
ignoreTLS: true
}));
var sendMail = function(req, res, template, to, subject, callback) {
var mailOptions = {
from: constants.MAIL_FROM, // sender address
to: to, // list of receivers
subject: subject, // Subject line
text: "random text", // plaintext body
html: template // html body
}
console.log(mailOptions);
console.log(smtpTransport);
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
console.log("Message sent: " + response);
console.log(error);
if(error) {
throw error;
console.log(error);
}
else {
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages
});
}
exports.sendMail = sendMail;

Categories

Resources