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

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

asked Nov 26, 2008 at 21:53
2
  • I updated my answer with a new idea. Commented Nov 27, 2008 at 16:55
  • @erickson: the upvote on your answer is from me ;) Commented May 18, 2010 at 15:06

8 Answers 8

16

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...

answered Nov 27, 2008 at 20:33
5

In addition to removing the html.setHeader("Content-Type", html.getContentType()) call as suggest already, I'd replace the line:

MimeMultipart content = new MimeMultipart();

…with:

MimeMultipart content = new MimeMultiPart("alternative");

…and removing the line:

message.setHeader("Content-Type" , content.getContentType() );

The default MimeMultiPart constructor could be causing problems with a "multipart/mixed" content-type.

When using multipart/alternative, the alternatives are ordered by how faithful they are to the original, with the best rendition last. However, clients usually give users an option to display plain text, even when HTML is present. Are you sure that this option is not enabled in Outlook? How do other user agents, like Thunderbird, or GMail, treat your messages?

Also, ensure that the HTML is well-formed. I'd validate the HTML content with the W3 validation service, and possibly save it into a file and view it with different versions of IE too. Maybe there's a flaw there causing Outlook to fall back to plain text.

answered Nov 26, 2008 at 22:42
1
  • I've tried that too with no luck. I also tried the other modes even though I knew they wouldn't work (mixed, etc.). For Outlook, it still only wants to display the text version, never the html body. And gmail display the html, but html as text, not as an html rendered page. Weird... Commented Nov 27, 2008 at 16:32
3
html.setContent(htmlBody, "text/html");
html.setHeader("MIME-Version" , "1.0" );
html.setHeader("Content-Type" , html.getContentType() );

setContent and setHeader("Content-Type", String) do the same thing - is it possible that html.getContentType() is returning something other than text/html?

Expanding based on comment and @PhilLho & @erickson's answer (geez, I must type slowly), use:

MimeMultipart content = new MimeMultipart("alternative")
answered Nov 26, 2008 at 22:03
1
  • That is indeed one issue. It's returning "text/plain". So I've made the adjustment but I'm still getting the same issue... Good catch! Commented Nov 26, 2008 at 22:08
3

Change this To:

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

The content char set need to be set, I don't see why the content itself. Should rather be:

message.setContent(sBuffer.toString(), "text/html;charset=iso-8859-1");
Buhake Sindi
89.4k30 gold badges176 silver badges234 bronze badges
answered Jan 12, 2010 at 15:09
3

I used the following code:

mimeBodyPart1.setDataHandler(new DataHandler(new ByteArrayDataSource(messageBody, "text/html; charset=utf-8")));
multiPart.addBodyPart(mimeBodyPart1);
message.setContent(multiPart, "text/html; charset=utf-8");

Now, Outlook displays in html format.

Alex K
22.9k19 gold badges116 silver badges245 bronze badges
answered Sep 20, 2012 at 7:04
1

You should look at the source of the received message: is the Content-Type of the message multipart/alternative?

answered Nov 26, 2008 at 22:32
1
message.setContent(new String(sBuffer.toString().getBytes(), "iso-8859-1"), "text/html; charset=iso-8859-1");

Should solve your problem (removed \" characters).

stealthyninja
10.4k11 gold badges55 silver badges61 bronze badges
answered Sep 14, 2010 at 6:08
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 [email protected]

Buhake Sindi
89.4k30 gold badges176 silver badges234 bronze badges
answered Mar 10, 2009 at 15:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.