2

提前感谢您对此的任何帮助。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);
    }

谢谢!

4

1 回答 1

2

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

感谢大家。

于 2013-03-27T21:03:03.510 回答