How to send html email to outlook from Java

前端 未结 8 2195
忘掉有多难
忘掉有多难 2021-02-04 04:06

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 
{
    P         


        
8条回答
  •  醉酒成梦
    2021-02-04 04:44

    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.

提交回复
热议问题