Commons Email 发送 HtmlEmail失败原因分析

今天折腾了一天,有个业务需要写个简单的邮件发送,但是测试了。从Apache Commons Email 上取得的例子,稍作了修改:

Java代码   收藏代码
  1. public class SendMail {  
  2.     public static void main(String[] args) throws Exception {  
  3.   
  4.         HtmlEmail email = new HtmlEmail();  
  5.         email.setCharset("UTF-8");  
  6.         email.setHostName("smtp.163.com");  
  7.         email.addTo("bestupon@foxmail.com""BestUpon");  
  8.         email.setFrom("guzhuyunfan@163.com""蒋绪升");  
  9.         email.setSubject("这是一个主题");  
  10.         email.setAuthentication("username""password");  
  11.         email.setMsg("这是一个消息...........");  
  12.         URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");  
  13.         String cid = email.embed(url, "Apache logo");  
  14.         email.setHtmlMsg("<html>测试十四和<img src=\"cid:" + cid + "\"></html>");  
  15.         email.setTextMsg("Your email client does not support HTML messages 这是一个TEXTMEG");  
  16.         email.send();  
  17.         System.out.println("发送成功!");  
  18.   
  19.     }  
  20. }  

 这个测试例子,但是使用以上的代码发送邮件,应该的到,也得到了如下的结果:

 

 

但是当我将其放置到Web工程中的时候:

Java代码   收藏代码
  1. package com.jxs.sys.core.global.mail.service.impl;  
  2.   
  3. import java.net.MalformedURLException;  
  4. import java.net.URL;  
  5.   
  6. import org.apache.commons.lang.StringUtils;  
  7. import org.apache.commons.mail.Email;  
  8. import org.apache.commons.mail.EmailException;  
  9. import org.apache.commons.mail.HtmlEmail;  
  10. import org.springframework.stereotype.Service;  
  11.   
  12. import com.jxs.sys.core.base.manager.ManagerSupport;  
  13. import com.jxs.sys.core.global.config.utils.PropertyHolder;  
  14. import com.jxs.sys.core.global.mail.dao.MailDao;  
  15. import com.jxs.sys.core.global.mail.model.Mail;  
  16. import com.jxs.sys.core.global.mail.service.MailManager;  
  17.   
  18. @Service("sendMailManager")  
  19. public class MailManagerImpl extends ManagerSupport<Mail, MailDao> implements MailManager {  
  20.   
  21.     public void sendMail(Mail mailMessage) {  
  22.         try {  
  23.             /*Email msg = null; 
  24.             msg = buildMessage(mailMessage); 
  25.             String msgto = mailMessage.getTo(); 
  26.             if (StringUtils.isNotEmpty(msgto)) { 
  27.                 String msgtos[] = msgto.split(";"); 
  28.                 for (String msto : msgtos) { 
  29.                     String to[] = msto.split(","); 
  30.                     if (to.length == 2) { 
  31.                         msg.addTo(to[1], to[0]); 
  32.                         msg.send(); 
  33.                     } 
  34.                 } 
  35.             }*/  
  36.             HtmlEmail email = new HtmlEmail();  
  37.             email.setCharset("UTF-8");  
  38.             email.setHostName("smtp.163.com");  
  39.             email.addTo("bestupon@foxmail.com""BestUpon");  
  40.             email.setFrom("guzhuyunfan@163.com""蒋绪升");  
  41.             email.setSubject("这是一个测试主题");  
  42.             email.setAuthentication("username""password");  
  43.             email.setMsg("这是一个消息...........");  
  44.             URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");  
  45.             String cid = email.embed(url, "Apache logo");  
  46.             email.setHtmlMsg("<html>测试十测试内容四和<img src=\"cid:" + cid + "\"></html>");  
  47.             email.setTextMsg("Your email client does not support HTML messages 这是一个TEXTMEG");  
  48.             email.send();  
  49.             System.out.println("发送成功!");  
  50.         } catch (EmailException e) {  
  51.             this.log.info("消息发送失败! 原因: " + e.getMessage());  
  52.         } catch (MalformedURLException e) {  
  53.             this.log.info("消息发送失败! 原因: " + e.getMessage());  
  54.         }  
  55.     }  
  56.   
  57.     private Email buildMessage(Mail mailMessage) throws EmailException, MalformedURLException {  
  58.         HtmlEmail email = new HtmlEmail();  
  59.         email.setCharset("UTF-8");  
  60.         email.setHostName(PropertyHolder.getProperty("mail.host"));// 主机  
  61.   
  62.         String from[] = mailMessage.getFrom().split(",");  
  63.         email.setFrom(from[1], from[0]);  
  64.         email.setSubject(mailMessage.getSubject());  
  65.   
  66.         email.setAuthentication(PropertyHolder.getProperty("mail.username"), PropertyHolder.getProperty("mail.password"));  
  67.         email.setHtmlMsg("<html>" + mailMessage.getContent() + "</html>");  
  68.         return email;  
  69.     }  
  70. }  
 

利用Action调用的时候,会出现如下的结果:

Base64代码   收藏代码
  1. ------=_Part_0_12512205.1282227745012  
  2. Content-Type: multipart/related; boundary="----=_Part_1_7008653.1282227745012"  
  3.   
  4. ------=_Part_1_7008653.1282227745012  
  5. Content-Type: multipart/alternative; boundary="----=_Part_2_18379432.1282227745012"  
  6.   
  7. ------=_Part_2_18379432.1282227745012  
  8. Content-Type: text/plain; charset=UTF-8  
  9. Content-Transfer-Encoding: quoted-printable  
  10.   
  11. Your email client does not support HTML messages =E8=BF=99=E6=98=AF=E4=B8=80=  
  12. =E4=B8=AATEXTMEG  
  13. ------=_Part_2_18379432.1282227745012  
  14. Content-Type: text/html; charset=UTF-8  
  15. Content-Transfer-Encoding: quoted-printable  
  16.   
  17. <html>=E6=B5=8B=E8=AF=95=E5=8D=81=E5=9B=9B=E5=92=8C<img src=3D"cid:hiuudkwrl=  
  18. s"></html>  
  19. ------=_Part_2_18379432.1282227745012--  

 这样的结果,测试后了好几次,还是没有解决问题。

 

1.前面按照是过滤器的思路解决,去掉了所有的过滤器,行不通。

2.想着是不是本工程的编码问题,写了一个编码过滤器,行不通,

3.后来就直接新建了一个工程,使用一个Servlet来发送邮件,结果是成功了。

4.将前面的工程代码全部拷贝到上面的工程中,有行不通了.

5.这样猜想问题是出现在了jar包上了,是不是JavaMail的包和Commons Email的包有冲突,去掉了JavaMail的包还是行不通,

6.顺着5的这个思路,将原来所有的jar包都拷贝到这个工程中,不加载其他的任何信息,清理了一遍所有的JAR包,将以下的包,能发送成功了。

问题出现在了geronimo-javamail_1.4_spec-1.3 和 javamail 有冲突,geronimo-javamail_1.4_spec-1.3这个是使用CXF的时候引进的无用包!结果很让人郁闷,花费了我一天的时间!,写下来,警戒切勿引用无用包!


-------------------------------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值