JavaMail、Commons Email发送邮件 出现结果后到的文件是Base64的问题的解决办法

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

public class SendMail {
	public static void main(String[] args) throws Exception {

		HtmlEmail email = new HtmlEmail();
		email.setCharset("UTF-8");
		email.setHostName("smtp.163.com");
		email.addTo("bestupon@foxmail.com", "BestUpon");
		email.setFrom("guzhuyunfan@163.com", "蒋绪升");
		email.setSubject("这是一个主题");
		email.setAuthentication("username", "password");
		email.setMsg("这是一个消息...........");
		URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
		String cid = email.embed(url, "Apache logo");
		email.setHtmlMsg("<html>测试十四和<img src=\"cid:" + cid + "\"></html>");
		email.setTextMsg("Your email client does not support HTML messages 这是一个TEXTMEG");
		email.send();
		System.out.println("发送成功!");

	}
}

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

 

 

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

package com.jxs.sys.core.global.mail.service.impl;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.stereotype.Service;

import com.jxs.sys.core.base.manager.ManagerSupport;
import com.jxs.sys.core.global.config.utils.PropertyHolder;
import com.jxs.sys.core.global.mail.dao.MailDao;
import com.jxs.sys.core.global.mail.model.Mail;
import com.jxs.sys.core.global.mail.service.MailManager;

@Service("sendMailManager")
public class MailManagerImpl extends ManagerSupport<Mail, MailDao> implements MailManager {

	public void sendMail(Mail mailMessage) {
		try {
			/*Email msg = null;
			msg = buildMessage(mailMessage);
			String msgto = mailMessage.getTo();
			if (StringUtils.isNotEmpty(msgto)) {
				String msgtos[] = msgto.split(";");
				for (String msto : msgtos) {
					String to[] = msto.split(",");
					if (to.length == 2) {
						msg.addTo(to[1], to[0]);
						msg.send();
					}
				}
			}*/
			HtmlEmail email = new HtmlEmail();
			email.setCharset("UTF-8");
			email.setHostName("smtp.163.com");
			email.addTo("bestupon@foxmail.com", "BestUpon");
			email.setFrom("guzhuyunfan@163.com", "蒋绪升");
			email.setSubject("这是一个测试主题");
			email.setAuthentication("username", "password");
			email.setMsg("这是一个消息...........");
			URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
			String cid = email.embed(url, "Apache logo");
			email.setHtmlMsg("<html>测试十测试内容四和<img src=\"cid:" + cid + "\"></html>");
			email.setTextMsg("Your email client does not support HTML messages 这是一个TEXTMEG");
			email.send();
			System.out.println("发送成功!");
		} catch (EmailException e) {
			this.log.info("消息发送失败! 原因: " + e.getMessage());
		} catch (MalformedURLException e) {
			this.log.info("消息发送失败! 原因: " + e.getMessage());
		}
	}

	private Email buildMessage(Mail mailMessage) throws EmailException, MalformedURLException {
		HtmlEmail email = new HtmlEmail();
		email.setCharset("UTF-8");
		email.setHostName(PropertyHolder.getProperty("mail.host"));// 主机

		String from[] = mailMessage.getFrom().split(",");
		email.setFrom(from[1], from[0]);
		email.setSubject(mailMessage.getSubject());

		email.setAuthentication(PropertyHolder.getProperty("mail.username"), PropertyHolder.getProperty("mail.password"));
		email.setHtmlMsg("<html>" + mailMessage.getContent() + "</html>");
		return email;
	}
}
 

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

------=_Part_0_12512205.1282227745012
Content-Type: multipart/related; boundary="----=_Part_1_7008653.1282227745012"

------=_Part_1_7008653.1282227745012
Content-Type: multipart/alternative; boundary="----=_Part_2_18379432.1282227745012"

------=_Part_2_18379432.1282227745012
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Your email client does not support HTML messages =E8=BF=99=E6=98=AF=E4=B8=80=
=E4=B8=AATEXTMEG
------=_Part_2_18379432.1282227745012
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>=E6=B5=8B=E8=AF=95=E5=8D=81=E5=9B=9B=E5=92=8C<img src=3D"cid:hiuudkwrl=
s"></html>
------=_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的时候引进的无用包!结果很让人郁闷,花费了我一天的时间!,写下来,警戒切勿引用无用包!

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值