300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 使用java mail发送带附件的邮件 网页登录接收正常 foxmail接收附件为dat文件的处理

使用java mail发送带附件的邮件 网页登录接收正常 foxmail接收附件为dat文件的处理

时间:2018-11-28 05:12:07

相关推荐

使用java mail发送带附件的邮件 网页登录接收正常 foxmail接收附件为dat文件的处理

问题引入

在一次做发邮件功能时发现,邮件附件为xlsx文件,在网页登录接收邮箱时,附件正常,用foxmail或者网易邮箱大师客户端接收时,附件有问题,foxmail接收的附件为dat文件,网易邮箱大师接收的为bin文件,在网上查找的时候发现好像是因为附件名过长导致的。参考Java发送邮件Excel附件名称变为.dat文件,虽然我的附件名没超过60个字符,但是有中文,也出现了这个问题

问题解决

参考上文中的博客,需要在Spring 容器启动完成后加入下面设置:

System.setProperty("mail.mime.splitlongparameters","false");

我在系统中新建了一个类,实现了ApplicationListener<ContextRefreshedEvent>接口,代码如下:

package config;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;public class SpringContextInitlized implements ApplicationListener<ContextRefreshedEvent>{/** * <p>Title: onApplicationEvent</p> * <p>Description: </p> * @param arg0 */@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {//web系统中会初始化spring容器和servlet容器,这里给出判定,是spring容器的时候才进行设置if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")){System.setProperty("mail.mime.splitlongparameters","false");}}}

配置这个bean:

<bean class="config.SpringContextInitlized" id="springContextInitlized"/>

然后再测试发邮件,客户端中的附件显示正常。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。