I'm using java mail API and it's working fine on a local machine but when I am deploying my web-app on a server, I'm getting an exception saying javax.mail.MessagingException: can't determine local email address.
this is my code
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
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");
try {
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]", "fromEmailPass");
}
});
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setSubject("sub");
message.setText("msg");
Transport.send(message);
System.out.println("mail sent successfully");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
Below given is the exception this code is throwing.
javax.mail.MessagingException: can't determine local email address
From this question: Failed messages: javax.mail.MessagingException: can't determine local email address
The author seems to suggest that this exception is thrown when the from address is not set.
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
message.setFrom("[email protected]"); // <-- add 'from' email address
message.setSubject("sub");
message.setText("msg");
Transport.send(message);