开发者

HtmlEmail not rendering HTML correctly (apache commons)

Hoping that someone can find something I am missing. I am trying to send a simple HTML email within a java application. I am using commons-mail 1.1 (via maven) as my mail lib.

The code I'm using is:

HtmlEmail email = new HtmlEmail();
email.setDebug(true);
email.setHostName("my.emailhost.com");
email.addTo("my_gmail_address@gmail.com");
email.setFrom("my_work_address@emailhost.com");
email.setSubject("Test Subject");
email.setHtmlMsg("<b>This is a bolded message</b>");
email.setTextMsg("This is a text message");
email.send();

I am receiving the email, but it is formatted thusly. It should also be noted that there is no subject, but I obviously specified one in my code.

------=_Part_0_122395252.1304699110505
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is a text message
------开发者_如何学JAVA=_Part_0_122395252.1304699110505
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<b>This is a bolded message</b>
------=_Part_0_122395252.1304699110505--

I'm not sure what I'm doing wrong here. I switched to sending via gmail - and it did send, it just formatted it exactly the same. When i view the raw message I see

Delivered-To: xxxxxxx@gmail.com
Received: by 10.90.73.11 with SMTP id v11cs143993aga;
        Fri, 6 May 2011 09:25:11 -0700 (PDT)
Received: by 10.236.187.97 with SMTP id x61mr4380232yhm.476.1304699111088;
        Fri, 06 May 2011 09:25:11 -0700 (PDT)
Return-Path: <xxxxxxx@xxxxxxx.com>
Received: from xxxxxxx.xxxxxxx.com (xxxxxxx.xxxxxxx.com [x.x.x.x])
        by mx.google.com with ESMTP id 68si9400486yhl.82.2011.05.06.09.25.10;
        Fri, 06 May 2011 09:25:10 -0700 (PDT)
Received-SPF: pass (google.com: domain of xxxxxxx@xxxxxxx.com designates x.x.x.x as permitted sender) client-ip=x.x.x.x;
Authentication-Results: mx.google.com; spf=pass (google.com: domain of xxxxxxx@xxxxxxx.com designates x.x.x.x as permitted sender) smtp.mail=xxxxxxx@xxxxxxx.com
Received: from xxxxxxx-mac-pro.local ([x.x.x.x])
          by xxxxxxx.xxxxxxx.com (Lotus Domino Release 8.5.1FP3)
          with ESMTP id 2011050612251028-17357 ;
          Fri, 6 May 2011 12:25:10 -0400 
From: xxxxxxx@xxxxxxx.com
Date: Fri, 6 May 2011 12:25:10 -0400
Message-ID: <OF8BBF4CBD.70926756-ON85257888.005A31F5@xxxxxxx.com>

Any thoughts would be greatly appreciated.

Cheers


I am not sure, and got no easy way to test it myself.

All examples i saw for commons-mail had the msg formatted as html - site (<html><body><p>Some text for testing</p></body></html>)

Furthermore I noticed this:

email.setHtmlMsg("<b>This is a bolded message</b>"); // HTML TEXT
email.setTextMsg("This is a text message"); // NORMAL Text, with no HTML styles

maybe this is a problem.

Did you tested the way it is written in the API ? They use a StringBuffer:

 StringBuffer msg = new StringBuffer();
 msg.append("<html><body>");
 msg.append("<img src=cid:").append(he.embed(img)).append(">");
 msg.append("<img src=cid:").append(he.embed(png)).append(">");
 msg.append("</body></html>");
 HtmlEmail he = new HtmlEmail();
he.setHtmlMsg(msg.toString());

Just some thoughts, never worked with commons-mail so far...

Hope this help! Regards,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜