- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
- 定义邮件服务接口
import com.baomidou.mybatisplus.extension.service.IService;
public interface IMailService {
/**
* 异步发送纯文本的简单邮件
* @param subject
* @param content
*/
public void sendSimpleMail(String subject, String content);
/**
* 异步发送纯文本的简单邮件
* @param to
* @param subject
* @param content
*/
public void sendSimpleMail(String to, String subject, String content);
}
- 定义邮件服务接口实现类
在方法上添加@Async 实现方法的异步处理
import com.goldnurse.commons.mail.IMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Slf4j
@Service
public class MailServiceImpl implements IMailService {
@Resource
private JavaMailSender sender;
@Value("${spring.mail.username}")
private String from;
@Value("${goldnurse.commons.config.mail.enable}")
private boolean send_mail_enable;
@Value("${goldnurse.commons.config.mail.address}")
private String send_mail_address;
/**
* 发送纯文本的简单邮件
* @param subject
* @param content
*/
@Async
@Override
public void sendSimpleMail(String subject, String content){
this.sendSimpleMail(send_mail_address,subject,content);
}
/**
* 发送纯文本的简单邮件
* @param to
* @param subject
* @param content
*/
@Async
@Override
public void sendSimpleMail(String to, String subject, String content){
if (send_mail_enable) {
SimpleMailMessage message = new SimpleMailMessage();
String[] toRecive = to.split(",");
message.setFrom(from);
message.setTo(toRecive);
message.setSubject(subject);
message.setText(content);
try {
sender.send(message);
log.info("简单邮件已经发送。");
} catch (Exception e) {
log.error("发送简单邮件时发生异常!", e);
}
}
}
}
- application.yml 邮件参数
- 企业邮件地址如下
- host、username、password 注意需修改
- enable true时发送邮件,false 时不发送邮件
- address 接收邮件的地址,可配置多个,使用逗号隔开
spring:
mail:
host: smtp.goldnurse.com
username: service@goldnurse.com
password: 99999999
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
goldnurse:
commons:
config:
mail:
enable: true
address: fengrz@goldnurse.com,fengrz@goldnurse.com
- 开启多线程
在主函数中添加@EnableAsync 开启多线程
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@EnableAsync
public class SurveyWebApplication {
public static void main(String[] args) {
SpringApplication.run(SurveyWebApplication.class, args);
}
}
- 调用并测试
- 注入
@Autowired
IMailService iMailService;
- 发送邮件
iMailService.sendSimpleMail("访问异常", "消息内容");