基本步骤:
- 添加pom文件依赖
- 配置邮件相关
- 书写代码
- 测试
添加pom文件依赖
<!-- springboot mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置邮件相关
配置application.yml相关
spring:
mail:
host: smtp.aliyun.com
username: xxxxxx@aliyun.com
password: xxxxxx
properties:
mail:
smtp:
auth: true
starttls:
enable: false
required: false
书写代码
实体类
package com.ahut.email;
import java.io.Serializable;
import java.util.HashMap;
public class Email implements Serializable
/**
* 序列化
*/
private static final long serialVersionUID = 1L;
// 必填参数
private String email;// 接收方邮件
private String subject;// 主题
private String content;// 邮件内容
// 选填
private String template;// 模板
private HashMap<String, String> kvMap;// 自定义参数
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
public HashMap<String, String> getKvMap() {
return kvMap;
}
public void setKvMap(HashMap<String, String> kvMap) {
this.kvMap = kvMap;
}
}
工具类
package com.ahut.email;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class MailUtil
private Logger logger = LoggerFactory.getLogger(MailUtil.class);
private ScheduledExecutorService service = Executors.newScheduledThreadPool(6);
private final AtomicInteger count = new AtomicInteger(1);
public void start(final JavaMailSender mailSender, final SimpleMailMessage message) {
service.execute(new Runnable() {
@Override
public void run() {
try {
if (count.get() == 2) {
service.shutdown();
logger.info("the task is down");
}
logger.info("start send email and the index is " + count);
mailSender.send(message);
logger.info("send email success");
} catch (Exception e) {
logger.error("send email fail", e);
}
}
});
}
public void startHtml(final JavaMailSender mailSender, final MimeMessage message) {
service.execute(new Runnable() {
@Override
public void run() {
try {
if (count.get() == 2) {
service.shutdown();
logger.info("the task is down");
}
logger.info("start send email and the index is " + count);
mailSender.send(message);
logger.info("send email success");
} catch (Exception e) {
logger.error("send email fail", e);
}
}
});
}
}
发送类
package com.ahut.email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import com.ahut.action.LoginAction;
/**
*
* @ClassName: MailService
* @Description: 发送邮件
* @author cheng
* @date
@Service
public class MailService
private Logger log = LoggerFactory.getLogger(LoginAction.class);
@Autowired
private JavaMailSender mailSender;// 执行者
@Value("${spring.mail.username}")
public String USER_NAME;// 发送者
public void send(Email mail) throws Exception {
log.info("发送邮件");
MailUtil mailUtil = new MailUtil();
SimpleMailMessage message = new
测试
package mail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.ahut.AhutApplication;
import com.ahut.email.Email;
import com.ahut.email.MailService;
@SpringBootTest(classes = AhutApplication.class)
@RunWith(SpringRunner.class)
public class MailTest {
@Autowired
private MailService mailService;
@Test
public void testSend() throws Exception {
Email mail = new Email();
mail.setEmail("xxxxx@163.com");
mail.setSubject("注册验证");
mail.setContent("多发一封应该没事");
mailService.send(mail);
详细请参考这位博主的文章:SpringBoot开发案例之整合mail发送服务