最近重写一个邮件服务来给告警业服务用,做成一个内置的应用。直接使用 spring 的 javamail 来实现。
公司使用腾讯企业邮箱。
这个例子正常使用,可以直接套上使用。
邮件配置
以下配置方式二选一
- 代码方式
- yml方式
代码方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package com.liukai.springmail.send;
import org.springframework.beans.factory.annotation.Configurable; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.mail.MailProperties; import org.springframework.context.annotation.Bean; import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.HashMap; import java.util.Map; import java.util.Properties;
@Configurable @ConditionalOnProperty(prefix = "spring.mail", name = "host") public class MailSenderConfig { private final MailProperties properties;
MailSenderConfig(MailProperties properties) { this.properties = properties; }
@Bean @ConditionalOnMissingBean public JavaMailSenderImpl mailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setDefaultEncoding("UTF-8"); sender.setHost("smtp.qq.com"); sender.setPassword("12345678"); sender.setUsername("test@qq.com"); sender.setPort(25); Properties javaMailProperties = sender.getJavaMailProperties();
Map<String, String> smtp = new HashMap<>(); smtp.put("socketFactoryClass", "javax.net.ssl.SSLSocketFactory"); Map<String, Object> mail = new HashMap<>(); mail.put("smtp", smtp); javaMailProperties.put("mail", mail); return sender; } }
|
yml方式
如果配置使用yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| spring: mail: host: smtp.qq.com username: test@qq.com password: 12345678 port: 25 default-encoding: UTF-8 properties: mail: smtp: socketFactoryClass: javax.net.ssl.SSLSocketFactory
|
maven 依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <?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 http://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.6.0</version> </parent>
<groupId>com.liukai.spring.mail</groupId> <artifactId>com.liukai.springboot-mail</artifactId> <version>1.0-SNAPSHOT</version>
<url>https://www.liukay.com</url> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>2.2.2.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.2.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> </project>
|
测试发送邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.liukai.springmail.send;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource; import java.util.Date;
@RunWith(SpringRunner.class) @SpringBootTest public class Sender {
@Resource private JavaMailSender javaMailSender;
@Test public void sendSimpleMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("这是一封测试邮件"); message.setFrom("test@qq.com"); message.setTo("liukai@163.com");
message.setSentDate(new Date()); message.setText("这是测试邮件的正文2"); javaMailSender.send(message); } }
|