在PHP版本的博客中,留言板收到留言以及文章收到评论。都会通过邮箱给我发一个邮件提醒我,那么在java版本中自然也是少不了这个功能,这里我来记录一下,SpringBoot使用QQ邮箱stmp发送邮件。
如何申请qq邮箱的stmp服务请移步《QQ邮箱开启SMTP服务》
一:添加maven发送邮件依赖
在pom.xml配置文件中加入 spring-boot-starter-mail 依赖。
<!-- 发送邮件库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二:在yml配置文件中添加邮箱配置参数
spring:
# 邮箱配置
mail:
# 使用谁家的邮箱发邮件
host: smtp.qq.com
# 端口号,465对应smtps 587对应stmp
port: 465
protocol: smtps
# 发送邮件邮箱
username: guanchao_gc@qq.com
# 秘钥
password: yyfhjeykkdrlbgaa
# 发送邮件邮箱
from: guanchao_gc@qq.com
# 字符集
default-encoding: UTF-8
三:编写一个邮箱工具类
Email.java
package com.springbootblog.utils;
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.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class Email
{
private final Logger logger = LoggerFactory.getLogger(this.getClass());
//Spring Boot 提供了一个发送邮件的简单抽象,使用的是下面这个接口,这里直接注入即可使用
@Autowired
private JavaMailSender mailSender;
// 配置文件中我的qq邮箱
@Value("${spring.mail.from}")
private String from;
/**
* 简单文本邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
public void sendSimpleMail(String to, String subject, String content)
{
// System.out.println(from);
// System.out.println(mailSender);
// System.out.println(to);
// System.out.println(subject);
// System.out.println(content);
//创建SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//邮件发送人
message.setFrom(from);
//邮件接收人
message.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
message.setText(content);
//发送邮件
mailSender.send(message);
}
/**
* html邮件
* @param to 收件人,多个时参数形式 :"xxx@xxx.com,xxx@xxx.com,xxx@xxx.com"
* @param subject 主题
* @param content 内容
*/
public void sendHtmlMail(String to, String subject, String content)
{
//获取MimeMessage对象
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try
{
messageHelper = new MimeMessageHelper(message, true);
//邮件发送人
messageHelper.setFrom(from);
//邮件接收人,设置多个收件人地址
InternetAddress[] internetAddressTo = InternetAddress.parse(to);
messageHelper.setTo(internetAddressTo);
//messageHelper.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容,html格式
messageHelper.setText(content, true);
//发送
mailSender.send(message);
//日志信息
logger.info("邮件已经发送。");
}
catch (Exception e)
{
logger.error("发送邮件时发生异常!", e);
}
}
/**
* 带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
public void sendAttachmentsMail(String to, String subject, String content, String filePath)
{
MimeMessage message = mailSender.createMimeMessage();
try
{
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
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 (Exception e)
{
logger.error("发送邮件时发生异常!", e);
}
}
}
上边的工具类中我封装了多种方法,但是我这里只用到了简单的文本发送,其余的功能我也没有用到。其他的功能有同学需要你们自己研究。
四:调用
这部分就比较简单了,但是也是需要有一个点要注意。
发送邮件的工具类email.java需要通过@Resource注解来注入到使用类中。
实例代码如下:
@Resource
private Email emailObj;
public Map<String, Object> putComment() throws Exception {
mailto = "805795955@qq.com"; //收件人
subject = "时间里的博客评论"; //邮件主题
body = "您在时间里的博客《" + articleInfo.getArttitle() + "》文章收到评论,内容为“" +content+ "”"; //邮件内容
emailObj.sendSimpleMail(mailto,subject,body);
Map<String, Object> result = new HashMap<>(2);
result.put("code", 1);
result.put("msg", "操作成功!");
return result;
}
至此,在我的项目中使用qq邮箱的stmp发送邮件就成功了。
有好的建议,请在下方输入你的评论。