Spring发送eMail
邮件发送准备
导入mail以及spring context的依赖支持。
POM文件
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| <?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>
<groupId>com.ooyhao.spring</groupId> <artifactId>spring-in-action-19-01</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging>
<name>spring-in-action-19-01 Maven Webapp</name> <url>http://www.example.com</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.9.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.1.8.RELEASE</version> </dependency>
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.9.RELEASE</version> </dependency>
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency>
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.0.Alpha3</version> </dependency>
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency>
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.9.RELEASE</version> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.6.RELEASE</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency>
<dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.11</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.20</version> </dependency>
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency>
</dependencies>
<build> <finalName>spring-in-action-19-01</finalName> <pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
|
配置JavaMailSender
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @ComponentScan(basePackages = "com.ooyhao.spring", useDefaultFilters = true, excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class), @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = RestController.class) } ) public class RootConfig { @Bean public JavaMailSender javaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.163.com"); mailSender.setUsername("ooyhao1996@163.com"); mailSender.setPassword("163mailcode"); mailSender.setDefaultEncoding("utf-8"); return mailSender; } }
|
项目结构图
发送简单邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Service public class SendMailService {
@Autowired private JavaMailSender mailSender;
public void sendSimpleMail(){ SimpleMailMessage message = new SimpleMailMessage(); message.setFrom("ooyhao1996@163.com"); message.setTo("3459649791@qq.com","625194996@qq.com"); message.setSubject("起床了"); message.setText("起床啦"); message.setSentDate(new Date()); mailSender.send(message); } }
|
发送带有附件的邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Service public class SendMailService {
@Autowired private JavaMailSender mailSender;
public void sendMailWithAttachment() throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setFrom("ooyhao1996@163.com"); helper.setTo("625194996@qq.com"); helper.setSentDate(new Date()); helper.setSubject("带有附件的邮件"); helper.setText("这是一封具有附件的邮件"); ClassPathResource resource1 = new ClassPathResource("images/123.png"); helper.addAttachment("123.png",resource1); ClassPathResource resource2 = new ClassPathResource("images/456.png"); helper.addAttachment("456.png",resource2); mailSender.send(mimeMessage); } }
|
发送HTML邮件
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
| @Service public class SendMailService {
@Autowired private JavaMailSender mailSender;
public void sendHtmlMail() throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setFrom("ooyhao1996@163.com"); helper.setTo("625194996@qq.com"); helper.setSentDate(new Date()); helper.setSubject("HTML邮件"); helper.setText("<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "\t<title>HTML邮件</title>\n" + "</head>\n" + "<body>\n" + "\t<h1>这是一份HTML邮件</h1>\n" + "\t<img src=\"cid:logo\">\n" + "</body>\n" + "</html>",true); ClassPathResource resource = new ClassPathResource("res/images/logo.png"); helper.addInline("logo",resource); mailSender.send(mimeMessage); } }
|
发送模板邮件
使用模板邮件发送时,需要配置模板解析器和模板引擎。
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
| @Bean public ClassLoaderTemplateResolver emailTemplateResolver(){ ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); resolver.setPrefix("mail/"); resolver.setTemplateMode(TemplateMode.HTML); resolver.setCharacterEncoding("utf-8"); return resolver; }
@Bean public SpringResourceTemplateResolver templateResolver(){ SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setTemplateMode(TemplateMode.HTML); resolver.setSuffix("/WEB-INF/templates/"); resolver.setSuffix(".html"); resolver.setCharacterEncoding("utf-8"); return resolver; }
@Bean public SpringTemplateEngine templateEngine(Set<ITemplateResolver> templateResolvers){ SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolvers(templateResolvers); return engine; }
|
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
| @Service public class SendMailService {
@Autowired private JavaMailSender mailSender;
@Autowired private SpringTemplateEngine springTemplateEngine; public void sendTemplateHTML() throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setFrom("ooyhao1996@163.com"); helper.setTo("625194996@qq.com"); helper.setSentDate(new Date()); helper.setSubject("HTML邮件"); Context context = new Context(); context.setVariable("firstTitle","这是一封模板生成的邮件"); context.setVariable("secondTitle","模板邮件|ouyanghao"); context.setVariable("src","https://ooyhao.gitee.io/vuepress-blog/logo.png"); String emailText = springTemplateEngine.process("emailTemplate.html", context); helper.setText(emailText,true); mailSender.send(mimeMessage); } }
|
HTML模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1 th:text="${firstTitle}">一级标题</h1> <h2 th:text="${secondTitle}">二级标题</h2> <img th:src="${src}">
</body> </html>
|
发送HTML(Thymeleaf)模板邮件时,需要注意一些细节,比如上述代码中的图片显示,则需要用一张外网可以访问到的图片,否则邮件中无法正常显示。
总结:
本节重要是练习如何使用Spring中的简单方法来实现邮件发送。并且包含了简单邮件、附件邮件、HTML邮件以及模板邮件。