使用spring Mail 发送带email的邮件,大大的减小了使用java email发送邮件的麻烦,
spring提供了一个MimeMessageHelper的助手类来完成发送附件的功能
- package mail;
-
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.mail.javamail.JavaMailSender;
- import org.springframework.mail.javamail.MimeMessageHelper;
-
- public class Main {
-
-
-
-
- public static void main(String[] args) {
-
- ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
- JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
- MimeMessage mime = mailSender.createMimeMessage();
- MimeMessageHelper helper;
- try {
- helper = new MimeMessageHelper(mime,true,"utf-8");
- helper.setFrom("abcd@163.com");
- helper.setTo("abcd@gmail.com");
- helper.setSubject(" 测试spring Mail的附件功能");
-
-
- helper.setText("<html><body>javaeye是个好网站:<br>"+
- "<a href='http://www.iteye.com'>" +
- "<img src='cid:logo'></a></body></html>",true);
- helper.addInline("logo", new ClassPathResource("logo.gif"));
- helper.addAttachment("javaeye.gif", new ClassPathResource("javaeye.gif"));
- } catch (MessagingException e) {
-
- e.printStackTrace();
- }
-
- mailSender.send(mime);
- }
-
- }
package mail;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper;
try {
helper = new MimeMessageHelper(mime,true,"utf-8");
helper.setFrom("abcd@163.com");
helper.setTo("abcd@gmail.com");
helper.setSubject(" 测试spring Mail的附件功能");
//需要将附件显示在html中
//在标签中用cid:xx 标记,使用helper.addInline()方法添加
helper.setText("<html><body>javaeye是个好网站:<br>"+
"<a href='http://www.iteye.com'>" +
"<img src='cid:logo'></a></body></html>",true);
helper.addInline("logo", new ClassPathResource("logo.gif"));
helper.addAttachment("javaeye.gif", new ClassPathResource("javaeye.gif"));
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mailSender.send(mime);
}
}
对应的config.xml ,同发送普通邮件的一样
分享到:
相关推荐
搜狗输入法皮肤 软件 《躲在墙角的彼尔德》搜狗输入法动画皮肤 图片
行业资料-建筑装置-带墙角包边机构的钢结构.zip
三角连接器,放在墙角处_三角连接器,放在墙角1.stl
墙角的父亲
墙角的垃圾采用本.ppt
CN110762631A墙角空调 .pdf
墙角的垃圾记叙文.docx
OBLOG 蹲在墙角的野孩子
改写作文:墙角的垃圾范文.docx
收教所石材墙角强度计算书.doc
iOS和Android开启互挖墙角模式.pdf
Android正在挖iOS在平板市场上的墙角.pdf
行业资料-交通装置-一种墙角排水管支撑保护装置.zip
电信设备-可清除墙角灰尘的移动扫地设备.zip
利用GPS RTK测定建筑物墙角点方法研究.pdf
只要锄头舞的好,哪有墙角挖不倒.doc
“墙角数枝梅,凌寒独自开”的意思.doc
【墙角数枝梅,凌寒独自开】代码改变未来.html
行业文档-设计装置-一种轻体保温墙体板墙角.zip
行业文档-设计装置-一种建筑用墙角垂直修砌装置