springboot简单模拟邮箱发送
- 在pom.xml中添加依赖
org.springframework.boot
spring-boot-starter-mail
- 然后可以通过springboot提供的工具类进行模拟邮件发送。
接口:
public interface MailService {
/**- 发送文本邮件
- @param to 收件人
- @param subject 主题
- @param content 内容
*/
void sendSimpleMail(String to, String subject, String content);
/**
* 发送HTML邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendHtmlMail(String to, String subject, String content);
/**
* 发送带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath);
}
3. 具体发送接口实现
@Service
public class MailServiceImpl implements MailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用
*/
@Autowired
private JavaMailSender mailSender;
/**
* 配置文件中的邮箱
*/
@Value("${spring.mail.from}")
private String from;
/**
* 简单文本邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
//创建SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//邮件发送人
message.setFrom(from);
//邮件接收人
message.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
message.setText(content);
//发送邮件
mailSender.send(message);
}
/**
* html邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
//获取MimeMessage对象
MimeMessage message = mailSender.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//邮件发送人
helper.setFrom(from);
//邮件接收人
helper.setTo(to);
//邮件主题
helper.setSubject(subject);
//邮件内容,html格式
helper.setText(content, true);
//发送
mailSender.send(message);
//日志信息
logger.info("邮件已经发送。");
}catch (MessagingException e){
logger.error("发送邮件时发生异常!",e);
}
}
/**
* 带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
//获取MimeMessage对象
MimeMessage message = mailSender.createMimeMessage();
try{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
//邮件发送人
helper.setFrom(from);
//邮件接收人
helper.setTo(to);
//邮件主题
helper.setSubject(subject);
//邮件内容,html格式
helper.setText(content, true);
//附件文件
FileSystemResource file = new FileSystemResource(new File(filePath));
//附件名字
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
//添加附件
helper.addAttachment(fileName, file);
//发送
mailSender.send(message);
//日志信息
logger.info("邮件已经发送。");
}catch (MessagingException e){
logger.error("发送邮件时发生异常!",e);
}
}
}
4. 对了,记得在spring的核心配置文件中进行相关邮件配置。
#邮件配置
#发送邮件服务器
spring.mail.host=smtp.163.com
#发送邮件的邮箱地址
spring.mail.username=@163.com
#客户端授权码,不是邮箱密码
spring.mail.password=RJUNSTDKWDMYWRLB
#465或994
spring.mail.properties.mail.smtp.port=994
#发送邮件地址,和username一致
spring.mail.from=@163.com
#可以任意
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=utf-8
5. 可以在main方法或者使用@test注解进行测试。
String to = “****@163.com”;
mailService.sendSimpleMail(to, “预警邮件” ,“主子,系统又炸了,请求紧急救援!”);