Sending emails using nodemailer in node.js - node.js

I've got an authorisation problem while sending emails via my own email address. Here is my code:
var transporter = nodemailer.createTransport({
host: 'serverDomain',
port: 25,
secure: 'false',
tls: {ciphers: "SSLv3"},
auth: {
user: 'myEmailAddress',
pass: 'myPassword'
}
});
transporter.sendMail({
from: 'myEmailAddress',
to: 'someAddress',
subject: 'Subject',
html: 'Some HTML code here'
}, function(error, info){
if(error){
console.log(error);
}
});
And the error is as follows:
{ [Error: Invalid login: 535 authorization failed (#5.7.0)]
code: 'EAUTH',
response: '535 authorization failed (#5.7.0)',
responseCode: 535 }
I'm sure the credentials are correct. I get the same error also on ports 465 and 587.
Any help will be appreciated. Thanks.

This can be issue with giving access rights in your email account. So can you make sure those setups you did properly.
Need to Allow Less Secure Apps in case of using Gmail service
If you are using 2FA in that case you would have to create an Application specific password. Because your normal password wont work.

Related

Error: Invalid greeting. response=421 Service not available: 421 Service not available

i tried using nodemailer to send emails from my outlook account to other accounts but i keep getting this error.
Error: Invalid greeting. response=421 Service not available: 421 Service not available
code: 'EPROTOCOL',
response: '421 Service not available',
responseCode: 421,
command: 'CONN'
Here is my code
const nodemailer = require("nodemailer");
module.exports = function OTP() {
// Sends otp link to and code to user
return new Promise((resolve, reject) => {
const transporter = nodemailer.createTransport({
service: "hotmail",
port: "25",
secure: true,
auth: {
user: process.env.EMAIL, //Outlook email
pass: process.env.EMAIL_PASSWORD,
},
tls: {
ciphers: "SSLv3",
},
});
transporter.sendMail(
{
from: process.env.EMAIL,
to: email,
subject: "Test",
text: `
`,
},
(err, info) => {
if (err) {
console.log(err);
return reject({ message: "An error has occurred" });
} else return resolve({ message: "Email sent" });
}
);
});
};
i tried changing the port and switching to gmail but i get the same error.
I had this same error but in nestjs and this is my solution that you can try.
First in my configuration I did not define a port and my secure was set to false, so port was default to 587. I was sending mail and everything was fine untill one day I started to get the same error as you are getting now. so here is my fix
I use telnet to test my connection to the mail server:
a. Open terminal and type: telnet your_domain 587. eg
telnet smtp.sendgrid.net 587
I got 421 Service not available
I change secure to true which will now default the port to 465 and I try telnet again, this time
telnet smtp.sendgrid.net 465
I got no error. I test the endpoint again and everything works fine.
I hope this helps
Reference:
stackoverflow
nodemailersmtp

Using Nodemailer with Hotmail/Node.js

I know there are a lot of posts about this and i've cycled through a good amount of them, none of them panning out. So here we go:
Trying to send an email via Nodemailer in NestJS application. Tested with the documentations's pre done eretheral test email and it worked fine. Now i'm trying to connect it to the hotmail account (which I have attained an app password) and no luck so far. Here's the function:
export async function sendEmail(createTicketDto: CreateTicketDto) {
const { username, email, ticket_body, issue_type } = createTicketDto;
const emailBody = `
User ${email} reporting issue regarding ${issue_type}:\n
\t${ticket_body}\n
\t\tActive username is: ${username}`;
const transporter = nodemailer.createTransport({
service: 'hotmail',
host: 'smtp-mail.outlook.com',
secure: false,
port: 587,
auth: {
user: 'xxx#hotmail.com',
pass: 'PASSWORD',
},
tls: {
ciphers: 'SSLv3',
},
});
console.log(emailBody);
const mailData = await transporter.sendMail({
from: '"XXXSUPPORT"<XXX#hotmail.com>',
to: 'YYY#gmail.com',
subject: issue_type,
text: emailBody,
});
Logger.log(`Email sent with ID: ${mailData.messageId}`);
return mailData;
}
The error being returned resembles:
[Nest] 22727 - 02/10/2023, 1:32:32 PM ERROR [ExceptionsHandler] Invalid login: 535 5.7.3 Authentication unsuccessful [MN2PR17CA0007.namprd17.prod.outlook.com 2023-02-10T18:32:32.130Z 08DB0B4DDC8F1521]
Feedback is appreciated, thank you!!
So problem has been resolved, however a few notes for myself (and anyone who stumbles across this):
const transporter = nodemailer.createTransport({
service: 'hotmail',
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
After that it was still failing, so based on a throwaway line in another forum post, I sent a test email to and from the hotmail account, and... it worked! So apparently, if you haven't sent any emails from the target send account, at least in the case of hotmail, it will not work.

nodemailer is causing an error in aws ec2 production evnironment

I am sending emails in node.js using nodemailer and I have this configuration to send emails.
var transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port : 465,
secure: true,
auth: {
user: 'myemail#gmail.com',
pass: 'password'
}
});
const mailOptions = {
from: 'myemail#gmail.com', // sender address
to: user.company_email, // list of receivers
subject: 'EnStream New Account Signup Password', // Subject line
html: `<p style="font-size : 15px;">Please sing in to your en-stream account here http://demo.en-stream.com/auth/login with this password : ${userPassword}</p>`// plain text body
};
It's sending emails on local environment correctly but in production environment on Aws Ec2 it is not sending email and throwing this error
code: "EAUTH"
command: "AUTH PLAIN"
response: "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbua↵534-5.7.14 qup7zOOL4GtmYEf1HEHBASSSBgbuMNJRQqK2v3X9mcxDRX1SrXU2Y_LSqZm7Y7yJvH2PwL↵534-5.7.14 JZW6iSXxsRhBdntFAAamrvitpdKS_YJiE-pEnXWakITAz1PAzwDMxjJPKntQrLl2Qx-xA1↵534-5.7.14 zZ4aTvKvYOAk85YHwABnnd0wHU2HkUeHPoDYqgXUWgSA_8Rrn4xkIsUN> Please log↵534-5.7.14 in via your web browser and then try again.↵534-5.7.14 Learn more at↵534 5.7.14 https://support.google.com/mail/answer/78754 a11sm34494120wrx.5 - gsmtp"
responseCode: 534
I allowed outbound port 465 in my ec2 instance security group as well.

sending email error:Error: Invalid login: 454 4.7.0 Too many login attempts, please try again later. b18sm2198190pgk.36 - gsmtp

I am getting this error while sending mail through my Node js application using gmail SMTP server.
I have not reached the daily limit to send mails, my config looks like this:
var transporter = nodemailer.createTransport({
service: 'Gmail',
pool: true,
port: 587,
auth: {
user: username,
pass: password
}
});
Kindly help.
Thanks

ETIMEDOUT connect error using nodemailer in Nodejs Openshift application

I'm facing some problems using the nodemailer module in my node.js single gear non-scalable application in Openshift. As the documentation suggested, I initialized the transporter object and used the sendMail function. A simplified version of my code is
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'my.mail#gmail.com',
pass: 'mypassword'
}
});
var mail = {
from: 'my.mail#gmail.com',
to: 'test#mydomain.com',
subject: 'Test mail',
html: 'Test mail'
};
transporter.sendMail(mail, function(error, info) {
if(error){
console.log(error);
}else{
console.log(info);
}
});
This code works correctly when I run it on my local machine, but when I try to execute it on the server I got an ETIMEDOUT error, as if the application couln't connect to the smtp server.
{ [Error: connect ETIMEDOUT] code: 'ETIMEDOUT', errno: 'ETIMEDOUT', syscall: 'connect' }
I've tried to increase the timeout connection parameter, but I got the same results.
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'my.mail#gmail.com',
pass: 'mypassword'
},
connectionTimeout: 5 * 60 * 1000, // 5 min
});
Is there any firewall or default Openshift settings or enviroment variable I'm missing?
I've to run a few more tests, but I think I figured out what the problem was. The authentication via username and password is a not-secure authentication method for Gmail. As written in the Nodemailer documentation
Even though Gmail is the fastest way to get started with sending emails, it is by no means a preferable solution unless you are using OAuth2 authentication. Gmail expects the user to be an actual user not a robot so it runs a lot of heuristics for every login attempt and blocks anything that looks suspicious to defend the user from account hijacking attempts. For example you might run into trouble if your server is in another geographical location – everything works in your dev machine but messages are blocked in production.
I upgraded my authorization method using XOAuth2, as well explained in this guide and now the mails are correctly sent.
In my case the problem is with the port
I have changed port from 25 to 2525 and it worked.
try changing port
If you are trying to run this from the localhost.
Add the following line of code after the auth option
tls:{
rejectUnauthorized:false
}
It will work but you might consider upgrading to XOAuth2 as suggested by #matt.kiwi
const transporter = await nodemailer.createTransport(smtpTransport({
service: "Outlook365",
host: "smtp.office365.com",
port: "587",
tls: {
ciphers: "SSLv3",
rejectUnauthorized: false,
},
auth: {
user: '***',
pass: '***'
}
}));
It works for outlook

Resources