Springboot + swagger send email (text format and html format) mail
I am using the 126 mailbox here. It can be said that I am a loyal user of 126. The mailbox I applied for in 2006. I have been using it now. Thanks NetEase, thank you 126!
1. Turn on the service and get the authorization password
Settings==>Enable==>Select to send SMS verification==>Generate authorization code
2. Directory structure
config: Place the configuration class of swagger
controller: program entry
3. Introduce pom dependency
<?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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.email</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok ********************* Begin -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok ********************* End -->
<!-- swagger ********************* Begin -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger ********************* End -->
<!-- email ********************* Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- email ********************* End -->
<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.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4. Configure the properties file
Configure the parameters for sending mail here, and inject the JavaMailSender instance by himself
server.port=2080
# SMTP服务器
spring.mail.host=smtp.126.com
# 邮箱账号
spring.mail.username=zjylove2006@126.com
# 授权密码,并非邮箱密码
spring.mail.password=刚才生成的授权码
# 邮箱协议类型
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
5.SwaggerConfig
package com.email.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.email.demo.controller"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("swagger接口文档")
.version("1.0")
.build();
}
}
6.EmailController
package com.email.demo.controller;
import io.swagger.annotations.Api;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
@Api(value = "发送邮件", tags = {
"发送邮件"})
@Slf4j
@Validated
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private JavaMailSender javaMailSender;
/**
* 发件人
*/
@Value("${spring.mail.username}")
private String from;
@PostMapping("/send")
public String send(@RequestBody SimpleMailMessage message){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
message.setFrom(from);
javaMailSender.send(message);
log.info("发送人为:{}, 收件人为:{}, 发送标题为:{}, 发送内容为:{}, 发送时间为:{}",
from,
message.getTo(),
message.getSubject(),
message.getText(),
time);
return "发送成功!!!";
}
/**
* 发送html格式
* @return
*/
@PostMapping("/sendHtml")
public String sendHtml() throws Exception{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = format.format(new Date());
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setSubject("日志");
messageHelper.setFrom(from);
messageHelper.setTo("[email protected]");
messageHelper.setText("<a href=\"https://blog.csdn.net/dayonglove2018/article/details/106784064\">老周的博客</a>", true);
javaMailSender.send(messageHelper.getMimeMessage());
log.info("发送人为:{}, 收件人为:{}, 发送标题为:{}, 发送内容为:{}, 发送时间为:{}",
from, "[email protected]", "日志", messageHelper.getMimeMessage(),
time);
return "发送成功!!!";
}
}
7. Test
1. Start the program and access the swagger interface:
http://localhost:2080/swagger-ui.html#
2. Enter the test parameters:
{
"subject": "日志",
"text": "springboot + mybatis-plus整合quartz实现定时任务(超详细,附带sql)",
"to": "[email protected]"
}
3. Test results
1. Test sending text mail
Swagger returns the result: the
background console prints the result: the
email has also been received: the
test is OK!!!
2. Test sending html mail
Swagger returns the result: the
console returns the result: the
received email is: the
test is OK!!!
welcome the big guys to leave a comment and learn together!!! thank you!!!
===========================
Original article, reprinted with the source!