使用spring的邮件发送功能
使用spring提供的MailSender和JavaMailSender类。
1、邮件对象类
package cn.luxh.app.mail;
import java.util.List;
import org.springframework.core.io.AbstractResource;
public class Email {
//发件人
private String from;
//收件人
private String[] to;
//主题
private String subject;
//邮件内容
private String text;
//附件
private List<AbstractResource> resources;
//geter seter
//...
}
2、邮件发送服务类
package cn.luxh.app.mail; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import org.springframework.core.io.AbstractResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; public class MailService { //简单的文本邮件发送类
private MailSender mailSender;
//复杂邮件发送类
private JavaMailSender javaMailSender; /**
* 发送简单的文本邮件
* @param email
*/
public void send(Email email){
SimpleMailMessage smm = new SimpleMailMessage();
smm.setFrom(email.getFrom());
smm.setSubject(email.getSubject());
smm.setTo(email.getTo());
smm.setText(email.getText());
mailSender.send(smm); } /**
* 发送复杂邮件
* @param email
* @throws MessagingException
*/
public void sendMime(Email email) throws MessagingException{
MimeMessage mm = javaMailSender.createMimeMessage();
//加上编码,解决中文乱码
MimeMessageHelper helper = new MimeMessageHelper(mm,true,"GB2312"); helper.setFrom(email.getFrom());
helper.setTo(email.getTo());
helper.setSubject(email.getSubject());
helper.setText(email.getText(),true); //添加附件
if(email.getResources()!=null && email.getResources().size()>0) {
for(AbstractResource resource:email.getResources()) {
helper.addAttachment(resource.getFilename(), resource);
}
} javaMailSender.send(mm); } public MailSender getMailSender() {
return mailSender;
} public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
} public JavaMailSender getJavaMailSender() {
return javaMailSender;
} public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
}
3、邮件属性配置文件mail.properties
#smtp服务器
mail.host=smtp.163.com #用户名
mail.username=heymenfolk@163.com #密码
mail.password=your password
4、spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 启用注解支持 -->
<context:annotation-config /> <!-- 加载属性文件 -->
<context:property-placeholder location="classpath:mail.properties" /> <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${mail.host}"/>
<property name="username" value="${mail.username}"/>
<property name="password" value="${mail.password}"/>
</bean> <bean id="mailService" class="cn.luxh.app.mail.MailService">
<property name="mailSender" ref="javaMailSender"/>
<property name="javaMailSender" ref="javaMailSender"/>
</bean>
</beans>
5、测试
package cn.luxh.app.test; import java.util.ArrayList;
import java.util.List; import javax.mail.MessagingException; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.luxh.app.mail.Email;
import cn.luxh.app.mail.MailService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:app-mail.xml"})
public class MailTester { @Autowired
private MailService mailService;
@Test
public void testSendMail() {
Email email = new Email();
email.setFrom("heymenfolk@163.com");
email.setTo(new String[]{"21760658@qq.com"});
email.setSubject("简单文本邮件");
email.setText("how are you.i am from china!\r你好,程序猿!!");
mailService.send(email);
} @Test
public void testSendMimeMail() throws MessagingException {
Email email = new Email();
email.setFrom("heymenfolk@163.com");
email.setTo(new String[]{"21760658@qq.com","heymenfolk@outlook.com"});
email.setSubject("复杂邮件");
String text = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='http://luxh.cnblogs.com'>我的博客</a></h1></body></html>";
email.setText(text); List<AbstractResource> resources = new ArrayList<AbstractResource>();
//添加附件
ClassPathResource file1 = new ClassPathResource("top1.jpg");
FileSystemResource file2 = new FileSystemResource("d:/中文.txt");
resources.add(file1);
resources.add(file2);
email.setResources(resources); mailService.sendMime(email);
}
}
使用spring的邮件发送功能的更多相关文章
- spring-boot-route(二十二)实现邮件发送功能
在项目开发中,除了需要短信验证外,有时候为了节省 短信费也会使用邮件发送.在Spring项目中发送邮件需要封装复杂的消息体,不太方便.而在Spring Boot项目中发送邮件就太简单了,下面一起来看看 ...
- SpringBoot 2.X从0到1实现邮件发送功能
Spring中提供了JavaMailSender接口实现邮件发送功能,在SpringBoot2.X中也封装了发送邮件相关的Starter并且提供了自动化配置. 本文目录 一.添加对应的Starter二 ...
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
- redmine邮件发送功能配置详解
redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...
- .NET开发邮件发送功能
.NET开发邮件发送功能 今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知 ...
- shell邮件发送功能实现
本文中以163邮箱为例,测试shell邮件发送功能.常见的工具有:mailx.sendmail.mutt等. 1.设置邮件客户端 (1)启用pop3.smtp服务,以支持第三方客户端支持 (2)设置授 ...
- System.Net邮件发送功能踩过的坑
System.Net邮件发送功能踩过的坑 目录 System.Net邮件发送功能踩过的坑 1.EazyEmail邮件发送类库 2.邮件发送授权码与邮件密码 3.通过邮件密码来发送邮件 4.Wiresh ...
- Spring Boot 2.0 图文教程 | 集成邮件发送功能
文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ...
随机推荐
- mysql将字符转换成数字
在操作mysql时,经常需要将字符转换成数字,这一步虽然简单,但不常用的话也很容易忘记,现将在网上找到的方法记录如下: 1.将字符的数字转成数字,比如'0'转成0可以直接用加法来实现例如:将pony表 ...
- ScrumMaster需要了解的7件事
当一个组织开始使用Scrum时,被选为担任Scrumaster角色的人通常来自于那些有管理背景的人.组织期望那些管理人员,所谓的“大师”,能够交付Scrum项目因为她有管理的专门知识——并且可以同时管 ...
- R(三): R包原理及安装
包(package)是多个函数的集合,常作为分享代码的基本单元,代码封装成包可以方便其他用户使用.越来越多的R包正在由世界上不同的人所创建并分发,这些分发的R包,可以从CRAN 或 github 上获 ...
- Eclipse中添加web dynamic project
因为我的eclipse版本是kepler service release 2,所以我用了这个链接,http://download.eclipse.org/releases/helios/ 参考链接: ...
- python使用random函数生成随机数
python使用random函数来生成随机数,常用的方法有: import random #生成0-1之间的数(不包括0和1) random.random() #生成1-5之间的随机整数(包括1和5) ...
- Tortoise SVN Clean up失败的解决方法
step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe (找到 Precompiled Binaries for ...
- Python try/except/finally应用
1.通过if和else处理异常 import os if os.path.exists('sketch.txt'): data = open ('sketch.txt') for each_line ...
- 第二次正式java web开发项目的总结(回收站恢复)
都说互联网行业加班很是厉害,记得前不久网上还晒出了几个大城市互联网行业的加班排名调查,但是我们公司,或者说我们项目组倒是非常的例外,进公司也差不多半年了,才仅仅上个月有一个周六加过一天班而已. 不过好 ...
- C#应用程序单进程检测
以下程序经过VS2010测试通过: /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Ma ...
- Navicat(连接)-1
连接 要在 Navicat 开始使用你的服务器,你要首先用连接窗口创建一个或多个连接.如果你是一个服务器新手和不肯定如何工作,你可能想看看: MySQL 用户手册 Oracle 数据库文件 Postg ...