建立一個springboot工程,在其pom文件加入:html
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
|
1
2
3
4
5
6
|
spring.mail.host=smtp.
163
.com
spring.mail.username=miles02
@163
.com
spring.mail.password=
spring.mail.port=
25
spring.mail.protocol=smtp
spring.mail.
default
-encoding=UTF-
8
|
在password 中填寫本身的郵箱密碼。java
測試代碼清單以下:spring
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
|
package
com.forezp;
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.core.io.FileSystemResource;
import
org.springframework.mail.SimpleMailMessage;
import
org.springframework.mail.javamail.JavaMailSenderImpl;
import
org.springframework.mail.javamail.MimeMessageHelper;
import
org.springframework.test.context.junit4.SpringRunner;
import
javax.mail.internet.MimeMessage;
import
java.io.File;
@RunWith
(SpringRunner.
class
)
@SpringBootTest
public
class
SpringbootJmsApplicationTests {
@Test
public
void
contextLoads() {
}
@Autowired
private
JavaMailSenderImpl mailSender;
/**
* 發送包含簡單文本的郵件
*/
@Test
public
void
sendTxtMail() {
SimpleMailMessage simpleMailMessage =
new
SimpleMailMessage();
// 設置收件人,寄件人
simpleMailMessage.setTo(
new
String[] {
"miles02@163.com"
});
simpleMailMessage.setFrom(
"miles02@163.com"
);
simpleMailMessage.setSubject(
"Spring Boot Mail 郵件測試【文本】"
);
simpleMailMessage.setText(
"這裏是一段簡單文本。"
);
// 發送郵件
mailSender.send(simpleMailMessage);
System.out.println(
"郵件已發送"
);
}
/**
* 發送包含HTML文本的郵件
* @throws Exception
*/
@Test
public
void
sendHtmlMail()
throws
Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper =
new
MimeMessageHelper(mimeMessage);
mimeMessageHelper.setTo(
"miles02@163.com"
);
mimeMessageHelper.setFrom(
"miles02@163.com"
);
mimeMessageHelper.setSubject(
"Spring Boot Mail 郵件測試【HTML】"
);
StringBuilder sb =
new
StringBuilder();
sb.append(
"<html><head></head>"
);
sb.append(
"<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test。</p></body>"
);
sb.append(
"</html>"
);
// 啓用html
mimeMessageHelper.setText(sb.toString(),
true
);
// 發送郵件
mailSender.send(mimeMessage);
System.out.println(
"郵件已發送"
);
}
/**
* 發送包含內嵌圖片的郵件
* @throws Exception
*/
@Test
public
void
sendAttachedImageMail()
throws
Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// multipart模式
MimeMessageHelper mimeMessageHelper =
new
MimeMessageHelper(mimeMessage,
true
);
mimeMessageHelper.setTo(
"miles02@163.com"
);
mimeMessageHelper.setFrom(
"miles02@163.com"
);
mimeMessageHelper.setSubject(
"Spring Boot Mail 郵件測試【圖片】"
);
StringBuilder sb =
new
StringBuilder();
sb.append(
"<html><head></head>"
);
sb.append(
"<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test。</p>"
);
// cid爲固定寫法,imageId指定一個標識
sb.append(
"<img src=\"cid:imageId\"/></body>"
);
sb.append(
"</html>"
);
// 啓用html
mimeMessageHelper.setText(sb.toString(),
true
);
// 設置imageId
FileSystemResource img =
new
FileSystemResource(
new
File(
"E:/1.jpg"
));
mimeMessageHelper.addInline(
"imageId"
, img);
// 發送郵件
mailSender.send(mimeMessage);
System.out.println(
"郵件已發送"
);
}
/**
* 發送包含附件的郵件
* @throws Exception
*/
@Test
public
void
sendAttendedFileMail()
throws
Exception {
MimeMessage mimeMessage = mailSender.createMimeMessage();
// multipart模式
MimeMessageHelper mimeMessageHelper =
new
MimeMessageHelper(mimeMessage,
true
,
"utf-8"
);
mimeMessageHelper.setTo(
"miles02@163.com"
);
mimeMessageHelper.setFrom(
"miles02@163.com"
);
mimeMessageHelper.setSubject(
"Spring Boot Mail 郵件測試【附件】"
);
StringBuilder sb =
new
StringBuilder();
sb.append(
"<html><head></head>"
);
sb.append(
"<body><h1>spring 郵件測試</h1><p>hello!this is spring mail test。</p></body>"
);
sb.append(
"</html>"
);
// 啓用html
mimeMessageHelper.setText(sb.toString(),
true
);
// 設置附件
FileSystemResource img =
new
FileSystemResource(
new
File(
"E:/1.jpg"
));
mimeMessageHelper.addAttachment(
"image.jpg"
, img);
// 發送郵件
mailSender.send(mimeMessage);
System.out.println(
"郵件已發送"
);
}
}
|
資料和源碼來源地址springboot
Spring Cloud大型企業分佈式微服務雲架構源碼請加企鵝求求:一七九一七四三三八零
架構