0

First, let me appoligize if this is a duplicate question. Over the last couple of calendar months, I have been attempting to send email, using JavaMail; via my Hotmail account. I have used the numerious tips and code snipits that have been posted to this site in the past on this topic; however, I am still getting a java.net.ConnectException: Connection refused... when I execute the Transport.connect method

Here's my code

String fromUserName = "testEmailAddress@hotmail.com";
String fromEMailAddress = "My Email Test <testEmailAddress@hotmail.com>";
String fromEmailPassword = "testEmailPassword";
String emailServerName = "smtp.live.com";
String emailServerPort = "587";
String toEMailAddress = "<TestDestinationEmailAddress@gmail.com>";      

Properties emailProps = System.getProperties();
emailProps.put("mail.smtps.host", emailServerName);
emailProps.put("mail.smtps.auth", "true");
emailProps.put("mail.transport.protocol", "smtps");
emailProps.put("mail.smtps.starttls.enable", "true");
emailProps.put("mail.smtps.ssl.enable","true");
emailProps.put("mail.smtps.port", emailServerPort);
emailProps.put("mail.debug", "true");

Authenticator localAuthenticator = new SMTPAuthenticator(fromUserName, fromEmailPassword);

Session emailSession = Session.getInstance(emailProps, localAuthenticator);

try {

    SMTPTransport emTransport = (SMTPTransport) emailSession.getTransport("smtps"); 
    emTransport.connect(emailServerName, Integer.parseInt(emailServerPort), fromUserName, fromEmailPassword);
    System.out.println("Ok, we connected ok.");

    MimeMessage emailMsg = new MimeMessage(emailSession);           
    emailMsg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEMailAddress));
    emailMsg.setFrom(new InternetAddress(fromEMailAddress));
    emailMsg.setSubject("Automated Notification Number 1");
    emailMsg.setContent(getHtmlContent(), "text/html");
    emTransport.sendMessage(emailMsg, emailMsg.getAllRecipients());
    System.out.println("Sent Message#: 1");

} catch (Exception e) {
    e.printStackTrace();
}

And here's the exception...

DEBUG JavaMail version 1.4.5
DEBUG successfully loaded resource: /META-INF/javamail.default.providers
DEBUG SMTP useEhlo true, useAuth true
DEBUG SMTP trying to connect to host "smtp.live.com", port 587, isSSL true
javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at com.acf.TestingClasses.EmailSendingGames.sendMail(Unknown Source)
    at com.acf.TestingClasses.EmailSendingGames.main(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect



A couple of things:

  • I have tried both "smtp" and "smtps"...it doesn't seem to matter
  • I have tried ports: 25, 465, 587, and 995...still refuses the connection
  • I have tried the code on many computers with the same results.
  • Cut-and-pasted the code from the JavaMail demo code but stll get the error.
  • The code works for yahoo, at&t, gmail, and others...but not Hotmail!
AstarAndy
  • 121
  • 1
  • 8
  • Someone ran into some issues with teh JavaMail + Hotmail combo: http://stackoverflow.com/questions/9086420/using-javamail-to-send-from-hotmail – Fritz Jul 25 '12 at 22:28

2 Answers2

2

I removed all the "socketFactory" stuff, as in Using javamail to send from hotmail? Then it worked fine...

Below is the function:

private Properties _setProperties() {   
    Properties props = new Properties();   

    props.put("mail.smtp.host", _host);   

    if(_debuggable) {   
      props.put("mail.debug", "true");   
    }   

    if(_auth) {   
      props.put("mail.smtp.auth", "true");   
    }

    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", _host);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", _port);

    return props;
  } 
Community
  • 1
  • 1
0

Presumably you've already read this JavaMail FAQ entry.

Did you also try the debugging tips here?

From the debug output you posted, it looks like you have a firewall or something that's preventing you from connecting to that site.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40