Nodemailer error: [Error: No transport method defined] - node.js

I am using nodemailer with sendgrid and i am getting the error : [Error: No transport method defined]
I am setting up the transport like so:
var email = nodemailer.createTransport("STMP", {
service: "Sendgrid",
auth: {
user: "username",
pass: "pass"
}
})

it says "STMP" rather than "SMTP" - simple typo

You're using a wrong method... Instead of STMP use SMPT(simple mail transfer protocol).
As Below
var transport = nodemailer.createTransport("SMTP", {
service: 'gmail',
auth: {
user: 'youremail#address.com',
pass: 'yourpassword'
}
})

There was just the typing mistake. You wrote the wrong spelling of SMTP.
So, the correct solution was :-
var email = nodemailer.createTransport("SMTP", {
service: "Sendgrid",
auth: {
user: "username",
pass: "pass"
}
})

Related

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.

Why does nodemailer throw the error: Invalid credentials? [duplicate]

I would like to find a way to send email from my app using nodemailer to the users either with some kind of google authentication or any other way. Below mentioned working code has stopped working after Google has disabled less secure app option.
const nodemailer = require('nodemailer')
const sendEmail = async options => {
const transporter = nodemailer.createTransport({
// host: "smtp.gmail.com",
// port: "465",
// secure: true,
service:'gmail',
auth: {
user: "USER_EMAIL",
pass: "USER_PASSWORD"
},
tls:{rejectUnauthorized:false}
})
const message = {
from: `${process.env.FROM_NAME} <${process.env.FROM_EMAIL}>`,
to: options.email,
subject: options.subject,
text: options.message,
html: options.message,
attachments: [
{
filename: '.png',
path: __dirname + '.png',
cid: '.png'
}
]
}
const info = await transporter.sendMail(message)
console.log('Message sent : %s', info.messageId)
console.log(__dirname)
}
module.exports = sendEmail
At the time of writing, Less Secure Apps is no longer supported by google. And you can't use your google account password.
You're gonna have to generate a new app password.
App passwords only work if 2-step verification is turned on.
Follow this steps to get the app password
Go to https://myaccount.google.com/security
Enable 2FA
Create App Password for Email
Copy that password (16 characters) into the pass parameter in Nodemailer auth.
const client = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "username#gmail.com",
pass: "Google-App-Password-Without-Spaces"
}
});
client.sendMail(
{
from: "sender",
to: "recipient",
subject: "Sending it from Heroku",
text: "Hey, I'm being sent from the cloud"
}
)
You should check out Xoauth2.
Nodmailer supports serval types of Oauth
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: "user#example.com",
clientId: "000000000000-xxx0.apps.googleusercontent.com",
clientSecret: "XxxxxXXxX0xxxxxxxx0XXxX0",
refreshToken: "1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx",
accessToken: "ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x",
expires: 1484314697598,
},
});

Using Webmail with Nodemailer

I used gmail with node mailer to create a login system in js node.
In development mode I used gmail.
const sendEmail = async options =>{
//1)Create a transporter
// const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL_SEND_SESSION,
pass: process.env.EMAIL_SEND_PASSWORD,
}
});
//2) Define the mail options
const mailOptions = {
from:'Creative Point <test764#gmail.com>',
to:options.email,
subject:options.subject,
html:options.message
}
//3) Actually send the email
await transporter.sendMail(mailOptions);
};
But in production mode I would like to use webmail,I received when I created the data and port account on cpanel.
const transporter = nodemailer.createTransport({
service:'smtp.specialsoft.ro ',
port: 465,
auth: {
user: process.env.EMAIL_SEND_SESSIONCPANEL,
pass: process.env.EMAIL_SEND_PASSWORDCPANEL,
}
});
But it gives me the next mistake.
Error: There was an error sending the email.Try again later!Error: Invalid login: 535-5.7.8 Username
and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials g20sm3266545ejz.88 - gsmtp
at C:\Users\Cosmin\Desktop\Authentification System Node
JS,Express,JsonWebToken\controllers\sessionController.js:56:21
at processTicksAndRejections (internal/process/task_queues.js:93:5)
I haven't found anything relevant on the internet about this error.
I solved the problem by following the steps below.
I installed a module: nodemailer-smtp-transport.
const smtpTransport = require('nodemailer-smtp-transport');
I corrected the host name and some options.
host:'mail.mydomani.ro'
secureConnection: false,
tls: {
rejectUnauthorized: false
}
3)I integrated the module nodemailer-smtp-transport in the transporter.
const transporter = nodemailer.createTransport(smtpTransport({
host:'mail.mydomanin.ro',
secureConnection: false,
tls: {
rejectUnauthorized: false
},
port: 587,
auth: {
user: process.env.EMAIL_SEND_SESSION,
pass: process.env.EMAIL_SEND_PASSWORD,
}
}));

Send Email Using Microsoft 365 Email Server In NodeJS

let transporter = nodemailer.createTransport({
service: "Outlook365",
host: 'smtp.office365.com',
port: 587,
tls: {
ciphers:'SSLv3'
},
auth: {
user: 'username',
pass: 'password'
}
});
I have an EAUTH error while sending an email, please check the image for error.
[1]: https://i.stack.imgur.com/snt3T.jpg
This code should do what you wish, you'll need to set your password to test this.
If the password is incorrect, you'll get an error:
Error: Invalid login: 535 5.7.3 Authentication unsuccessful message.
const nodemailer = require('nodemailer');
// Set this from config or environment variable.
const PASSWORD = '....';
async function send365Email(from, to, subject, html, text) {
try {
const transportOptions = {
host: 'smtp.office365.com',
port: '587',
auth: { user: from, pass: PASSWORD },
secureConnection: true,
tls: { ciphers: 'SSLv3' }
};
const mailTransport = nodemailer.createTransport(transportOptions);
await mailTransport.sendMail({
from,
to,
replyTo: from,
subject,
html,
text
});
} catch (err) {
console.error(`send365Email: An error occurred:`, err);
}
}
send365Email("from#example.com", "to#example.com", "Subject", "<i>Hello World</i>", "Hello World");
You will need to disable the SmtpClientAuthenticationDisabled through the online power shell with the command
Set-TransportConfig -SmtpClientAuthenticationDisabled $false
You can check for more information at Enable or disable authenticated client SMTP submission (SMTP AUTH) in Exchange Online
You can find the detailed information here:
https://developer.microsoft.com/en-us/graph/quick-start?code=M.R3_BAY.822b5ade-d816-85bb-ec94-8c349cdfca4b&state=option-node

Error sending email using nodemailer via Office365 smtp (MEANjs scaffold)

I'm trying to use Office365 SMTP to send email using Nodemailer (in a MEANjs scaffold), but I get the following error:
[Error: 140735277183760:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:795:]
I'm using the following Nodemailer options:
{
host: 'smtp.office365.com',
port: '587',
auth: { user: 'xxxx', pass: 'xxxx' },
secure: 'false',
tls: { ciphers: 'SSLv3' }
}
Removing the tls field doesn't make a difference. What am I missing?
The solution was simple. The 'secure' field should be 'secureConnection'. The MEANjs scaffold that generated the configs created mailer options with the 'secure' field. The rest of the options are fine. For anyone that needs a working Office365 SMTP nodemailer options block, the following should work:
{
host: 'smtp.office365.com',
port: '587',
auth: { user: 'xxxx', pass: 'xxxx' },
secureConnection: false,
tls: { ciphers: 'SSLv3' }
}
I know this is old but if anyone looks this up in 2019, you can just add
service: "Outlook365"
and you won't have to specify connection options.
Node Mailer Docs
let transporter = nodemailer.createTransport({
service: "Outlook365",
auth: {
user: 'FROMUSER#office365.com',
pass: 'FROMUSERPASS'
},
})
let info = transporter.sendMail({
from: 'FROMUSER#office365.com',
to: 'TOUSER#office365.com',
subject: 'Test',
text: 'hello world',
html: '<h1>TEST</h1>'
})
This nodemailer documentation https://nodemailer.com/2-0-0-beta/setup-smtp/ indeed states options.secure and not options.secureConnection. It also suggests, in an example, that options.secure is expecting a boolean value true or false and not a string value 'true' or 'false'. Removing the '' from around 'false' works for me.
My problem was that the username and password were spelled correctly but I did not login in the Account after creation. So I used a mail program (Thunderbird) to login once and had to change my password and then I had access over Nodemailer.

Resources