使用springMail发送带附件的email(墙角的续集)

使用spring Mail 发送带email的邮件,大大的减小了使用java email发送邮件的麻烦,
spring提供了一个MimeMessageHelper的助手类来完成发送附件的功能

 

Java代码 复制代码
  1. package mail;   
  2.   
  3. import javax.mail.MessagingException;   
  4. import javax.mail.internet.MimeMessage;   
  5.   
  6. import org.springframework.context.ApplicationContext;   
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  8. import org.springframework.core.io.ClassPathResource;   
  9. import org.springframework.mail.javamail.JavaMailSender;   
  10. import org.springframework.mail.javamail.MimeMessageHelper;   
  11.   
  12. public class Main {   
  13.   
  14.     /**  
  15.      * @param args  
  16.      */  
  17.     public static void main(String[] args) {   
  18.         // TODO Auto-generated method stub   
  19.         ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");   
  20.         JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");   
  21.         MimeMessage mime = mailSender.createMimeMessage();   
  22.         MimeMessageHelper helper;   
  23.         try {   
  24.             helper = new MimeMessageHelper(mime,true,"utf-8");   
  25.             helper.setFrom("abcd@163.com");   
  26.             helper.setTo("abcd@gmail.com");   
  27.             helper.setSubject(" 测试spring Mail的附件功能");   
  28.             //需要将附件显示在html中   
  29.             //在标签中用cid:xx 标记,使用helper.addInline()方法添加   
  30.             helper.setText("<html><body>javaeye是个好网站:<br>"+   
  31.                     "<a href='http://www.iteye.com'>" +   
  32.                     "<img src='cid:logo'></a></body></html>",true);   
  33.             helper.addInline("logo"new ClassPathResource("logo.gif"));   
  34.             helper.addAttachment("javaeye.gif"new ClassPathResource("javaeye.gif"));   
  35.         } catch (MessagingException e) {   
  36.             // TODO Auto-generated catch block   
  37.             e.printStackTrace();   
  38.         }   
  39.        
  40.         mailSender.send(mime);   
  41.     }   
  42.   
  43. }  
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 ,同发送普通邮件的一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值