How to send html email to outlook from Java

前端 未结 8 2034
忘掉有多难
忘掉有多难 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:53

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

提交回复
热议问题