使用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 大家好,后续会间断地奉 ...
随机推荐
- 使用原生js写ajax
// 使用原生js 封装ajax // 兼容xhr对象 function createXHR(){ if(typeof XMLHttpRequest != "undefined") ...
- 【性能测试】性能测试总结<四>
性能测试常见指标 性能测试说白了就是通过工具模拟多个用户对被测系统进行访问.然后查看系统对于多个用户发来请求的处理能力. 左边的两个小人表示两个用户,向右边服务器发送请求,然后得到服务器 ...
- Puppet Agent/Master HTTPS Communications
The agent/master HTTP interface is REST-like, but varies from strictly RESTful design in several way ...
- 通过共享用户ID来实现多个应用程序使用同一个进程(一些情况的测试)
从很多方面来看,每个Android 应用程序都存在于它自己的世界之中:• 默认情况下,每个应用程序均运行于它自己的Linux 进程中.当应用程序中的任意代码开始执行时,Android 启动一个进程,而 ...
- OkHttp 源码分析
在工作中用到封装HTTP传输的OkHTTP,OkHttp是相对成熟的解决方案,同时也是开源项目.本文将从源码角度看下OkHttp是如何实现一些网络操作的. HTTP GET: OkHttpClient ...
- POJ 1410 Intersection(计算几何)
题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...
- NeHe OpenGL教程 第三十七课:卡通映射
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 大神:python怎么爬取js的页面
大神:python怎么爬取js的页面 可以试试抓包看看它请求了哪些东西, 很多时候可以绕过网页直接请求后面的API 实在不行就上 selenium (selenium大法好) selenium和pha ...
- Redis中7种集合类型应用场景&redis常用命令
Redis常用数据类型 Redis最为常用的数据类型主要有以下五种: String Hash List Set Sorted set 在具体描述这几种数据类型之前,我们先通过一张图了解下Redis内部 ...
- (英文版)使用Visual Studio 2015 编写 MASM 汇编程序!
原文地址:http://kipirvine.com/asm/gettingStartedVS2015/index.htm#CreatingProject Getting Started with MA ...