什么是spring boot
spring boot是一個框架,其設(shè)計目的是簡化spring應(yīng)用的初始搭建配置以及開發(fā)過程。該框架使用了特定的配置方式,從而使開發(fā)人員不在需要定義樣板化的配置。
spring boot的好處
1、配置簡單;
2、編碼簡單;
3、部署簡單;
4、監(jiān)控簡單;
概述
spring boot下面整合了郵件服務(wù)器,使用spring boot能夠輕松實(shí)現(xiàn)郵件發(fā)送;整理下最近使用spring boot發(fā)送郵件和注意事項;
maven包依賴
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-mail</artifactid> </dependency> |
spring boot的配置
1
2
3
4
5
6
7
|
spring.mail.host=smtp.servie.com spring.mail.username=用戶名 //發(fā)送方的郵箱 spring.mail.password=密碼 //對于qq郵箱而言 密碼指的就是發(fā)送方的授權(quán)碼 spring.mail.properties.mail.smtp.auth= true spring.mail.properties.mail.smtp.starttls.enable= false #是否用啟用加密傳送的協(xié)議驗(yàn)證項 spring.mail.properties.mail.smtp.starttls.required=fasle #是否用啟用加密傳送的協(xié)議驗(yàn)證項 #注意:在spring.mail.password處的值是需要在郵箱設(shè)置里面生成的授權(quán)碼,這個不是真實(shí)的密碼。 |
spring 代碼實(shí)現(xiàn)
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
|
package com.dbgo.webservicedemo.email; import org.springframework.beans.factory.annotation.autowired; import org.springframework.core.io.filesystemresource; import org.springframework.mail.javamail.javamailsender; import org.springframework.mail.javamail.mimemessagehelper; import org.springframework.stereotype.component; import javax.mail.messagingexception; import javax.mail.internet.mimemessage; import java.io.file; @component ( "emailtool" ) public class emailtool { @autowired private javamailsender javamailsender; public void sendsimplemail(){ mimemessage message = null ; try { message = javamailsender.createmimemessage(); mimemessagehelper helper = new mimemessagehelper(message, true ); helper.setfrom( "jiajinhao@dbgo.cn" ); helper.setto( "653484166@qq.com" ); helper.setsubject( "標(biāo)題:發(fā)送html內(nèi)容" ); stringbuffer sb = new stringbuffer(); sb.append( "<h1>大標(biāo)題-h1</h1>" ) .append( "<p style='color:#f00'>紅色字</p>" ) .append( "<p style='text-align:right'>右對齊</p>" ); helper.settext(sb.tostring(), true ); filesystemresource filesystemresource= new filesystemresource( new file( "d:\76678.pdf" )) helper.addattachment( "電子發(fā)票" ,filesystemresource); javamailsender.send(message); } catch (messagingexception e) { e.printstacktrace(); } } } |
非spring boot下發(fā)送電子郵件:
maven包依賴
1
2
3
4
5
6
7
|
<dependencies> <dependency> <groupid>com.sun.mail</groupid> <artifactid>javax.mail</artifactid> <version> 1.5 . 2 </version> </dependency> </dependencies> |
demo1代碼事例
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
package com.justin.framework.core.utils.email; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.unsupportedencodingexception; import java.util.date; import java.util.properties; import javax.activation.datahandler; import javax.activation.datasource; import javax.activation.filedatasource; import javax.mail.address; import javax.mail.authenticator; import javax.mail.message; import javax.mail.message.recipienttype; import javax.mail.messagingexception; import javax.mail.passwordauthentication; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimebodypart; import javax.mail.internet.mimemessage; import javax.mail.internet.mimemultipart; import javax.mail.internet.mimeutility; /** * 使用smtp協(xié)議發(fā)送電子郵件 */ public class sendemailcode { // 郵件發(fā)送協(xié)議 private final static string protocol = "smtp" ; // smtp郵件服務(wù)器 private final static string host = "mail.tdb.com" ; // smtp郵件服務(wù)器默認(rèn)端口 private final static string port = "25" ; // 是否要求身份認(rèn)證 private final static string is_auth = "true" ; // 是否啟用調(diào)試模式(啟用調(diào)試模式可打印客戶端與服務(wù)器交互過程時一問一答的響應(yīng)消息) private final static string is_enabled_debug_mod = "true" ; // 發(fā)件人 private static string from = "tdbjrcrm@tdb.com" ; // 收件人 private static string to = "db_yangruirui@tdbcwgs.com" ; private static string sendusername= "tdbjrcrm@tdb.com" ; private static string senduserpwd= "new*2016" ; // 初始化連接郵件服務(wù)器的會話信息 private static properties props = null ; static { props = new properties(); props.setproperty( "mail.enable" , "true" ); props.setproperty( "mail.transport.protocol" , protocol); props.setproperty( "mail.smtp.host" , host); props.setproperty( "mail.smtp.port" , port); props.setproperty( "mail.smtp.auth" , is_auth); //視情況而定 props.setproperty( "mail.debug" ,is_enabled_debug_mod); } /** * 發(fā)送簡單的文本郵件 */ public static boolean sendtextemail(string to, int code) throws exception { try { // 創(chuàng)建session實(shí)例對象 session session1 = session.getdefaultinstance(props); // 創(chuàng)建mimemessage實(shí)例對象 mimemessage message = new mimemessage(session1); // 設(shè)置發(fā)件人 message.setfrom( new internetaddress(from)); // 設(shè)置郵件主題 message.setsubject( "內(nèi)燃機(jī)注冊驗(yàn)證碼" ); // 設(shè)置收件人 message.setrecipient(recipienttype.to, new internetaddress(to)); // 設(shè)置發(fā)送時間 message.setsentdate( new date()); // 設(shè)置純文本內(nèi)容為郵件正文 message.settext( "您的驗(yàn)證碼是:" +code+ "!驗(yàn)證碼有效期是10分鐘,過期后請重新獲取!" + "中國內(nèi)燃機(jī)學(xué)會" ); // 保存并生成最終的郵件內(nèi)容 message.savechanges(); // 獲得transport實(shí)例對象 transport transport = session1.gettransport(); // 打開連接 transport.connect( "meijiajiang2016" , "" ); // 將message對象傳遞給transport對象,將郵件發(fā)送出去 transport.sendmessage(message, message.getallrecipients()); // 關(guān)閉連接 transport.close(); return true ; } catch (exception e) { e.printstacktrace(); return false ; } } public static void main(string[] args) throws exception { sendhtmlemail( "db_yangruirui@tdbcwgs.com" , 88888 ); } /** * 發(fā)送簡單的html郵件 */ public static boolean sendhtmlemail(string to, int code) throws exception { // 創(chuàng)建session實(shí)例對象 session session1 = session.getinstance(props, new myauthenticator()); // 創(chuàng)建mimemessage實(shí)例對象 mimemessage message = new mimemessage(session1); // 設(shè)置郵件主題 message.setsubject( "內(nèi)燃機(jī)注冊" ); // 設(shè)置發(fā)送人 message.setfrom( new internetaddress(from)); // 設(shè)置發(fā)送時間 message.setsentdate( new date()); // 設(shè)置收件人 message.setrecipients(recipienttype.to, internetaddress.parse(to)); // 設(shè)置html內(nèi)容為郵件正文,指定mime類型為text/html類型,并指定字符編碼為gbk message.setcontent( "<div style='width: 600px;margin: 0 auto'><h3 style='color:#003e64; text-align:center; '>內(nèi)燃機(jī)注冊驗(yàn)證碼</h3><p style=''>尊敬的用戶您好:</p><p style='text-indent: 2em'>您在注冊內(nèi)燃機(jī)賬號,此次的驗(yàn)證碼是:" +code+ ",有效期10分鐘!如果過期請重新獲取。</p><p style='text-align: right; color:#003e64; font-size: 20px;'>中國內(nèi)燃機(jī)學(xué)會</p></div>" , "text/html;charset=utf-8" ); //設(shè)置自定義發(fā)件人昵稱 string nick= "" ; try { nick=javax.mail.internet.mimeutility.encodetext( "中國內(nèi)燃機(jī)學(xué)會" ); } catch (unsupportedencodingexception e) { e.printstacktrace(); } message.setfrom( new internetaddress(nick+ " <" +from+ ">" )); // 保存并生成最終的郵件內(nèi)容 message.savechanges(); // 發(fā)送郵件 try { transport.send(message); return true ; } catch (exception e) { e.printstacktrace(); return false ; } } /** * 發(fā)送帶內(nèi)嵌圖片的html郵件 */ public static void sendhtmlwithinnerimageemail() throws messagingexception { // 創(chuàng)建session實(shí)例對象 session session = session.getdefaultinstance(props, new myauthenticator()); // 創(chuàng)建郵件內(nèi)容 mimemessage message = new mimemessage(session); // 郵件主題,并指定編碼格式 message.setsubject( "帶內(nèi)嵌圖片的html郵件" , "utf-8" ); // 發(fā)件人 message.setfrom( new internetaddress(from)); // 收件人 message.setrecipients(recipienttype.to, internetaddress.parse(to)); // 抄送 message.setrecipient(recipienttype.cc, new internetaddress( "java_test@sohu.com" )); // 密送 (不會在郵件收件人名單中顯示出來) message.setrecipient(recipienttype.bcc, new internetaddress( "417067629@qq.com" )); // 發(fā)送時間 message.setsentdate( new date()); // 創(chuàng)建一個mime子類型為“related”的mimemultipart對象 mimemultipart mp = new mimemultipart( "related" ); // 創(chuàng)建一個表示正文的mimebodypart對象,并將它加入到前面創(chuàng)建的mimemultipart對象中 mimebodypart htmlpart = new mimebodypart(); mp.addbodypart(htmlpart); // 創(chuàng)建一個表示圖片資源的mimebodypart對象,將將它加入到前面創(chuàng)建的mimemultipart對象中 mimebodypart imagepart = new mimebodypart(); mp.addbodypart(imagepart); // 將mimemultipart對象設(shè)置為整個郵件的內(nèi)容 message.setcontent(mp); // 設(shè)置內(nèi)嵌圖片郵件體 datasource ds = new filedatasource( new file( "resource/firefoxlogo.png" )); datahandler dh = new datahandler(ds); imagepart.setdatahandler(dh); imagepart.setcontentid( "firefoxlogo.png" ); // 設(shè)置內(nèi)容編號,用于其它郵件體引用 // 創(chuàng)建一個mime子類型為"alternative"的mimemultipart對象,并作為前面創(chuàng)建的htmlpart對象的郵件內(nèi)容 mimemultipart htmlmultipart = new mimemultipart( "alternative" ); // 創(chuàng)建一個表示html正文的mimebodypart對象 mimebodypart htmlbodypart = new mimebodypart(); // 其中cid=androidlogo.gif是引用郵件內(nèi)部的圖片,即imagepart.setcontentid("androidlogo.gif");方法所保存的圖片 htmlbodypart.setcontent( "<span style='color:red;'>這是帶內(nèi)嵌圖片的html郵件哦!!!<img src=\"cid:firefoxlogo.png\" /></span>" , "text/html;charset=utf-8" ); htmlmultipart.addbodypart(htmlbodypart); htmlpart.setcontent(htmlmultipart); // 保存并生成最終的郵件內(nèi)容 message.savechanges(); // 發(fā)送郵件 transport.send(message); } /** * 發(fā)送帶內(nèi)嵌圖片、附件、多收件人(顯示郵箱姓名)、郵件優(yōu)先級、閱讀回執(zhí)的完整的html郵件 */ public static void sendmultipleemail() throws exception { string charset = "utf-8" ; // 指定中文編碼格式 // 創(chuàng)建session實(shí)例對象 session session = session.getinstance(props, new myauthenticator()); // 創(chuàng)建mimemessage實(shí)例對象 mimemessage message = new mimemessage(session); // 設(shè)置主題 message.setsubject( "使用javamail發(fā)送混合組合類型的郵件測試" ); // 設(shè)置發(fā)送人 message.setfrom( new internetaddress(from, "新浪測試郵箱" ,charset)); // 設(shè)置收件人 message.setrecipients(recipienttype.to, new address[] { // 參數(shù)1:郵箱地址,參數(shù)2:姓名(在客戶端收件只顯示姓名,而不顯示郵件地址),參數(shù)3:姓名中文字符串編碼 new internetaddress( "java_test@sohu.com" , "張三_sohu" , charset), new internetaddress( "xyang0917@163.com" , "李四_163" , charset), } ); // 設(shè)置抄送 message.setrecipient(recipienttype.cc, new internetaddress( "xyang0917@gmail.com" , "王五_gmail" ,charset)); // 設(shè)置密送 message.setrecipient(recipienttype.bcc, new internetaddress( "xyang0917@qq.com" , "趙六_qq" , charset)); // 設(shè)置發(fā)送時間 message.setsentdate( new date()); // 設(shè)置回復(fù)人(收件人回復(fù)此郵件時,默認(rèn)收件人) message.setreplyto(internetaddress.parse( "\"" + mimeutility.encodetext( "田七" ) + "\" <417067629@qq.com>" )); // 設(shè)置優(yōu)先級(1:緊急 3:普通 5:低) message.setheader( "x-priority" , "1" ); // 要求閱讀回執(zhí)(收件人閱讀郵件時會提示回復(fù)發(fā)件人,表明郵件已收到,并已閱讀) message.setheader( "disposition-notification-to" , from); // 創(chuàng)建一個mime子類型為"mixed"的mimemultipart對象,表示這是一封混合組合類型的郵件 mimemultipart mailcontent = new mimemultipart( "mixed" ); message.setcontent(mailcontent); // 附件 mimebodypart attach1 = new mimebodypart(); mimebodypart attach2 = new mimebodypart(); // 內(nèi)容 mimebodypart mailbody = new mimebodypart(); // 將附件和內(nèi)容添加到郵件當(dāng)中 mailcontent.addbodypart(attach1); mailcontent.addbodypart(attach2); mailcontent.addbodypart(mailbody); // 附件1(利用jaf框架讀取數(shù)據(jù)源生成郵件體) datasource ds1 = new filedatasource( "resource/earth.bmp" ); datahandler dh1 = new datahandler(ds1); attach1.setfilename(mimeutility.encodetext( "earth.bmp" )); attach1.setdatahandler(dh1); // 附件2 datasource ds2 = new filedatasource( "resource/如何學(xué)好c語言.txt" ); datahandler dh2 = new datahandler(ds2); attach2.setdatahandler(dh2); attach2.setfilename(mimeutility.encodetext( "如何學(xué)好c語言.txt" )); // 郵件正文(內(nèi)嵌圖片+html文本) mimemultipart body = new mimemultipart( "related" ); //郵件正文也是一個組合體,需要指明組合關(guān)系 mailbody.setcontent(body); // 郵件正文由html和圖片構(gòu)成 mimebodypart imgpart = new mimebodypart(); mimebodypart htmlpart = new mimebodypart(); body.addbodypart(imgpart); body.addbodypart(htmlpart); // 正文圖片 datasource ds3 = new filedatasource( "resource/firefoxlogo.png" ); datahandler dh3 = new datahandler(ds3); imgpart.setdatahandler(dh3); imgpart.setcontentid( "firefoxlogo.png" ); // html郵件內(nèi)容 mimemultipart htmlmultipart = new mimemultipart( "alternative" ); htmlpart.setcontent(htmlmultipart); mimebodypart htmlcontent = new mimebodypart(); htmlcontent.setcontent( "<span style='color:red'>這是我自己用java mail發(fā)送的郵件哦!" + "<img src='cid:firefoxlogo.png' /></span>" , "text/html;charset=gbk" ); htmlmultipart.addbodypart(htmlcontent); // 保存郵件內(nèi)容修改 message.savechanges(); /*file eml = buildemlfile(message); sendmailforeml(eml);*/ // 發(fā)送郵件 transport.send(message); } /** * 將郵件內(nèi)容生成eml文件 * @param message 郵件內(nèi)容 */ public static file buildemlfile(message message) throws messagingexception, filenotfoundexception, ioexception { file file = new file("c:\\" + mimeutility.decodetext(message.getsubject())+".eml"); message.writeto(new fileoutputstream(file)); return file; } /** * 發(fā)送本地已經(jīng)生成好的email文件 */ public static void sendmailforeml(file eml) throws exception { // 獲得郵件會話 session session = session.getinstance(props,new myauthenticator()); // 獲得郵件內(nèi)容,即發(fā)生前生成的eml文件 inputstream is = new fileinputstream(eml); mimemessage message = new mimemessage(session,is); //發(fā)送郵件 transport.send(message); } /** * 向郵件服務(wù)器提交認(rèn)證信息 */ static class myauthenticator extends authenticator { private string username = "" ; private string password = "" ; public myauthenticator() { super (); this .password=senduserpwd; this .username=sendusername; } public myauthenticator(string username, string password) { super (); this .username = username; this .password = password; } @override protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(username, password); } } } |
demo2代碼事例:
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
|
package com.justin.framework.core.utils.email; import java.util.hashset; import java.util.properties; import java.util.set; import javax.activation.datahandler; import javax.activation.filedatasource; import javax.mail.address; import javax.mail.bodypart; import javax.mail.message; import javax.mail.multipart; import javax.mail.session; import javax.mail.transport; import javax.mail.internet.internetaddress; import javax.mail.internet.mimebodypart; import javax.mail.internet.mimemessage; import javax.mail.internet.mimemultipart; import javax.mail.internet.mimeutility; public class mailmanagerutils { //發(fā)送郵件 public static boolean sendmail(email email) { string subject = email.getsubject(); string content = email.getcontent(); string[] recievers = email.getrecievers(); string[] copyto = email.getcopyto(); string attbody = email.getattbody(); string[] attbodys = email.getattbodys(); if (recievers == null || recievers.length <= 0 ) { return false ; } try { properties props = new properties(); props.setproperty( "mail.enable" , "true" ); props.setproperty( "mail.protocal" , "smtp" ); props.setproperty( "mail.smtp.auth" , "true" ); props.setproperty( "mail.user" , "tdbjrcrm@tdb.com" ); props.setproperty( "mail.pass" , "new***" ); props.setproperty( "mail.smtp.host" , "mail.tdb.com" ); props.setproperty( "mail.smtp.from" , "tdbjrcrm@tdb.com" ); props.setproperty( "mail.smtp.fromname" , "tdbvc" ); // 創(chuàng)建一個程序與郵件服務(wù)器的通信 session mailconnection = session.getinstance(props, null ); message msg = new mimemessage(mailconnection); // 設(shè)置發(fā)送人和接受人 address sender = new internetaddress(props.getproperty( "mail.smtp.from" )); // 多個接收人 msg.setfrom(sender); set<internetaddress> touserset = new hashset<internetaddress>(); // 郵箱有效性較驗(yàn) for ( int i = 0 ; i < recievers.length; i++) { if (recievers[i].trim().matches( "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)+$" )) { touserset.add( new internetaddress(recievers[i].trim())); } } msg.setrecipients(message.recipienttype.to, touserset.toarray( new internetaddress[ 0 ])); // 設(shè)置抄送 if (copyto != null ) { set<internetaddress> copytouserset = new hashset<internetaddress>(); // 郵箱有效性較驗(yàn) for ( int i = 0 ; i < copyto.length; i++) { if (copyto[i].trim().matches( "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)+$" )) { copytouserset.add( new internetaddress(copyto[i].trim())); } } // msg.setrecipients(message.recipienttype.cc,(address[])internetaddress.parse(copyto)); msg.setrecipients(message.recipienttype.cc, copytouserset.toarray( new internetaddress[ 0 ])); } // 設(shè)置郵件主題 msg.setsubject(mimeutility.encodetext(subject, "utf-8" , "b" )); // 中文亂碼問題 // 設(shè)置郵件內(nèi)容 bodypart messagebodypart = new mimebodypart(); messagebodypart.setcontent(content, "text/html; charset=utf-8" ); // 中文 multipart multipart = new mimemultipart(); multipart.addbodypart(messagebodypart); msg.setcontent(multipart); /********************** 發(fā)送附件 ************************/ if (attbody != null ) { string[] filepath = attbody.split( ";" ); for (string filepath : filepath) { //設(shè)置信件的附件(用本地機(jī)上的文件作為附件) bodypart mdp = new mimebodypart(); filedatasource fds = new filedatasource(filepath); datahandler dh = new datahandler(fds); mdp.setfilename(mimeutility.encodetext(fds.getname())); mdp.setdatahandler(dh); multipart.addbodypart(mdp); } //把mtp作為消息對象的內(nèi)容 msg.setcontent(multipart); }; if (attbodys != null ) { for (string filepath : attbodys) { //設(shè)置信件的附件(用本地機(jī)上的文件作為附件) bodypart mdp = new mimebodypart(); filedatasource fds = new filedatasource(filepath); datahandler dh = new datahandler(fds); mdp.setfilename(mimeutility.encodetext(fds.getname())); mdp.setdatahandler(dh); multipart.addbodypart(mdp); } //把mtp作為消息對象的內(nèi)容 msg.setcontent(multipart); } ; /********************** 發(fā)送附件結(jié)束 ************************/ // 先進(jìn)行存儲郵件 msg.savechanges(); system.out.println( "正在發(fā)送郵件...." ); transport trans = mailconnection.gettransport(props.getproperty( "mail.protocal" )); // 郵件服務(wù)器名,用戶名,密碼 trans.connect(props.getproperty( "mail.smtp.host" ), props.getproperty( "mail.user" ), props.getproperty( "mail.pass" )); trans.sendmessage(msg, msg.getallrecipients()); system.out.println( "發(fā)送郵件成功!" ); // 關(guān)閉通道 if (trans.isconnected()) { trans.close(); } return true ; } catch (exception e) { system.err.println( "郵件發(fā)送失敗!" + e); return false ; } finally { } } // 發(fā)信人,收信人,回執(zhí)人郵件中有中文處理亂碼,res為獲取的地址 // http默認(rèn)的編碼方式為iso8859_1 // 對含有中文的發(fā)送地址,使用mimeutility.decodetex方法 // 對其他則把地址從iso8859_1編碼轉(zhuǎn)換成gbk編碼 public static string getchinesefrom(string res) { string from = res; try { if (from.startswith( "=?gb" ) || from.startswith( "=?gb" ) || from.startswith( "=?utf" )) { from = mimeutility.decodetext(from); } else { from = new string(from.getbytes( "iso8859_1" ), "gbk" ); } } catch (exception e) { e.printstacktrace(); } return from; } // 轉(zhuǎn)換為gbk編碼 public static string tochinese(string strvalue) { try { if (strvalue == null ) return null ; else { strvalue = new string(strvalue.getbytes( "iso8859_1" ), "gbk" ); return strvalue; } } catch (exception e) { return null ; } } public static void main(string[] args) { email email= new email(); email.setrecievers( new string[]{ "db_yangruirui@tdbcwgs.com" }); email.setsubject( "test測件" ); email.setcontent( "test測試" ); sendmail(email); } } |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://www.cnblogs.com/xibei666/p/9016593.html