springboot3——Email
【摘要】 maven导入包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.1.6.RELEASE</version> </dependency>
参数配置:
# MailP...
maven导入包:
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-mail</artifactId>
-
<version>2.1.6.RELEASE</version>
-
</dependency>
参数配置:
-
# MailProperties
-
spring.mail.host=smtp.sina.com
-
spring.mail.port=465
-
spring.mail.username=hebtu_ftz@sina.com
-
spring.mail.password=c33ce75afaff8015
-
spring.mail.protocol=smtps
-
spring.mail.properties.mail.smtp.ssl.enable=true
-
spring.mail.properties.mail.smtl.auth=true
-
spring.mail.properties.mail.smtp.starttls.enable=true
-
spring.mail.properties.mail.smtp.starttls.required=true
写发送工具类:
-
package com.now.community.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;
-
-
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(helper.getMimeMessage());
-
} catch (MessagingException e) {
-
logger.error("发送邮件失败:" + e.getMessage());
-
}
-
}
-
}
测试类:
-
package com.now.community.community;
-
-
import com.now.community.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("450427951@qq.com","TEST","welcome");
-
}
-
-
@Test
-
public void testHtmlMail(){
-
Context context=new Context();
-
context.setVariable("username","fantianzuo");
-
String content=templateEngine.process("/mail/demo",context);
-
System.out.println(content);
-
mailClient.sendMail("450427951@qq.com","HTML",content);
-
}
-
}
html:
-
<!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>
文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。
原文链接:fantianzuo.blog.csdn.net/article/details/102689621
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)