如何从Java发送html电子邮件到Outlook [英] How to send html email to outlook from Java

查看:367
本文介绍了如何从Java发送html电子邮件到Outlook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaMail发送html格式的电子邮件,但它似乎只能在Outlook中显示为文字电子邮件。

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.

这是我的代码:

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

任何想法,为什么html版本的电子邮件不会显示在Outlook?

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

推荐答案

经过大量调查,我已经取得了一些重大进展。

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

首先,我建议不要直接使用JavaMail,而是使用 Jakarta Commons Email 图书馆。这真的很简单的问题了!

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

代码现在是:

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();

谈论简单。

但是,仍然有一个问题。电子邮件的html版本在Gmail,Hotmail等方面表现非常出色,但在Outlook中仍然无法正确显示。它总是想显示文本版本,我不知道为什么。我怀疑这是Outlook中的设置,但我找不到...

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

这篇关于如何从Java发送html电子邮件到Outlook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆