使用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 大家好,后续会间断地奉 ...
随机推荐
- ExtJs学习笔记之Window组件
Window窗体组件 window是一个指定的打算作为一个应用程序窗口的面板,默认窗口是浮动的,resizable, 并且draggable,默认的,窗体靠document.body呈现. 1.示例: ...
- apache commons vfs 文件夹监控
package test.vfs; import java.io.File; import org.apache.commons.logging.Log; import org.apache.comm ...
- [Hibernate] - Select/Update/Delete/Insert
Java bean: package com.my.bean; import java.util.Date; public class WorkPack { private String uWorkP ...
- jquery 整理之一
1.选择器: jQuery 属性选择器 jQuery 使用 XPath 表达式来选择带有给定属性的元素. $("[href]") 选取所有带有 href 属性的元素. $(&quo ...
- LintCode "Partition Array by Odd and Even"
One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integ ...
- 【sql】之使用sql根据身份证查询过生日人数
根据当前日期查询有多少人过生日 ,) = DATE_FORMAT(NOW(),'%m'); 查询price一样的人数 select * from people where price in (sele ...
- oracle学习笔记(四)oracle内存优化
emca -config dbcontrol db -repos recreate 解决'oracle Environment variable ORACLE_SID not defined. Ple ...
- activiti自定义流程之整合(一):整体环境配置
结合之前所说的自定义流程的思路,分别是后台.前台.整合,之前的内容也分别进行了相关的练习和尝试,现在就该到了最后的整合了,依旧是以实现功能为目的,细节暂且不去管他. 因为我们实际项目后端用的是spri ...
- Android APP高效开发的十大建议
在使用Android开发APP过程中,为什么确保最优化.运行流畅且不会使Android系统出现问题至关重要呢?因为影响APP产品效率的每一个问题,如:耗电或内存占用情况等,都是关乎APP成功与否关键因 ...
- Hadoop使用lzo压缩格式
在hadoop中搭建lzo环境: wget http://www.oberhumer.com/opensource/lzo/download/lzo-2.06.tar.gz export CFLAGS ...