Java Mail突然无法正常工作

时间:2016-02-19 07:10:54

标签: java javamail

Java邮件代码正在运行,现在它根本不起作用!

改变了什么?代码有什么问题?

private static void testRaw(){
    //<editor-fold defaultstate="collapsed" desc="code">
    String host = "smtp.hotmail.com";
    int port = 587;
    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);
    properties.put("mail.smtp.port", 587);
    properties.setProperty("mail.transport.protocol", smtp);
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(properties,
            new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(from, password);
                    }
            }
    );
    session.setDebug(true);
    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("test title");
        message.setText("test message");

        Transport transport = session.getTransport("smtp");
        transport.connect(host, port, from, password);
        transport.send(message);

        System.out.println("Send OK");
    } catch (Exception e) {
        e.printStackTrace();
    }
    //</editor-fold>
}

以下是版画:

DEBUG: setDebug: JavaMail version 1.5.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.live.com", port 587, isSSL true
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.live.com, 587; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
    at javax.mail.Service.connect(Service.java:366)
    at email.v2.Test.testRaw(Test.java:135)
    at email.v2.Test.main(Test.java:41)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)

代码有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

端口587不适用于hotmail。你必须使用25或465。 同时将您的主机更改为smtp.live.com

看看here