0

I am trying to write a simple Java program to send emails from my hotmail account using JavaMail API. Here is my code :

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class HotMailSend {
public static void main(String args[])
{
    final String username = HOTMAIL.username;
    final String password = HOTMAIL.password;

    Properties props = new Properties();
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty("mail.smtp.host", "smtp.live.com");
    props.setProperty("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(HOTMAIL.username));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(GMAIL.username));
        message.setSubject("Testing Subject");
        message.setText("Hey Buddy..!!!,"
            + "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

}

And here is the error I am getting :

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: java.net.SocketException: Connection closed by remote host at HotMailSend.main(HotMailSend.java:45) Caused by: javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: java.net.SocketException: Connection closed by remote host at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2163) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2150) at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1220) at javax.mail.Transport.send0(Transport.java:197) at javax.mail.Transport.send(Transport.java:124) at HotMailSend.main(HotMailSend.java:40) Caused by: java.net.SocketException: Connection closed by remote host at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkWrite(SSLSocketImpl.java:1307) at com.sun.net.ssl.internal.ssl.AppOutputStream.write(AppOutputStream.java:43) at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114) at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65) at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123) at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:2161) ... 5 more

yashdosi
  • 1,186
  • 1
  • 18
  • 40
  • I had this problem before too. I given up on trying to find a solution but my guess was that hotmail do not allow this kind of connections for security reasons – Adel Boutros Jul 05 '12 at 15:16
  • @AdelBoutros - I tried the same code again!! It worked without any problems...then I could not believe it actually happened so I ran it again...and again got the same error... now any comments for this weird phenomena?? As a proof it worked I have the main in my gmail inbox!!! – yashdosi Jul 05 '12 at 15:20
  • Possibly you have a firewall or antivirus product that's intercepting your connections and causing this problem. – Bill Shannon Jul 05 '12 at 17:06
  • -in case of a firewall I dont think it should work even for a single time!! (read the comments above) – yashdosi Jul 06 '12 at 06:07
  • also i verified....there is no firewall problem... – yashdosi Jul 06 '12 at 09:36
  • What does the protocol trace show when it fails? – Bill Shannon Jul 06 '12 at 17:28
  • forgive me for my ignorance...but how do i see my protocol trace...?? and what exactly is protocol trace..?? – yashdosi Jul 06 '12 at 17:50
  • 1
    http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug – Bill Shannon Jul 06 '12 at 21:50

2 Answers2

1

I've used SMTP/Javamail with Hotmail before, so it definitely works. Were you sending a lot of emails in a short time? (I think they may block your mail or send it to the junk folder when you do that as a spam prevention measure.)

Taraz
  • 1,242
  • 13
  • 13
0

Check the e-mail that you're currently using if it arrived some message from microsoft.

With me occurred this problem, and when i looked for it, I had one e-mail that had the following subject: Verify your account to do more with Outlook.com.

They asked me to confirm that i'm really a human so I could continue to send automatic messages. They sent me a message to my cellphone and I managed to keep sending e-mails with Java Mail.

Henrique
  • 11
  • 4