Alex Rivera | Logout

How to send html email to outlook from Java

Asked 2008-11-26T21:53:57.840
18

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;
}
Edit
Report

2 Answers

1
message.setContent(new String(sBuffer.toString().getBytes(), "iso-8859-1"), "text/html; charset=iso-8859-1");

Should solve your problem (removed \" characters).

answered 2010-09-14T06:08:19.233
0

workaroung solution solved outlook 2003: This message uses a character set that is not supported by the Internet Service. doesn't display correctly.

It could be due to the encoding. Most html pages use iso-8859-1 not cp-1252 try changing

For example, your code is:

message.setContent(sBuffer.toString(), "text/html");

Change this to:

message.setContent(new String(sBuffer.toString().getBytes(), "iso-8859-1"), "text/html; charset=\"iso-8859-1\"");

This throws a new checked exception : java.io.UnsupportedEncodingException so you need to declare it to be thrown or catch it. iso-8859-1 is supported so, the exception will never be thrown unless something gets corrupted with your rt.jar.

Regards, Javeed javeed.mca@gmail.com

answered 2009-03-10T15:15:57.473

Your Answer