SpringBoot开发六-发送邮件
需求介绍—发送邮件
首先要进行邮箱设置,要启用客户端SMTP
服务。
而且SpringBoot
也给了JavaMailSender
发送邮件。
代码实现
首先你需要设置好邮箱,步骤百度一大堆,记住要配置一个授权码,是需要在后续进行配置的password
。
然后就是正式的来写了。
首先引入一个jar
包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.1.6.RELEASE</version> </dependency>
然后在application.properties
进行邮箱的配置
# MailProperties spring.mail.host=smtp.sina.com spring.mail.port=465 spring.mail.username=*** spring.mail.password=*** spring.mail.protocol=smtps spring.mail.properties.smtp.auth=true spring.mail.properties.mail.smtp.ssl.enable=true
那么这个邮箱发送的话我们就建一个工具类MailClient
,实现能够复用。
package com.nowcoder.community.util; 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; @Component public class MailClient { private static final Logger logger = LoggerFactory.getLogger(MailClient.class); @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String from; /** * @param to: 发送目标 * @param subject: 标题 * @param content: 内容 */ 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); // 允许支持html文件,默认是文本 helper.setText(content, true); mailSender.send(helper.getMimeMessage()); } catch (MessagingException e) { logger.error("发送邮件失败:" + e.getMessage()); } } }
那么我们写一个测试类测试一下MailTests
:
package com.nowcoder.community; 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; @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("***@163.com", "TEST", "Welcome."); } @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("***@163.com", "HTML", content); } }
<!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>