Reputation: 1113
This is my settingController:
var sendSmtpMail = function (req,res) {
var transport = nodemailer.createTransport({
service:'gmail',
auth: {
user: "[email protected]",
pass: "qwerr@wee"
}
});
var mailOptions = {
from: "[email protected]",
to:'[email protected]',
subject: req.body.subject+"nodejs working ?",
text: "Hello world ?",
}
transport.sendMail(mailOptions, function(error, response){
if(error){
res.send("Email could not sent due to error: "+error);
console.log('Error');
}else{
res.send("Email has been sent successfully");
console.log('mail sent');
}
});
in postman I got the error like that:
Email could not sent due to error:
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 g7sm64435626pfj.29 - gsmtp
Upvotes: 103
Views: 130133
Reputation: 6548
As mentioned in the comments and directly quoted from Google:
On May 30 2022, you may lose access to apps that are using less secure sign-in technology
So the bottom code will probably stop working with Gmail. The solution is to enable 2-Step Verification and generate Application password, then you can use the generated password to send emails using nodemailer.To do so you need to do the following:
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'YOUR-USERNAME',
pass: 'THE-GENERATED-APP-PASSWORD'
}
});
send();
async function send() {
const result = await transporter.sendMail({
from: 'YOUR-USERNAME',
to: 'RECEIVERS',
subject: 'Hello World',
text: 'Hello World'
});
console.log(JSON.stringify(result, null, 4));
}
I think that first you need to Allow less secure apps to access account setting in your Google account - by default this settings is off and you simply turn it on. Also you need to make sure that 2 factor authentication for the account is disabled. You can check how to disable it here.
Then I use the following script to send emails from a gmail account, also tested with yahoo and hotmail accounts.
const nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: '[email protected]',
pass: 'your.password'
}
});
let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Test',
text: 'Hello World!'
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('success');
});
If you put the previous code in send-email.js
for example, open terminal and write:
node send-email
You should see in the console - success, if the email was send successfully or the error message returned by nodemailer
Don't forget to first do the setting - Allow less secure apps to access account.
I hope this code will be useful for you. Good Luck!
Upvotes: 237
Reputation: 31
The setting App password one always works, if you are encountering issues or can't find the app password in the account, then go to your google account settings and then turn on 2-step verification and after setting up verification then click on 2-step verification and scroll down there find App passwords and then click create and name it as 'nodemailer' a random strings will appear copy it and paste in .env and remove spaces ofc.
Hope it helps : )
Upvotes: 0
Reputation: 2529
The directions above no longer work for me, there is no App Passwords
section on the Google Security page, even after enabling 2FA.
So:
1). Sign in to your google account 2). Make sure 2FA is enabled 3). Open this link: https://myaccount.google.com/apppasswords
And there you can enter name for your app and get the password you need.
Upvotes: 1
Reputation: 253
In May 2022, Google removed the less secure app option (https://myaccount.google.com/lesssecureapps).
because it's risky to connect third parties to Google.
So, Gmail has provided the app password. where you can use into an app (application)
Once you copied the app p/w, just register for smtp on https://www.smtper.net/ (Tick all options and don't leave any fields to fill up.) then click on "Submit" (check your Gmail account).
nodemailer code
const nodemailer = require("nodemailer");
const smtpTransport = require("nodemailer-smtp-transport");
var transporter = nodemailer.createTransport(
smtpTransport({
service: "gmail",
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: "[email protected]",
pass: "app password",
},
})
);
let mailOptions = {
from: "[email protected]",
to: "[email protected]",
subject: "Test",
text: "Hello World!",
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log("success");
});
For a better understanding, follow the two links given below.
Upvotes: 10
Reputation: 169
https://myaccount.google.com/lesssecureapps if it is OFF, turn it ON to enable lesssecureapps. (i had same issue turn it on resolve my issue)
Upvotes: 10
Reputation: 21
You have to allow Less-Secure-Apps to access account settings in your google account - by default, this setting is off and you can simply turn it on. Image example
Upvotes: 2
Reputation: 329
There are some account settings that are necessary for you to send through it using SMTP.
If you have two-step verification enabled on the account, you will need to use an application specific password (created in the Gmail account) in the device settings: Signing in using application-specific passwords
If not, please try this: sign into the Gmail account using a web browser at https://mail.google.com, then go to Settings > Accounts and Import > Other Google Account settings. Under Security, scroll down and enable access for less secure apps. This setting is required to enable SMTP, POP or IMAP access.
If there is still a problem, try clearing Captcha: visit https://accounts.google.com/DisplayUnlockCaptcha and sign in with the Gmail username and password. If necessary (it's usually not), enter the letters in the distorted picture then press Continue. This will allow ten minutes for the device to register as an approved connection. Note that you must use the account you are trying to add to the device - if the browser is already signed into another account, you must sign out first. Also, you must trigger the device to make a connection within ten minutes of pressing Continue.
Explanation from Google Support team
Upvotes: 1
Reputation: 43
I encountered the same problem, i solved it as follows:
Click on the Security Icon and you'll be taken to this page where you'll see Less secure apps section, click on it.
And you're not done yet, Not Go to the below link: https://myaccount.google.com/u/1/lesssecureapps
Now you'll see the switch. Enable it and try it'll definitely work.
Peace :)
Upvotes: 2
Reputation: 661
Make sure you are using the right port: ie port: 587
as of now.
Visit this link to allow Less secure App access: https://myaccount.google.com/lesssecureapps
Enable the recaptcha at this link: https://accounts.google.com/b/0/DisplayUnlockCaptcha
Wait for a few minutes and try to send an email.
Alternatively use sendgrid. It has a free version.
If you are not seeing the emails in your inbox, check the Spam folder.
Upvotes: 3
Reputation: 573
If you have enabled 2-factor authentication on your Google account you can't use your regular password to access Gmail programmatically. You need to generate an app-specific password and use that in place of your actual password.
Steps:
Your script will now look like this:
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'YOUR-GENERATED-APP-PASSWORD'
}
});
I hope this helps someone.
Upvotes: 49
Reputation: 397
You are using gmail service so your mail must be in gmail:
var transport = nodemailer.createTransport({
service:'gmail',
auth: {
user: "[email protected]",
pass: "qwerr@wee"
}
});
var mailOptions = {
from: "[email protected]",
to:'[email protected]',
subject: req.body.subject+"nodejs working ?",
text: "Hello world ?",
}
transport.sendMail(mailOptions, function(error, response){
if(error){
res.send("Email could not sent due to error: "+error);
console.log('Error');
}else{
res.send("Email has been sent successfully");
console.log('mail sent');
}
});
Upvotes: -1