本想使用网易,结果使用465端口尝试了半天,老是提示无法连接,后面说是要开通vip,果断放弃,本次教程使用是qq邮箱,端口号是587,之所以这样是阿里云服务器无法使用25端口发送邮件了
废话不多说直接贴效果图
邮件发送的日志打印
pom文件
<!--引入junit单元测试依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--下面是springboot实现邮件发送的相关依赖--> <!--springboot整合邮件发送--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--引入thymeleaf依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--添加fastjson依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency>
springboot使用的版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <relativePath/> </parent>
配置文件
server.port=8081 spring.mail.host=smtp.qq.com spring.mail.port=587 [email protected] spring.mail.password=123 spring.mail.default-encoding=UTF-8 spring.mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.debug=true
package com.mail.service; import com.alibaba.fastjson.JSONObject; /** * @Description: * @Author: Yourheart * @Create: 2023/1/29 15:35 */ public interface MailService { /** * 邮件发送 * @param title * @param content * @param targetAddress */ void sendEmail(String title,String content,String targetAddress); /** * 使用thymeleaf的形式发送邮件 * @param jsonObject */ void sendThymeleafMail(JSONObject jsonObject); }
package com.mail.service.impl; import com.alibaba.fastjson.JSONObject; import com.mail.service.MailService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; import org.thymeleaf.context.Context; import org.thymeleaf.spring5.SpringTemplateEngine; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.util.Date; /** * @Description: * @Author: Yourheart * @Create: 2023/1/29 15:36 */ @Service @Slf4j public class UniversalServiceImpl implements MailService { @Autowired private JavaMailSender javaMailSender; @Autowired private SpringTemplateEngine templateEngine; @Value("${spring.mail.username}") private String fromMail; /** * 邮件发送 * * @param title * @param content * @param targetAddress */ @Override public void sendEmail(String title, String content, String targetAddress) { SimpleMailMessage msg = new SimpleMailMessage(); //构建一个邮件对象 msg.setSubject(title); // 设置邮件主题 msg.setFrom(fromMail); // 设置邮箱发送者 msg.setTo(targetAddress); // 设置邮件接收者,可以有多个接收者 msg.setSentDate(new Date()); // 设置邮件发送日期 msg.setText(content); // 设置邮件的正文 javaMailSender.send(msg); } /** * 使用thymeleaf的形式发送邮件 * * @param jsonObject */ @Override public void sendThymeleafMail(JSONObject jsonObject) { try { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setSubject(jsonObject.getString("title")); helper.setFrom(fromMail); helper.setTo(jsonObject.getString("toAddress")); helper.setSentDate(new Date()); Context context = new Context(); context.setVariable("bankName",jsonObject.getString("bankName")); context.setVariable("endCard",jsonObject.getString("endCard")); context.setVariable("times",jsonObject.getString("times")); context.setVariable("TheAmountOf",jsonObject.getString("TheAmountOf")); String process = templateEngine.process("mail.html",context); helper.setText(process,true); javaMailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } } }
测试类
package com.mail; import com.alibaba.fastjson.JSONObject; import com.mail.entity.MimeMessageDTO; import com.mail.service.MailService; import com.mail.service.SendMailsService; import lombok.Data; import lombok.extern.slf4j.Slf4j; 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.junit4.SpringRunner; import java.util.Date; import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class MailApplicationTest { @Autowired private MailService mailService; @Test public void test(){ //开始邮件发送 JSONObject jsonObject=new JSONObject(); jsonObject.put("title","信用卡电子账单"); jsonObject.put("toAddress","[email protected]"); jsonObject.put("bankName","测试001"); jsonObject.put("endCard","1234"); jsonObject.put("times","2023-01-29 15:44:00"); jsonObject.put("TheAmountOf","234"); mailService.sendThymeleafMail(jsonObject); } }
同时使用了页面
mail.html前端代码
<!DOCTYPE html> <html lang="zh-CN" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Thymeleaf邮件模板</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <h1><span th:text="${bankName}"></span>信用卡电子账单</h1> <table class="table"> <tr> <td>个人消费卡账户<span th:text="${endCard}"></span></td> <td>还款日<span th:text="${times}"></span></td> </tr> <tr> <td>已出账单</td> <td>¥<span th:text="${TheAmountOf}"></span> </td> </tr> </table> </div> </body> </html>
以上是全部代码
标签:springboot,jsonObject,mail,springframework,发送,org,import,com,邮件 From: https://www.cnblogs.com/q202105271618/p/17073915.html