I\'m not familiar with this function to send mail in java. I\'m getting an error while sending email to reset a password. Hope you can give me a solution.
Below is m
The solution that works for me has two steps.
First step
package com.student.mail;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author jorge santos
*/
public class GoogleMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("conaderindicadores@gmail.com","Silueta95#");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("conaderindicadores@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("netneillip@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Test Mail");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Enable the gmail security
https://myaccount.google.com/u/2/lesssecureapps?pli=1&pageId=none
Change this (set less secure app): https://www.google.com/settings/security/lesssecureapps
If you are logging in to your gmail account from a new application or device, Google might be blocking that device. Try following these steps:
To protect your account, Google might make it harder to sign in to your account if we suspect it isn’t you. For example, Google might ask for additional information besides your username and password if you are traveling or if you try to sign in to your account from a new device.
Go to https://g.co/allowaccess from a different device you have previously used to access your Google account and follow the instructions. Try signing in again from the blocked app.
I have been getting the same error for long time.
When i changed session debug to true
Session session = Session.getDefaultInstance(props, new GMailAuthenticator("xxxxx@gmail.com", "xxxxx"));
session.setDebug(true);
I got help url https://support.google.com/mail/answer/78754 from console along with javax.mail.AuthenticationFailedException.
From the steps in the link, I followed each steps. When I changed my password with mix of letters, numbers, and symbols to be my surprise the email was generated without authentication exception.
Note: My old password was more less secure.
By default Gmail account is highly secured. When we use gmail smtp from non gmail tool, email is blocked. To test in our local environment, make your gmail account less secure as
You should change the port to 587
, I tested your code and it's working fine
If error still happens, please change session variable to code below:
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});