javax.mail.MessagingException:无法连接到SMTP主机:172.16.100.185,port:25;

时间:2016-02-16 13:20:46

标签: java email javamail sendmail

我编写了一个使用Java API发送电子邮件的简单程序。下面是代码。以下是我得到的错误消息。

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

错误:

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

仅供参考,我也尝试过以下方法。

1)我尝试使用端口号:25,465和587.

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

2)检查主机

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

3)在主机中,我检查了SMTP服务器是否正在侦听端口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)能够从Linux终端发送电子邮件

我已经成功地使用以下命令从Linux框中向我的电子邮件地址发送电子邮件,其中SMPT服务器已启动并运行。 mail -s"来自44,sudo access" anand.rajendran @ *****。净

2 个答案:

答案 0 :(得分:0)

问题在于安装了SMPT服务器的配置文件。我将我的java应用程序指向另一台运行另一台SMTP服务器的机器。它工作正常。 :) 感谢所有的时间和精力!!欢呼声

答案 1 :(得分:-1)

发现一些引用表明这与Java-7相关:
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 +权限+拒绝%3A +连接。' +异常

这些页面提供了解决方法。