在项目中,有多种场合需要用到邮件发送功能,比如说OA系统中,给用户创建了账号后,给用户的邮箱发送账号信息,或者在系统报错的情况下,向开发人员发送错误信息方便及时进行错误排查等等。
在Springboot项目中,整合邮件发送功能非常简单方便,只需要在项目中添加spring-boot-starter-mail这个jar包后再进行基本信息配置就可以了。
本文以QQ邮件为示例,接下来看看项目的配置流程:
开发工具:idea
1、开启QQ邮箱的POP3/SMTP服务:
登录QQ邮箱,点击页面上的设置按钮后,进入邮箱设置的账户,在下方找到POP3/SMTP服务,点击开启,然后按照提示进行就可以了,设置完成后,将会获取一个授权密码,这个密码将会用在后面的开发中,操作流程如下:
2、项目结构:
3、pom文件引入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hongke.mail</groupId>
<artifactId>mailApplication</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<description>this is mail demo</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.report.outputEncoding>UTF-8</project.report.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--邮件发送jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>
4、创建配置文件application.yml:
server:
port: 9001
spring:
mail:
default-encoding: UTF-8
host: smtp.qq.com
port: 587
username: 用来发送邮件的邮箱
password: 开启POP3/SMTP是获取的授权码
properties:
mail:
stmp:
ssl:
enable: true
socketFactory:
class: javax.net.ssl.SSLSocketFactory
imap:
ssl:
socketFactory:
fallback: false
debugger: true
注意:25端口禁止使用,25是SMTP服务默认端口,主要用途是用于邮件发送,有些攻击就是对默认端口进行,同时禁用也是为了防止垃圾邮件,不让邮件泛滥。在阿里云服务器中可以解封25端口,详细操作不在本文介绍范围内,请参考https://developer.aliyun.com/article/701303。
5、创建发送邮件工具类SendMailUtils:
package com.hongke.mail.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
/**
* @author chengjunyu
* @classname SendMailUtils
* @description TODO
* @date 2020/7/23 14:15
*/
@Component
public class SendMailUtils {
@Autowired
JavaMailSender mailSender;
/**
* @description
* @author chengjunyu
* @date 2020/7/23 14:17
* @param formUser 发件人
toUser 收件人
ccUser 抄送人
subject 邮件主题
content 邮件内容
* @return void
**/
public void sendEmail(String formUser, String toUser, String ccUser, String subject, String content) {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(formUser);
mailMessage.setTo(toUser);
mailMessage.setCc(ccUser);
mailMessage.setSubject(subject);
mailMessage.setText(content);
mailSender.send(mailMessage);
}
}
解释:JavaMailSender是springboot在MailSenderPropertiesConfiguration类中配置好的,该类在Mail的自动配置类MailSenderAutoConfiguration中导入,因此此处可以直接注入JavaMailSender进行使用。
6、创建controller层、service层和serviceImpl文件:
UserService:
package com.hongke.mail.service;
/**
* @author chengjunyu
* @classname UserService
* @description TODO
* @date 2020/7/23 14:12
*/
public interface UserService {
void addUser(String userName, String password);
}
UserServiceImpl:
package com.hongke.mail.service.impl;
import com.hongke.mail.service.UserService;
import com.hongke.mail.utils.SendMailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author chengjunyu
* @classname UserServiceImpl
* @description TODO
* @date 2020/7/23 14:13
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
SendMailUtils sendMailUtils;
@Override
public void addUser(String userName, String password) {
//发件人邮箱,和application.yml文件中配置的spring.mail.username一致即可
String fromUser = "xxx@foxmail.com";
//收件人邮箱地址
String toUser = "xxx@qq.com";
//抄送人邮箱
String cc = "xxx@qq.com";
String subject = "用户创建成功";
String content = "您的账户已经常见成功,账户名:" + userName + ",登录密码:" + password;
try {
System.out.println("准备发送邮件");
sendMailUtils.sendEmail(fromUser, toUser, cc, subject, content);
System.out.println("邮件发送成功");
}catch (Exception e) {
System.out.println("请求失败,将发送邮件给开发人员");
sendMailUtils.sendEmail(fromUser, toUser, cc, subject, e.toString());
System.out.println("邮件发送成功");
}
}
}
UserController:
package com.hongke.mail.controller;
import com.hongke.mail.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author chengjunyu
* @classname UserControlelr
* @description TODO
* @date 2020/7/23 14:30
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
UserService userService;
@PostMapping("/addUser")
public void addUser(String userName, String password) {
userService.addUser(userName, password);
}
}
7、创建启动类MailApplication :
package com.hongke.mail;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author chengjunyu
* @classname MailApplication
* @description TODO
* @date 2020/7/23 14:09
*/
@SpringBootApplication
public class MailApplication {
public static void main(String[] args) {
SpringApplication.run(MailApplication.class);
}
}
8、Postman测试-127.0.0.1:9001/user/addUser
请求发送后,就可以在收件人和抄送人邮箱中看到发送的邮件主题和内容了。