javax.mail.MessagingException: Could not connect to SMTP host: 172.16.100.185, port: 25;
<p>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.</p>

<pre><code>        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();
        }
</code></pre>

<p><strong><em>Error:</em></strong></p>

<pre><code>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
</code></pre>

<p>FYI, I tried the following as well. </p>

<h2>1) I tried with port numbers: 25, 465 and 587.</h2>

<pre><code>props.put("mail.smtp.port", "465");
props.put("mail.smtp.port", "587");
</code></pre>

<h2>2) Checked the status of sendMail in the host</h2>

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

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

<pre><code>[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)
</code></pre>

<h2>4) Was able to send email from the Linux terminal</h2>

<p>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</p>