Com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required (Java Mail)
So, I'm trying to get Java Mail to work, and because other answers I've seen on this site don't work, I have to assume that something has changed in the last year or so. So I apoligies, if this question seems to be DUPLICATE, I can't figure out why it doesn't work. Below is my code:
try{
Properties property = new Properties();
property.setProperty("mail.smtp.host", "smtp.gmail.com");
property.setProperty("mail.smtp.starttls.enable", "true");
//property.setProperty("mail.smpt.port", "25");
property.setProperty("mail.smtp.user", "myEmail@gmail.com");
property.setProperty("mail.smtp.auth", "true");
System.out.println("Mail Check 1");
Session session = Session.getDefaultInstance(property);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("myEmail@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("myEmail@gmail.com"));
System.out.println("Mail Check 2");
message.setSubject("Oil Error Report");
message.setText(emailMessage);
System.out.println("Mail Check 3");
Transport transport = session.getTransport("smtps");
transport.connect("smtp.gmail.com",465,"myEmail@gmail.com","myPassword");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Mail Sent");
}catch(Exception ex){
System.out.println("Mail fail");
System.out.println(ex);
}
I go back to Mail Check 3, then I get the following exception:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 u3sm10254140ioi.27 - gsmtp
It doesn't make sense because I have mail.smtp.auth for true. I looked at past answers and I tried to get it to work while before posting. I guess it's simple. Any help would be greatly appreciated! Thank!
source to share
public class EmailSender {
public void sendEmail(String emailMessage){
try{
final String fromEmail = ""; //requires valid gmail id
final String password = ""; // correct password for gmail id
final String toEmail = ""; // can be any email id
System.out.println("TLSEmail Start");
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host
props.put("mail.smtp.port", "587"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
System.out.println("Mail Check 2");
message.setSubject("Oil Error Report");
message.setText(emailMessage);
System.out.println("Mail Check 3");
Transport.send(message);
System.out.println("Mail Sent");
}catch(Exception ex){
System.out.println("Mail fail");
System.out.println(ex);
}
}
}
This is the required code that made it work! It's November 2014 and it currently works for gmail! Hope this code helps you save some time, took most of mine! Along with this code, you must change your email address to allow the use of such email messages. You will receive an email from google on your first try and it will guide you through changing the setting.
Thanks and good luck!
source to share
Have you looked at the URL? Did you follow all the steps there? What happened?
You want to fix these common mistakes .
Note that since you are using the "smtps" protocol, the none properties of the mail.smtp properties apply. *. But that's ok, because you don't need them anyway due to the way the connect method is called.
source to share