I am trying out an email app on android.I take the username and password first
then on the next activity i take the recipiend address subject and msg and send the email.
I get the error JavaX authentication failure.Here is my Mail authentication and sending code.OnClick function of the send button calls this class
public class MailHandler {
final String username;
final String password;
public MailHandler(String username,String password){
this.username=username;
this.password=password;
}
public void sendMail(String Sub,String msg,String sender,String cc,String to) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(Sub);
message.setText(msg);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
Your code works. I copied it compiled and ran it using my gmail credentials. Are you sure you are using the correct gmail credentials for sending mail?
// After running I checked my work email and saw the message.
If I use an invalid password I get the following statcktrace. Is this what you are seeing