views:

147

answers:

3

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...

+2  A: 

This is something I found on the net and it works fine for me.

Arun P Johny
Actually my code works perfectly, the issue was the gmail account being used. I used my personal gmail one to test and it worked perfectly, previously I was using one I had only just set up and it wasn't working, but now it is. I guess it took a bit of time for the settings to apply..
Thaeos
A: 

Was an issue to do with gmail, not the code!

Thaeos
A: 

there are certain restrictions for a gmail account to be used in a program as yours. One among that is the account should be some days old and i guess its 1 days old.

rgksugan