Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send html email to outlook from Java

I'm trying to send an email in html format using JavaMail but it always seems to only display as a text email in Outlook.

Here is my code:

try 
{
    Properties props = System.getProperties();
    props.put("mail.smtp.host", mailserver);
    props.put("mail.smtp.from", fromEmail);
    props.put("mail.smtp.auth", authentication);
    props.put("mail.smtp.port", port);
    Session session = Session.getDefaultInstance(props, null);      

    // -- Create a new message --
    MimeMessage message = new MimeMessage(session);

    // -- Set the FROM and TO fields --
    message.setFrom(new InternetAddress(fromEmail, displayName));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

    MimeMultipart content = new MimeMultipart();
    MimeBodyPart text = new MimeBodyPart();
    MimeBodyPart html = new MimeBodyPart();

    text.setText(textBody);
    text.setHeader("MIME-Version" , "1.0" );
    text.setHeader("Content-Type" , text.getContentType() );

    html.setContent(htmlBody, "text/html");
    html.setHeader("MIME-Version" , "1.0" );
    html.setHeader("Content-Type" , html.getContentType() );

    content.addBodyPart(text);
    content.addBodyPart(html);

    message.setContent( content );
    message.setHeader("MIME-Version" , "1.0" );
    message.setHeader("Content-Type" , content.getContentType() );
    message.setHeader("X-Mailer", "My own custom mailer");

    // -- Set the subject --
    message.setSubject(subject);

    // -- Set some other header information --
    message.setSentDate(new Date());

    // INFO: only SMTP protocol is supported for now...
    Transport transport = session.getTransport("smtp");
    transport.connect(mailserver, username, password);
    message.saveChanges();

    // -- Send the message --
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

    return true;

} catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
    throw e;
}       

Any ideas why the html version of the email won't display in Outlook?

like image 829
Stephane Grenier Avatar asked Nov 26 '08 21:11

Stephane Grenier


1 Answers

After a lot of investigation, I've been able to make some significant progress.

Firstly, instead of using JavaMail directly, I recommend using the Jakarta Commons Email library. This really simplifies the issue a lot!

The code is now:

HtmlEmail email = new HtmlEmail();

email.setHostName(mailserver);
email.setAuthentication(username, password);
email.setSmtpPort(port);
email.setFrom(fromEmail);
email.addTo(to);
email.setSubject(subject);

email.setTextMsg(textBody);
email.setHtmlMsg(htmlBody);

email.setDebug(true);

email.send();

Talk about simple.

However, there is still an issue. The html version of the email works great in Gmail, Hotmail, etc. But it still won't correctly display in Outlook. It always wants to display the text version and I'm not sure why. I suspect it's a setting in Outlook, but I can't find it...

like image 176
Stephane Grenier Avatar answered Sep 17 '22 23:09

Stephane Grenier