I have been trying to send an email using an smtp server through javamail api. I get the following error:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.host.com, port: 587;
nested exception is:java.net.SocketException: Permission denied: connect
Here's how I create the Properties
and Session
object for the mail:
private Session getSession() {
Authenticator authenticator = new Authenticator();
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
properties.setProperty("mail.smtp.auth", "false");
properties.setProperty("mail.smtp.starttls.enable", "false");
properties.setProperty("mail.smtp.host", "smtp.hostname.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.ssl.trust", "smtp.hostname.com");
properties.setProperty("java.net.preferIPv4Stack" , "true");
return Session.getInstance(properties, authenticator);
}
private class Authenticator extends javax.mail.Authenticator {
private PasswordAuthentication authentication;
public Authenticator() {
String username = "username";
String password = "password";
authentication = new PasswordAuthentication(username, password);
}
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
}
I have tried disabling the authentication as suggested by some posts and also put in resolutions for IPv4 preference according to this post: JavaMail API to iMail -- java.net.SocketException: Permission denied: connect
but I still get the same error. Is there any other way this issue might be resolved?
Thanks.