慕课网《Spring Boot 发送邮件》学习总结html
第一部分:背景java
第二部分:实践git
邮件使用场景github
邮件发送原理web
邮件发送流程spring
Spring Boot介绍express
前置知识apache
开发流程并发
Hello World项目app
建立名为48-boot-mail-hello的maven工程pom以下
<?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"> <parent> <artifactId>48-boot-mail</artifactId> <groupId>com.myimooc</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>48-boot-mail-hello</artifactId> <properties> <spring.boot.version>2.0.4.RELEASE</spring.boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1.编写HelloService类
package com.myimooc.boot.mail.hello.service; import org.springframework.stereotype.Service; /** * <br> * 标题: Hello 服务<br> * 描述: Hello 服务<br> * 时间: 2018/09/08<br> * * @author zc */ @Service public class HelloService { public void sayHello(){ System.out.println("Hello World"); } }
2.编写HelloApplication类
package com.myimooc.boot.mail.hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * <br> * 标题: 启动类<br> * 描述: 启动类<br> * 时间: 2018/09/08<br> * * @author zc */ @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } }
3.编写HelloServiceTest类
package com.myimooc.boot.mail.hello.service; 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; /** * <br> * 标题: Hello 服务测试<br> * 描述: Hello 服务测试<br> * 时间: 2018/09/08<br> * * @author zc */ @RunWith(SpringRunner.class) @SpringBootTest public class HelloServiceTest { @Autowired private HelloService helloService; @Test public void sayHelloTest() { this.helloService.sayHello(); } }
简单文本邮件
建立名为48-boot-mail-mail的maven工程pom以下
<?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"> <parent> <artifactId>48-boot-mail</artifactId> <groupId>com.myimooc</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>48-boot-mail-mail</artifactId> <properties> <spring.boot.version>2.0.4.RELEASE</spring.boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
1.编写MailApplication类
package com.myimooc.boot.mail.mail; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * <br> * 标题: 启动类<br> * 描述: 启动类<br> * 时间: 2018/09/08<br> * * @author zc */ @SpringBootApplication public class MailApplication { public static void main(String[] args) { SpringApplication.run(MailApplication.class, args); } }
2.编写application.properties
#----------邮件发送配置 # 邮件发送协议 spring.mail.host=smtp.163.com # 用户名 spring.mail.username=zccodere@163.com # 受权码,并不是登陆密码 spring.mail.password=yourpassword # 默认编码 spring.mail.default-encoding=UTF-8
3.编写MailService类
package com.myimooc.boot.mail.mail.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; 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.standard.expression.MessageExpression; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; /** * <br> * 标题: 邮件服务<br> * 描述: 邮件服务<br> * 时间: 2018/09/08<br> * * @author zc */ @Service public class MailService { private Logger logger = LoggerFactory.getLogger(getClass()); /** * 发送人 */ @Value("${spring.mail.username}") private String from; /** * 注入JavaMailSender */ @Autowired private JavaMailSender mailSender; /** * 发送文本邮件 * * @param to 收件邮箱地址 * @param subject 主题 * @param content 内容 */ public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(content); message.setFrom(from); this.mailSender.send(message); } /** * 发送html邮件 * * @param to 收件邮箱地址 * @param subject 主题 * @param content 内容 * @throws Exception 异常 */ public void sendHtmlMail(String to, String subject, String content) throws Exception { MimeMessage message = this.mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); helper.setFrom(from); this.mailSender.send(message); } /** * 发送附件邮件 * * @param to 收件邮箱地址 * @param subject 主题 * @param content 内容 * @param filePaths 文件路径 * @throws Exception 异常 */ public void sendAttachmentsMail(String to, String subject, String content, String[] filePaths) throws Exception { MimeMessage message = this.mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file; for (String filePath : filePaths) { file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName, file); } helper.setFrom(from); this.mailSender.send(message); } /** * 发送图片邮件 * * @param to 收件邮箱地址 * @param subject 主题 * @param content 内容 * @param rscPath 图片路径 * @param rscId 图片ID */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) { logger.info("发送图片邮件开始:{},{},{},{},{}", to, subject, content, rscPath, rscId); MimeMessage message = this.mailSender.createMimeMessage(); MimeMessageHelper helper; try { helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, file); helper.setFrom(from); this.mailSender.send(message); logger.info("发送图片邮件成功!"); } catch (MessagingException ex) { logger.error("发送图片邮件异常:{}", ex); } } }
4.编写emailTemplate.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>邮件模版</title> </head> <body> <h3>你好,感谢您的注册,这是一封验证邮件,请点击下面的连接完成注册,感谢你你的支持!</h3><br/> <a href="#" th:href="@{http://www.ityouknow.com/register/{id}(id=${id})}">激活帐号</a> </body> </html>
5.编写MailServiceTest类
package com.myimooc.boot.mail.mail.service; 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 org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import static org.junit.Assert.*; /** * <br> * 标题: 邮件服务测试<br> * 描述: 邮件服务测试<br> * 时间: 2018/09/08<br> * * @author zc */ @RunWith(SpringRunner.class) @SpringBootTest public class MailServiceTest { /** * 收件邮箱地址 */ private static final String TO = "zccodere@163.com"; @Autowired private MailService mailService; @Test public void sendSimpleMail() { this.mailService.sendSimpleMail(TO, "这是第一封邮件", "你们好,这是个人第一封邮件"); } @Test public void sendHtmlMail() throws Exception { StringBuilder content = new StringBuilder(128); content.append("<html\n>"); content.append("<body>\n"); content.append("<h3> Hello World!这是一封Html邮件</h3>\n"); content.append("</body>\n"); content.append("</html>"); this.mailService.sendHtmlMail(TO, "这是一封html邮件", content.toString()); } @Test public void sendAttachmentsMail() throws Exception { String filePath = "d:\\48-boot-mail-hello.zip"; this.mailService.sendAttachmentsMail(TO, "这是一封带附件的邮件", "这是一封带附件的邮件内容", new String[]{filePath}); } @Test public void sendInlineResourceMail() { String rscPath = "d:\\thumb.jpg"; String rscId = "img001"; StringBuilder content = new StringBuilder(128); content.append("<html\n>"); content.append("<body>\n"); content.append("<h3> 这是有图片的邮件</h3>\n"); content.append("<img src=\'cid:" + rscId + "\'></img>\n"); content.append("<img src=\'cid:" + rscId + "\'></img>\n"); content.append("</body>\n"); content.append("</html>"); this.mailService.sendInlineResourceMail(TO, "这是一封带图片的邮件", content.toString(), rscPath, rscId); } @Autowired private TemplateEngine templateEngine; @Test public void sendTemplateMail() throws Exception { Context context = new Context(); context.setVariable("id", "006"); String emailContent = this.templateEngine.process("emailTemplate", context); this.mailService.sendHtmlMail(TO, "这是一封模版邮件", emailContent); } }
常见错误
邮件系统