使用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的邮件发送功能的更多相关文章

  1. spring-boot-route(二十二)实现邮件发送功能

    在项目开发中,除了需要短信验证外,有时候为了节省 短信费也会使用邮件发送.在Spring项目中发送邮件需要封装复杂的消息体,不太方便.而在Spring Boot项目中发送邮件就太简单了,下面一起来看看 ...

  2. SpringBoot 2.X从0到1实现邮件发送功能

    Spring中提供了JavaMailSender接口实现邮件发送功能,在SpringBoot2.X中也封装了发送邮件相关的Starter并且提供了自动化配置. 本文目录 一.添加对应的Starter二 ...

  3. .NET开发邮件发送功能的全面教程(含邮件组件源码)

    今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知识 2)         ...

  4. 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇

    在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...

  5. redmine邮件发送功能配置详解

    redmine的邮件发送功能还是很有用的.像项目有更新啦,任务分配啦,都能邮件发送的相关责任人.我自己在linux服务器上安装并启动了redmine后,邮件一直发送了不了.查了网上的资料,都是讲修改下 ...

  6. .NET开发邮件发送功能

    .NET开发邮件发送功能 今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知 ...

  7. shell邮件发送功能实现

    本文中以163邮箱为例,测试shell邮件发送功能.常见的工具有:mailx.sendmail.mutt等. 1.设置邮件客户端 (1)启用pop3.smtp服务,以支持第三方客户端支持 (2)设置授 ...

  8. System.Net邮件发送功能踩过的坑

    System.Net邮件发送功能踩过的坑 目录 System.Net邮件发送功能踩过的坑 1.EazyEmail邮件发送类库 2.邮件发送授权码与邮件密码 3.通过邮件密码来发送邮件 4.Wiresh ...

  9. Spring Boot 2.0 图文教程 | 集成邮件发送功能

    文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ...

随机推荐

  1. SSDB

    一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis. 特性 替代 Redis 数据库, Redis 的 100 倍容量 LevelDB 网络支持, 使用 C/C++ 开发 Re ...

  2. mapreduce精简概括--转

    mapreduce精简概括 We want to count all the books in the library. You count up shelf #1, I count up shelf ...

  3. startActivityForResult用法详解

    一.如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int requestCode ...

  4. 程序员书单_java专项进阶篇

    JDBC API数据库编程实作教材 http://download.csdn.net/detail/shenzhq1980/9145715 Java事务设计模式 http://download.csd ...

  5. WINRARA 排除 .svn 文件夹

    加入-x*\.svn -x*\.svn\* 即可: rar.exe u -m3 -s -r -o+ -x*.db -x*.zip -x*.rar -x*\.svn -x*\.svn\* zmv9net ...

  6. LintCode "Heapify"

    My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...

  7. QueryRunner的API

    org.apache.commons.dbutils Class QueryRunner java.lang.Object org.apache.commons.dbutils.AbstractQue ...

  8. 内存修改mfc

    vc++6.0,内涵图

  9. python 读取sqlite3 数据库

    import sqlite3 name = "tom" age = 30 con = sqlite3.connect("d:\\test.db") cur = ...

  10. HDU 2087 剪花布条(KMP,不可重叠重复子串)

    给KMP传的数组一定要从0开始!! 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 这题和我上一篇博客类似,只不过不可重叠,我看了数据范围不大,所以就开 ...