Trying to send email in Java using gmail always results in username and password not accepted.

Posted by Thaeos on Stack Overflow See other posts from Stack Overflow or by Thaeos
Published on 2010-05-14T03:02:50Z Indexed on 2010/05/14 3:14 UTC
Read the original article Hit count: 439

Filed under:
|
|

When I call the send method (after setting studentAddress), I get this:

javax.mail.AuthenticationFailedException: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 y15sm906936wfd.10

I'm pretty sure the code is correct, and 100% positive that the username and password details I'm entering are correct. So is this something wrong with gmail or what?

This is my code:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail {

    private String host = "smtp.gmail.com";
    private String emailLogin = "[email protected]";
    private String pass = "xxx";
    private String studentAddress;
    private String to;
    private Properties props = System.getProperties();

    public SendEmail() {
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtps.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", emailLogin);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        to = "[email protected]";
    }

    public void setStudentAddress(String newAddress) {
        studentAddress = newAddress;
    }

    public void send() {
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(emailLogin));

            InternetAddress[] studentAddressList = {new InternetAddress(studentAddress)};
            message.setReplyTo(studentAddressList);
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("Test Email");
            message.setText("This is a test email!");
            Transport transport = session.getTransport("smtps");
            transport.connect(host, emailLogin, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (MessagingException me) {
            System.out.println("There has been an email error!");
            me.printStackTrace();           
        }
    }

}

Any ideas...

© Stack Overflow or respective owner

Related posts about java

Related posts about gmail