利用springboot实现邮箱功能
1.导入依赖
- 去官网导(https://mvnrepository.com/)入依赖即可,如下图
- 如下,这个版本号可以不用写
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置application.properties
# mailproperties
# 邮箱配置
spring.mail.host=smtp.163.com
# 你的163邮箱
spring.mail.username=wysprime@163.com
# 注意这里不是邮箱密码,而是SMTP授权密码
spring.mail.password=授权码
spring.mail.port=25(邮箱不同,可能端口号也会不同)
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
3.编写邮件发送工具类MailClient
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.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* @author : wys
* @date : 2020-11-21 11:07
**/
@Component
public class MailClient {
private static final Logger logger = LoggerFactory.getLogger(MailClient.class);
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
public void sendMail(String to, String subject, String content){
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(message);
} catch (MessagingException e) {
logger.error("发送邮件失败!" + e.getMessage());
}
}
}
4.编写测试类
import com.nowcoder.community.util.MailClient;
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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
/**
* @author : wys
* @date : 2020-11-21 11:19
**/
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTests {
@Autowired
private MailClient mailClient;
@Autowired
private TemplateEngine templateEngine;
//测试普通文本
@Test
public void testTextMail(){
mailClient.sendMail("wysprime@qq.com", "TEST", "Welcome!");
}
//测试html文本
@Test
public void testHtmlMail(){
Context context = new Context();
context.setVariable("username", "sunday");
String content = templateEngine.process("/mail/demo", context);
System.out.println(content);
mailClient.sendMail("wysprime@qq.com", "HTML", content);
}
}
5.注意:使用html文本时要编写对应的thymeleaf模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件示例</title>
</head>
<body>
<p>欢迎您, <span style="color: red;" th:text="${username}"></span>!</p>
</body>
</html>
6.测试结果
-
普通文本
-
html