Question: Has anyone ever successfully sent an email to a hotmail account through JavaMail from an SMTP server? If so could you put up the code that worked?
I can send emails to gmail and yahoo accounts using my JavaMail code but I can not send any emails to hotmail accounts. If I use my phone or another email client and use the same SMTP server as my JavaMail code then I can indeed send emails to hotmail accounts. This leads me to believe JavaMail leaves out a flag that hotmail seems to think is important. Using the Apache Commons JavaMail implementation produces the same results.
try{
Email email = new SimpleEmail();
email.setSmtpPort(Integer.parseInt(port));
email.setAuthenticator(new DefaultAuthenticator(from, MyUtilities.getSystemPWD(from)));
email.setDebug(true);
email.setHostName(host);
email.setFrom(from);
email.setSubject(subject);
email.setMsg("test");
email.addTo(to);
email.setStartTLSRequired(true);
email.send();
} catch(Exception ex){
MyLogger.log("MyUtilities.sendEmail: Messaging error",ex);
Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", ex);
}
Answer: There is an accepted answer below but the underlying cause of the problem is that Hotmail requires extra authentication headers (SPF & DKIM) to prove the domain name of your from address is associated with the SMTP server. Using an intermediary SMTP server, like sendgrid, can solve the problem as they will do it for you automatically..at a cost.
You can also attempt to add the needed SPF and DKIM headers yourself.
You can try using sendgrid. I just tested it out and if you use legitimate email addresse for the from, it seems to work.
If you have code that works for sending to any other internet email address, it should work for sending to Hotmail too.
If you don't have code that works in general, see the JavaMail sample code and the JavaMail FAQ.
If you're trying to use Hotmail as your SMTP server, see the JavaMail FAQ.