使用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 大家好,后续会间断地奉 ...
随机推荐
- eclipse luna maven搭建spring mvc
1. 环境配置 a) Java 1.7 b) Eclipse luna c) Maven3.2.5 d) Spring 4.1.4 2. ...
- 图片上传,图片剪切jquery.imgareaselect
---恢复内容开始--- <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- Blitz Templates介绍
Blitz Templates Blitz Templates-应用于大型互联网项目的非常强大非常快的模板引擎. 下载: sourceforge, 源代码 主页, win32 二进制文件, 其他语 ...
- 虚拟机IP设置
实验软件环境:虚拟机Vmware Workstation10.0 .CentOS 6.5 32位 1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分 ...
- 如何利用gatling创建一个性能测试例
[原创博文,转载请声明出处] 基于上一篇博文介绍如何源码编译创建自己的gatling可执行工具,今天介绍一下如何基于gatling做性能测试! 由于gatling的测试例脚本是基于scala写的,所以 ...
- Android官方提供的支持不同屏幕大小的全部方法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8830286 原文地址为:http://developer.android.com/ ...
- 【Hadoop环境搭建】Centos6.8搭建hadoop伪分布模式
阅读目录 ~/.ssh/authorized_keys 把公钥加到用于认证的公钥文件中,authorized_keys是用于认证的公钥文件 方式2: (未测试,应该可用) 基于空口令创建新的SSH密钥 ...
- Puppet master/agent installation on RHEL7
==================================================================================================== ...
- JS截取后缀名,文件全名,非后缀名的方法---收藏(冷饭_)
<script language="javascript" type="text/javascript"> //取整个文件的路径并且把文件名赋给文件 ...
- 【MySQL】数据行长度的一些限制
今天开发在导入数据的时候报一个错误: Row size too large. The maximum row size for the used table type, not counting BL ...