Skip to content

Commit 9689c6b

Browse files
committed
实现邮件发送:简单邮件、附件邮件、嵌入资源的邮件、模板邮件
1 parent 31fd135 commit 9689c6b

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

Chapter4-5-1/pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter4-5-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter4-5-1</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-mail</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-velocity</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.mail.host=smtp.qq.com
2+
spring.mail.username=username@qq.com
3+
spring.mail.password=password
4+
spring.mail.properties.mail.smtp.auth=true
5+
spring.mail.properties.mail.smtp.starttls.enable=true
6+
spring.mail.properties.mail.smtp.starttls.required=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<h3>你好, ${username}, 这是一封模板邮件!</h3>
4+
</body>
5+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.didispace;
2+
3+
import org.apache.commons.collections.map.HashedMap;
4+
import org.apache.velocity.app.VelocityEngine;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.SpringApplicationConfiguration;
9+
import org.springframework.core.io.FileSystemResource;
10+
import org.springframework.mail.SimpleMailMessage;
11+
import org.springframework.mail.javamail.JavaMailSender;
12+
import org.springframework.mail.javamail.MimeMessageHelper;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.ui.velocity.VelocityEngineUtils;
15+
16+
import javax.mail.internet.MimeMessage;
17+
import java.io.File;
18+
import java.util.Map;
19+
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@SpringApplicationConfiguration(classes = Application.class)
23+
public class ApplicationTests {
24+
25+
@Autowired
26+
private JavaMailSender mailSender;
27+
@Autowired
28+
private VelocityEngine velocityEngine;
29+
30+
@Test
31+
public void sendSimpleMail() throws Exception {
32+
33+
SimpleMailMessage message = new SimpleMailMessage();
34+
message.setFrom("dyc87112@qq.com");
35+
message.setTo("dyc87112@qq.com");
36+
message.setSubject("主题:简单邮件");
37+
message.setText("测试邮件内容");
38+
39+
mailSender.send(message);
40+
}
41+
42+
@Test
43+
public void sendAttachmentsMail() throws Exception {
44+
45+
MimeMessage mimeMessage = mailSender.createMimeMessage();
46+
47+
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
48+
helper.setFrom("dyc87112@qq.com");
49+
helper.setTo("dyc87112@qq.com");
50+
helper.setSubject("主题:有附件");
51+
helper.setText("有附件的邮件");
52+
53+
FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
54+
helper.addAttachment("附件-1.jpg", file);
55+
helper.addAttachment("附件-2.jpg", file);
56+
57+
mailSender.send(mimeMessage);
58+
}
59+
60+
@Test
61+
public void sendInlineMail() throws Exception {
62+
63+
MimeMessage mimeMessage = mailSender.createMimeMessage();
64+
65+
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
66+
helper.setFrom("dyc87112@qq.com");
67+
helper.setTo("dyc87112@qq.com");
68+
helper.setSubject("主题:嵌入静态资源");
69+
helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);
70+
71+
FileSystemResource file = new FileSystemResource(new File("weixin.jpg"));
72+
helper.addInline("weixin", file);
73+
74+
mailSender.send(mimeMessage);
75+
}
76+
77+
@Test
78+
public void sendTemplateMail() throws Exception {
79+
80+
MimeMessage mimeMessage = mailSender.createMimeMessage();
81+
82+
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
83+
helper.setFrom("dyc87112@qq.com");
84+
helper.setTo("dyc87112@qq.com");
85+
helper.setSubject("主题:模板邮件");
86+
87+
Map<String, Object> model = new HashedMap();
88+
model.put("username", "didi");
89+
String text = VelocityEngineUtils.mergeTemplateIntoString(
90+
velocityEngine, "template.vm", "UTF-8", model);
91+
helper.setText(text, true);
92+
93+
mailSender.send(mimeMessage);
94+
}
95+
96+
}

Chapter4-5-1/weixin.jpg

22.1 KB
Loading

0 commit comments

Comments
 (0)