springMai的html文本和附件的传递、解决html中文乱码、解决附件的中文名字乱码
来源:互联网 发布:vggnet 网络结构 编辑:程序博客网 时间:2024/06/04 23:25
package cn.itcast.mail;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import com.sun.xml.messaging.saaj.packaging.mime.internet.MimeUtility;
public class JavaSpringMailTest {
/*
* 解决html的中文乱码和附件的英文名字乱码
* 在使用中有一些中文的乱码问题需要解决!
1.增加验证,否则会报:553 authentication is required 的错误信息
Properties prop = new Properties();
prop.setProperty("mail.smtp.auth", "true");
JavaMailSenderImpl.setJavaMailProperties(prop);
2.当邮件内容是HTML语言时的中文问题:
初始化MimeMessageHelper辅助类时,设置"GBK" encoding!如:
MimeMessageHelper messageHelp = new MimeMessageHelper(message,true,"GBK");
同时在设置:<META http-equiv=Content-Type content='text/html; charset=GBK'>
如果都设置为"UTF-8",在某些邮件客户端标题不能正常显示!
3.邮件附件的中文问题!
spring的文档里面说MimeMessageHelper设置了encoding,同时对title,text,attach产生作用,但还是会出问题:
解决方法:MimeUtility.encodeWord(file.getName());就OK了!
*
*/
public static void sendMail() throws MessagingException, UnsupportedEncodingException
{
JavaMailSenderImpl impl = new JavaMailSenderImpl();
impl.setHost("smtp.163.com");
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
impl.setJavaMailProperties(properties);
impl.setUsername("ch469299503");
impl.setPassword("CH13410084766");
MimeMessage message = impl.createMimeMessage();
//第三个参数是解决乱码
MimeMessageHelper helper = new MimeMessageHelper(message, true,"GBK");
helper.setSubject("这是发送带有html文本");
helper.setFrom(new InternetAddress("ch469299503@163.com"));
helper.setTo(new InternetAddress("ch900915caohuan@163.com"));
//这是发送html文本,第二个参数表示前面的内容是html格式的,这里的cid:file,前面的cid表示contentId是一个固定的值,后面的file是一个
//占位符,后面的helper.addInline("file", classPathResource)就是来填充占位符的
/*
*
*
*/
//这里的head部分是为了解决乱码
helper.setText("<html><head><meta http-equiv='keywords' content='keyword1,keyword2,keyword3'>" +
"<meta http-equiv='description' content='this is my page'><meta http-equiv='content-type' content='text/html; charset=GBK'>" +
"</head><body>这就是爱<img src=cid:file></body></html>", true);
ClassPathResource classPathResource = new ClassPathResource("mianshi.jpg");
//这句代码在填充上面的占位符的同时,还将这张图片作为附件传过去
helper.addInline("file", classPathResource);
//解决附件的中文名字乱码问题,这是传递附件
helper.addAttachment(MimeUtility.encodeWord("面试.jpg"), classPathResource);
// impl.send(message);
}
public static void main(String[] args) {
try {
try {
sendMail();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- springMai的html文本和附件的传递、解决html中文乱码、解决附件的中文名字乱码
- HTML中文乱码的解决
- JavaMail中解决中文附件名乱码的问题
- 解决Android4.3版本下,手机短彩接收中文文件名附件,中文名字的附件无法保存(第一步:解决从网络下载附件后,持久化时中文文件名中文乱码)
- footer.html中中文乱码的解决
- html中文乱码问题的解决
- python pyh html解决中文中文乱码的方法
- 附件下载,中文文件名乱码如何解决???
- 解决Android4.3版本下,手机短彩接收中文文件名附件,中文名字的附件无法保存(第二步:解决从从数据库中读取附件文件名,并在长按后保存附件时,中文乱码导致的无法保存附件)
- mutt发送的附件存在中文乱码
- 一个发邮件的类,带验证功能,可以发html内容,可以添加附件,并解决附件乱码问题。
- 一个发邮件的类,带验证功能,可以发html内容,可以添加附件,并解决附件乱码问题。
- 一个发邮件的类,带验证功能,可以发html内容,可以添加附件,并解决附件乱码问题。
- php实现的下载附件,解决乱码
- QReader阅读中文文本乱码的解决
- html向action提交表单,中文乱码问题的解决
- 如何解决HTML网页中文显示乱码的方法
- 解决get传递中文乱码的问题
- YouTube视频播放器采用了HTML5
- struts2的ajax实现
- Windows 8 Store应用开发XAML实例教程系列汇总
- awr report
- Vaadin Web应用开发教程(18):UI组件-Table 组件
- springMai的html文本和附件的传递、解决html中文乱码、解决附件的中文名字乱码
- Android ApiDemos示例解析(120):Views->Gallery->2. People
- 数据结构面试之一——单链表常见操作
- 对架构师而言,什么最重要?
- 链表集锦
- DataReader对象转换为实体类与给存储过程批量添加参数
- 执行脚本是报错./cross.install: 25: Syntax error: "do" unexpected (expecting "fi")
- springMail发送邮件,转载别人的,感觉挺有用的
- ArcGIS Runtime 加载SHAPE数据的另一种方式-动态图层