0

I have written a simple program to send an email using Java API. Below is the code. following that is the error message I get.

        String host = "172.16.100.185";
        final String user = "anand.rajendran@*****.net";

        String to = "anand.rajendran@*****.net";

        // Get the session object
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        //props.put("mail.smtp.port", "587");


        Session session = Session.getDefaultInstance(props,null);

        // Compose the message
        try {
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject("My subject");
            message.setText("This is simple program of sending email using JavaMail API");

            // send the message
            Transport.send(message);

        } catch (MessagingException e) {
            e.printStackTrace();
        }

Error:

javax.mail.MessagingException: Could not connect to SMTP host: 172.16.100.185, port: 25;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)
    at javax.mail.Service.connect(Service.java:105)
    at javax.mail.Transport.send0(Transport.java:168)
    at javax.mail.Transport.send(Transport.java:98)
    at com.photon.bigdata.heartbeat.service.impl.HeartBeatServiceImpl.sendFailureReportEmail(HeartBeatServiceImpl.java:167)
    at com.photon.bigdata.heartbeat.controller.HeartBeatController.sendReport(HeartBeatController.java:62)
    at com.photon.bigdata.heartbeat.controller.HeartBeatController.main(HeartBeatController.java:33)
Caused by: java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
    ... 9 more

FYI, I tried the following as well.

1) I tried with port numbers: 25, 465 and 587.

props.put("mail.smtp.port", "465");
props.put("mail.smtp.port", "587");

2) Checked the status of sendMail in the host

[root@stormcluster1 ~]# service sendmail status
sendmail (pid  13898) is running...
sm-client (pid  13910) is running...

3) In the host, I checked if SMTP server is listening at port 25

[root@stormcluster1 ~]# lsof -i :25
COMMAND    PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
sendmail 13898 root    4u  IPv4 2172898      0t0  TCP localhost:smtp (LISTEN)

4) Was able to send email from the Linux terminal

I was successfully able to send an email to my email address from the linux box where the SMPT server is up and running using the following command. mail -s "from 44 with sudo access" anand.rajendran@*****.net

Andy
  • 5,433
  • 6
  • 31
  • 38
  • The "permission denied" would indicate the firewall on the machine running the java code prevents your program from making network connections. Are you running that on windows? – Kenney Feb 16 '16 at 13:26
  • Yes, I am running the java code on windows 8.1 pro. – Andy Feb 16 '16 at 13:33
  • [Refer](http://stackoverflow.com/questions/11029444/javax-mail-messagingexception-could-not-connect-to-smtp-host) – thar45 Feb 16 '16 at 13:36
  • @GK27: I tried that option already, but it didn't help either. – Andy Feb 16 '16 at 13:41
  • Okies but i dont see any similar code in that :) – thar45 Feb 16 '16 at 13:42
  • I think you may need to [allow Java through the firewall](http://windows.microsoft.com/lo-la/windows-vista/allow-a-program-to-communicate-through-windows-firewall). – Kenney Feb 16 '16 at 13:57
  • Have you tried [telnet from the client machine to the server](http://www.oracle.com/technetwork/java/javamail/faq/index.html#condebug)? Is your code running in an application server with a security manager? You may need to [configure the Java security permissions needed by your application](http://www.oracle.com/technetwork/java/javamail/faq/index.html#securityManager). – Bill Shannon Feb 16 '16 at 18:53

2 Answers2

0

the issue was with the configuration file of the SMPT server installed. I pointed my java application to a different machine where another SMTP server was running. It worked fine. :) thanks to all for your time and effort!! cheers

Andy
  • 5,433
  • 6
  • 31
  • 38
-1

Found some references suggesting that this is Java-7 related:
JavaMail API to iMail -- java.net.SocketException: Permission denied: connect


https://confluence.atlassian.com/display/STASHKB/Mail+Server+Connection+Failed+With+'java.net.SocketException%3A+Permission+denied%3A+connect.'+Exception

Those pages guive workarounds.

Community
  • 1
  • 1
ernest_k
  • 44,416
  • 5
  • 53
  • 99