<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>mail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mail</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
# 邮件的配置
spring.mail.host=smtp.qq.com
spring.mail.port=465
# 邮件发送人
spring.mail.username=xxx@qq.com
# 使用自己的授权码
spring.mail.password=ontqrqxpjkysfhbb
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.test-connection=true
@Autowired
JavaMailSender javaMailSender;
//发送简单邮件
@Test
void contextLoads() throws Exception{
SimpleMailMessage message = new SimpleMailMessage();
//邮件发送人,注意这个邮件发送人要和application.properties中的username一致
message.setFrom("xxx@qq.com");
//邮件接收人
message.setTo("xxxx@qq.com");
message.setSentDate(new Date());
//邮件主题
message.setSubject("测试邮件主题-测试邮件");
//邮件内容
message.setText("测试邮件内容-测试邮件");
javaMailSender.send(message);
}
@Autowired
JavaMailSender javaMailSender;
//发送带附件的邮件
@Test
void test() throws MessagingException {
File file = new File("D://test.xlsx");
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("xxx@qq.com");
helper.setTo("xxxx@qq.com");
helper.setSentDate(new Date());
helper.setSubject("测试带附件的邮件主题-测试邮件");
helper.setText("测试带附件的邮件内容-测试邮件");
helper.addAttachment(file.getName(),file);
javaMailSender.send(mimeMessage);
}
@Autowired
JavaMailSender javaMailSender;
//发送带图片的邮件
@Test
void test1() throws MessagingException {
File file = new File("D://test//img/1.jpg");
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("xxx@qq.com");
helper.setTo("xxxx@qq.com");
helper.setSentDate(new Date());
helper.setSubject("测试带图片的邮件-主题");
helper.setText("<div>测试带图片的邮件内容</div> <div><img src = 'cid:p01'/></div>",true);
helper.addInline("p01",file);
javaMailSender.send(mimeMessage);
}
package com.yl.mail.domain;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String company;
private String position;
private Integer salary;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", company='" + company + '\'' +
", position='" + position + '\'' +
", salary=" + salary +
'}';
}
}
<div>欢迎 ${username} 加入 ${company} 大家庭,您的入职信息如下:</div>
<table border="1">
<tr>
<td>姓名</td>
<td>${username}</td>
</tr>
<tr>
<td>职位</td>
<td>${position}</td>
</tr>
<tr>
<td>薪水</td>
<td>${salary}</td>
</tr>
</table>
<div style="color: greenyellow;font-size: large">微风吹拂,杨柳依依</div>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>欢迎 <span th:text="${username}"></span> 加入 <span th:text="${company}"></span> 大家庭,您的入职信息如下:</div>
<table border="1">
<tr>
<td>姓名</td>
<td th:text="${username}"></td>
</tr>
<tr>
<td>职位</td>
<td th:text="${position}"></td>
</tr>
<tr>
<td>薪水</td>
<td th:text="${salary}"></tdth:text>
</tr>
</table>
<div style="color: greenyellow;font-size: large">微风吹拂,杨柳依依</div>
</body>
</html>
@Autowired
JavaMailSender javaMailSender;
//使用freemarker模板发送邮件
@Test
void test2() throws MessagingException, IOException, TemplateException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("xxx@qq.com");
helper.setTo("xxxx@qq.com");
helper.setSentDate(new Date());
helper.setSubject("测试邮件主题(freemarker)-测试邮件");
Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);
cfg.setClassForTemplateLoading(MailApplicationTests.class,"/mail");
Template template = cfg.getTemplate("mail.ftl");
User user = new User();
user.setUsername("root");
user.setCompany("xxx软件开发公司");
user.setPosition("软件开发工程师");
user.setSalary(11000);
StringWriter writer = new StringWriter();
template.process(user,writer);
String text = writer.toString();
System.out.println("邮件内容:" + text);
helper.setText(text,true);
javaMailSender.send(mimeMessage);
}
@Autowired
JavaMailSender javaMailSender;
@Autowired
TemplateEngine templateEngine;
//使用thymeleaf模板发送邮件
@Test
void test3() throws MessagingException, IOException, TemplateException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("xxx@qq.com");
helper.setTo("xxxx@qq.com");
helper.setSentDate(new Date());
helper.setSubject("测试邮件主题(thymeleaf)-测试邮件");
Context context = new Context();
context.setVariable("username","admin");
context.setVariable("company","xxx中小型公司");
context.setVariable("position","前端开发工程师");
context.setVariable("salary",10000);
String text = templateEngine.process("mail.html", context);
System.out.println("邮件内容:" + text);
helper.setText(text,true);
javaMailSender.send(mimeMessage);
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_41359273/article/details/122973345
内容来源于网络,如有侵权,请联系作者删除!