SpringBoot集成email
1. 导入Maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.编写properties配置文件
spring.mail.username=1318248167@qq.com
spring.mail.password=zpsodspfdmaqjcbh //密码
spring.mail.host=smtp.qq.com
#qq需要开启加密认证
spring.mail.properties.mail.smtp.ssl.enable=true
3. 编写测试类
package com.qilin;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class ShiroSpringBootApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("测试标题");
mailMessage.setText("测试内容");
mailMessage.setTo("1318248167@qq.com");
mailMessage.setFrom("1318248167@qq.com");
}
@Test
void contextLoads2() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("测试标题");
helper.setText("<p style='color:red'>测试内容</p>",true);
helper.addAttachment("1.jpg",new File("C:\\Users\\ASUS\\Pictures\\表情包\\1.jpg"));
helper.addAttachment("2.jpg",new File("C:\\Users\\ASUS\\Pictures\\表情包\\1.jpg"));
helper.setTo("1318248167@qq.com");
helper.setFrom("1318248167@qq.com");
mailSender.send(mimeMessage);
}
}