0

I am trying to send an email via my custom domain that I have setup using live domains: https://domains.live.com. The email itself works and I can login at outlook.com with my custom domain username and password and send and receive mail. What I am trying to do is send emails via Java and JMail here is my code (from here: http://www.oracle.com/technetwork/java/javamail/faq-135477.html#outlook) :

public static void sendEmail() throws MessagingException{
    System.out.println("Sending email!");

    String host = "smtp.live.com"; // using this or smtp-mail.outlook.com has the same outcome.
    String username = "username";
    String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");

    Session session = Session.getInstance(props);
    MimeMessage msg = new MimeMessage(session);
    msg.setSubject("Test Subject");
    msg.setFrom(new InternetAddress("fromemail"));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress("toemail")); 

    Transport t = session.getTransport("smtp");

    try {
        t.connect(host, username, password);
        t.sendMessage(msg, msg.getAllRecipients());
    } finally {
        t.close();
    }
}

I substitute in my username and password and I get the following message:

Sending email!
javax.mail.MessagingException: Exception reading response;


nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2210)
    at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1222)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2188)
    ... 36 more

Note: the from email is the same as my username!

What am I missing? Thanks!

EDIT: I have changed my props to:

Properties props = new Properties();
props.put("mail.smtp.auth", "true");  // If you need to authenticate
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");

Same error still showing..

EDIT: I fixed it. I turned on debug using session.setDebug(true) and could see I didnt have an email body. So i added msg.setText("Content") and it all worked!

user1625233
  • 253
  • 3
  • 9

1 Answers1

0

smtp.live.com does not listen on port 25 which is the default port. It appears that they require TLS so you will have to do that in javamail as well. The default TLS port is 587 and I've confirmed that the server you are trying to connect to listens on that port. This article shows you how to set the port:

Using JavaMail with TLS

Community
  • 1
  • 1
Zeki
  • 5,107
  • 1
  • 20
  • 27