I’m using java mail to send automatic mail when a button is pressed.
I work on android studio, and i get the error javax.mail.AuthenticationFailedException
and I can't find why.
- I tried changing port to 25 - 587 and 465
- Id are correct
- tried on emulator and on real device
- my gmail account is open to low secure application
here is the code :
Properties props = new Properties();
props.put("mail.smtp.host" , "smtp.gmail.com");
props.put("mail.stmp.user" , "horiond@gmail.com");
//TLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.password", "xxxxx");
//SSL
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance( props , null);
String to = "horiond@gmail.com";
String from = "horiond@gmail.com";
String subject = "Testing...";
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipient(Message.RecipientType.TO,
new InternetAddress(to));
msg.setSubject(subject);
msg.setText("Working fine..!");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com" , 465 , "horiov@gmail.com", "xxxx");
transport.send(msg);
System.out.println("fine!!");
}
catch(Exception exc) {
System.out.println(exc);
}