I am using JAVAMail API to send email. But Authentication fails.
Here is the sample input
private String to = "[email protected]";
private String from = "[email protected]";
private String username = "juniad_rocku";
private String password = "myactualpassword";
private String subject = "My Subject";
private String body = "Please see the attached file";
and code is
private void emailFile(){
String host = "relay.jangosmtp.net";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
// Get the Session object.
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(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(this.subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(this.body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
for(String filePath : UniversityForm.allAttachedFilesPath) {
if(filePath != null && !(filePath.equals("")))
this.addAttachment(multipart, filePath);
}
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
ERROR
java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.8 Authentication credentials invalid
I have given good username, password ... I don't know why Authentication Failed. I don't know about host
. Where am I mistaken? Please help.
Yahoo Mail Server is "smtp.mail.yahoo.com" with the port number 465. You forget to add the "mail.password" property. You need add the following additional properties: