Java邮件:无法连接到SMTP主机:...,port:...,java.net.ConnectException:操作超时

时间:2013-03-06 22:34:48

标签: javamail java-7

提前感谢您对此的任何帮助。 Java Mail总是对我有用,但现在它正在抛出这个恼人的异常:

//例外:

run:
DEBUG: setDebug: JavaMail version 1.4.6
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xxxxxx", port 25, isSSL false
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: xxxxxx, port: 25;
  nested exception is:
    java.net.ConnectException: Operation timed out
    at vesso.mail.TestMail.sendMail(TestMail.java:79)
    at vesso.mail.TestMail.main(TestMail.java:26)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: mail.mentorlicitacao.com.br, port: 25;
  nested exception is:
    java.net.ConnectException: Operation timed out
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1962)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at vesso.mail.TestMail.sendMail(TestMail.java:71)
    ... 1 more
Caused by: java.net.ConnectException: Operation timed out
    at java.net.PlainSocketImpl.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.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    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:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1928)
    ... 4 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 minute 19 seconds)

我尝试连接Mail.app(使用MacOS),并且运行正常。

//代码:

    final String host = "xxxx";
    final int port = 25;
    final String username = "xxxx";
    final String password = "xxxx";

    Properties props = new Properties();

    props.put("mail.smtp.auth", "true");

    Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
    session.setDebug(true);

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(username));
        message.setSubject("Teste");
        message.setText("Bla, Bla, Bla!");

        Transport transport = session.getTransport("smtp");
        transport.connect (host, port, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();  


        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

通过将端口从25更改为587解决了问题。我得出的结论是我的ISP阻止此端口。

谢谢大家。