如何解决:将电子邮件发送到以下服务器失败SocketException:权限被拒绝:connect [英] How to solve: Sending the email to the following server failed SocketException: Permission denied: connect

查看:798
本文介绍了如何解决:将电子邮件发送到以下服务器失败SocketException:权限被拒绝:connect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java以编程方式发送邮件时遇到问题.我从网络团队确认,使用防火墙阻止了通过Java发送邮件. 但能够在我的Windows命令提示符下从telnet获取响应.请在下面找到详细信息.

I am facing issue while sending mails programmatically using Java. I confirmed from the network team that sending mails through Java is blocked using firewall. but am able to get the response from telnet in my windows command prompt. Please find the details below.

用于使用Javax Mail发送邮件的代码:

Code used to send mail using Javax Mail:

public static void main(String[] args) throws MessagingException {
        String host = "mailhost.xxx";
        String to = "abc@xyz.edu";
        String from = "cde@xyz.edu";
        String subject = "test";
        String messageText = "body test";

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", "25");

        // If using authentication, otherwise comment out
        props.put("mail.smtp.auth", "true");

        // Gmail requires TLS, your server may not
        props.put("mail.smtp.starttls.enable", "true");

        // Session mailSession = Session.getDefaultInstance(props, null);

        Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username", "password");
            }
        });

        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(from));
        InternetAddress[] address = { new InternetAddress(to) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(messageText);

        Transport transport = mailSession.getTransport("smtp");

        // connect with authentication
        // transport.connect(host,"myUsername" , "myPassword");

        // connect without authentication
        transport.connect();
        transport.sendMessage(msg, address);

        transport.close();

        System.out.println("Mail was sent to " + to);
        System.out.println(" from " + from);
        System.out.println(" using host " + host + ".");

    }

使用Telnet能够在命令提示符下发送邮件: telnet mailhost.xxx 25 收到如下响应:

Using Telnet am able to send mails in command prompt: telnet mailhost.xxx 25 am getting the response like:

220 mailhost Microsoft ESMTP MAIL Service ready at Tue, 29 Mar 201
6 23:34:36 -0700

在谷歌搜索之后,我还尝试如下在Eclipse中设置JVM参数,如下所示,仍然没有运气得到异常

After googling I also tried setting JVM argument in Eclipse as below, no luck still getting the exception as shown below

-Djava.net.preferIPv4Stack=true

例外:

Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : mailhost.xxx:25
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
    at org.apache.commons.mail.Email.send(Email.java:1267)
    at edu.xxx.pageobject.appcenter.util.ReportGenerator.main(ReportGenerator.java:328)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mailhost xxx, 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:297)
    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 org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
    ... 2 more
Caused by: java.net.SocketException: Permission denied: connect
    at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    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

让我知道解决上述问题的任何可行解决方案.

Let me know any possible solution to resolve the above issue.

推荐答案

为上述问题发布替代解决方案: 当我们使用Exchange帐户时,我们能够使用EWS(Exchange Web服务)API以编程方式发送邮件. EWS入门指南链接

Posting an alternate solution for my above issue: As we were using Exchange accounts we were able to send the mails programmatically using EWS (Exchange Web Services) API. EWS Getting Started Guide Link

这篇关于如何解决:将电子邮件发送到以下服务器失败SocketException:权限被拒绝:connect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆