Jared Smith Jared Smith - 2 months ago 22
Java Question

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 of this site are not working, I have to assume somethings have changed in the past year or so. So I apoligies if this question seems like a DUPLICATE I can't figure out why it is not working. The following 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 get up 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


Which doesn't make sense because I have mail.smtp.auth set to true. I have looked at past answers and I have tried to get it to work for while before posting. I'm assuming it's something simple.. Any help would be much appreciated! Thanks!

Answer
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 code needed that got it working! It is November 2014 and this currently works for gmail! I hope this code will help save someone time, took much of mine! Along with this code you have to change your email setting to allow emails such as these to be allowed. You will get an email from google the first time you attempt, and it will walk you through on changing the setting.

Thanks and good luck!

Comments