0

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.

Community
  • 1
  • 1
  • Have you tried to use the same configuration values in a normal mail client such as Thunderbird for example, to verify that they are valid? – mjn Jun 13 '16 at 15:54
  • Possible duplicate of [JavaMail API to iMail -- java.net.SocketException: Permission denied: connect](http://stackoverflow.com/questions/12901475/javamail-api-to-imail-java-net-socketexception-permission-denied-connect) – mjn Jun 13 '16 at 15:55

2 Answers2

0

I found a similar problem here.

Try to see if the problem is relate to an anti virus or a firewall blocking your requests.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

Switch off you firewall and find in Google some security certificate that missed in you jar/lib/security I have already faced this then I have to make use phpmailer lib in php and make on API use anywhere in your code so you can try php.

vivek k
  • 61
  • 8