1、依赖
<!-- Email发送 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 发送模板邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
<version>1.1.3.RELEASE</version>
</dependency>
2、配置邮箱信息,这里本来我是配置在yml里结果一直测不成功,就干脆将配置放在了代码里,所以我yml里没有配置关于发送邮件的任何东西,改天再测放到yml里
EmailUtils
package com.demo.springboot.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.commons.collections.map.HashedMap;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.ui.velocity.VelocityEngineUtils;
public class EmailUtils{
private JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
private VelocityEngine velocityEngine = new VelocityEngine();
List<String> tos = new ArrayList<String>();
private String username = "***@zatxtech.com";
private String pass = "******";
private void init(){
//普通qq配置
//设置邮箱主机 若使用企业邮箱则为smtp.exmail.qq.com
//mailSender.setHost("smtp.qq.com");
//设置用户名
//mailSender.setUsername("[email protected]");
//设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码 企业邮箱则为客户端专用密码,需绑定微信后设置
//mailSender.setPassword("oebjutrcnamahbhe");
//mailSender.setDefaultEncoding("UTF-8");
//腾讯企业邮箱配置
mailSender.setHost("smtp.exmail.qq.com");
mailSender.setUsername(username);
mailSender.setPassword(pass);
mailSender.setDefaultEncoding("UTF-8");
}
/**
* 发送一条简单的邮件
* @param to
* @param subject
* @param content
*/
@Async
public void sendSimpleMail(String to, String subject, String content) {
init();
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(username);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
mailSender.send(message);
System.out.println("发送成功~");
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("发送失败~");
}
}
/**
* 发送一个HTML邮件
* @param to 发送地址
* @param subject 主题
* @param HTMLContent 内容《HTML格式》
*/
public void sendHtmlMail(String to, String subject, String HTMLContent) {
init();
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(username);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(HTMLContent, true);
System.out.println("发送成功~");
} catch (MessagingException e) {
e.printStackTrace();
System.out.println("发送失败~");
}
mailSender.send(message);
}
/**
* 发送一个模板消息
* @param to 发送地址
* @param subject 标题
* @param data 模板数据
*/
public void sendTemplateMail(String to, String subject,HashMap data) {
init();
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(username);
helper.setTo(to);
helper.setSubject(subject);
Map<String, Object> model = new HashMap<String, Object>();
model.put("username", data.get("username"));
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "template.vm", "UTF-8", model);
helper.setText(text, true);
mailSender.send(mimeMessage);
System.out.println("发送成功~");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("发送失败~");
}
}
/**
* 获取touser列表
* @return
*/
public List<String> getToUsers(){
try {
Properties prop = new Properties();
//读取属性文件a.properties
InputStream in = EmailUtils.class.getClassLoader().
getResourceAsStream("email.properties");
// InputStream in = new BufferedInputStream (
// new FileInputStream("email.properties"));
prop.load(in); ///加载属性列表
Iterator<String> it=prop.stringPropertyNames().iterator();
while(it.hasNext()){
String key=it.next();
// System.out.println(key);
tos.add(key);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到email配置文件");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("读取异常");
}
return tos;
}
public static void main(String[] args) {
// new EmailUtils().sendSimpleMail("***@qq.com","hello!","hello!");
// StringBuffer sb = new StringBuffer();
// sb.append("<h1>大标题-h1</h1>")
// .append("<p style='color:#F00'>红色字</p>")
// .append("<p style='text-align:right'>右对齐</p>");
// new EmailUtils().sendHtmlMail("***@qq.com", "HTML", sb.toString());
// HashMap<String, String> map = new HashMap<String, String>();
// map.put("username", "ddd");
// new EmailUtils().sendTemplateMail("***@qq.com", "Template", map);
}
}
3、email.properties
***@zatxtech.com
***@zatxtech.com
***@zatxtech.com