theAnonymous
theAnonymous

Reputation: 1804

Java Mail not working suddenly

Java mail code was working, now it suddenly doesn't work at all!

What changed? What's wrong with the code?

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>
}

The following are the prints:

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)

What's wrong with the code and how do I fix it?

Upvotes: 1

Views: 904

Answers (1)

Manos Pasgiannis
Manos Pasgiannis

Reputation: 1783

Port 587 does not work with hotmail. You have to use 25 or 465. Also change your host to smtp.live.com

Have a look here

Upvotes: 1

Related Questions