Java B2B2C多使用者商城 springcloud架構 (二十一)springboot整合JMS

happywawa發表於2019-03-20

構架工程

建立一個springboot工程,在其pom檔案加入:

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 中填寫自己的郵箱密碼。

測試發郵件

測試程式碼清單如下:

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("郵件已傳送");
}
}

  

架構程式碼如下:

"分散式b2b <wbr


Spring Cloud大型企業分散式微服務雲構建的B2B2C電子商務平臺原始碼請加企鵝求求:一零三八七七四六二六 

相關文章