一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot整合郵件發送與注意事項

Spring Boot整合郵件發送與注意事項

2021-05-14 10:58jiajinhao Java教程

這篇文章主要給大家介紹了關于Spring Boot整合郵件發送與注意事項的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

什么是spring boot

spring boot是一個框架,其設計目的是簡化spring應用的初始搭建配置以及開發過程。該框架使用了特定的配置方式,從而使開發人員不在需要定義樣板化的配置。

spring boot的好處

1、配置簡單;

2、編碼簡單;

3、部署簡單;

4、監控簡單;

概述

spring boot下面整合了郵件服務器,使用spring boot能夠輕松實現郵件發送;整理下最近使用spring boot發送郵件和注意事項;

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=用戶名 //發送方的郵箱
spring.mail.password=密碼 //對于qq郵箱而言 密碼指的就是發送方的授權碼
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=false #是否用啟用加密傳送的協議驗證項
spring.mail.properties.mail.smtp.starttls.required=fasle #是否用啟用加密傳送的協議驗證項
#注意:在spring.mail.password處的值是需要在郵箱設置里面生成的授權碼,這個不是真實的密碼。

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
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("[email protected]");
   helper.setto("[email protected]");
   helper.setsubject("標題:發送html內容");
 
   stringbuffer sb = new stringbuffer();
   sb.append("<h1>大標題-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("電子發票",filesystemresource);
   javamailsender.send(message);
  } catch (messagingexception e) {
   e.printstacktrace();
  }
 }
}

非spring boot下發送電子郵件:

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協議發送電子郵件
 */
public class sendemailcode {
 
 
 // 郵件發送協議
 private final static string protocol = "smtp";
 
 // smtp郵件服務器
 private final static string host = "mail.tdb.com";
 
 // smtp郵件服務器默認端口
 private final static string port = "25";
 
 // 是否要求身份認證
 private final static string is_auth = "true";
 
 // 是否啟用調試模式(啟用調試模式可打印客戶端與服務器交互過程時一問一答的響應消息)
 private final static string is_enabled_debug_mod = "true";
 
 // 發件人
 private static string from = "[email protected]";
 
 // 收件人
 private static string to = "[email protected]";
 
 private static string sendusername="[email protected]";
 private static string senduserpwd="new*2016";
 
 // 初始化連接郵件服務器的會話信息
 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);
 }
 
 
 /**
  * 發送簡單的文本郵件
  */
 public static boolean sendtextemail(string to,int code) throws exception {
  try {
   // 創建session實例對象
   session session1 = session.getdefaultinstance(props);
 
   // 創建mimemessage實例對象
   mimemessage message = new mimemessage(session1);
   // 設置發件人
   message.setfrom(new internetaddress(from));
   // 設置郵件主題
   message.setsubject("內燃機注冊驗證碼");
   // 設置收件人
   message.setrecipient(recipienttype.to, new internetaddress(to));
   // 設置發送時間
   message.setsentdate(new date());
   // 設置純文本內容為郵件正文
   message.settext("您的驗證碼是:"+code+"!驗證碼有效期是10分鐘,過期后請重新獲取!"
     + "中國內燃機學會");
   // 保存并生成最終的郵件內容
   message.savechanges();
 
   // 獲得transport實例對象
   transport transport = session1.gettransport();
   // 打開連接
   transport.connect("meijiajiang2016", "");
   // 將message對象傳遞給transport對象,將郵件發送出去
   transport.sendmessage(message, message.getallrecipients());
   // 關閉連接
   transport.close();
 
   return true;
  } catch (exception e) {
   e.printstacktrace();
   return false;
  }
 }
 
 public static void main(string[] args) throws exception {
  sendhtmlemail("[email protected]", 88888);
 }
 
 /**
  * 發送簡單的html郵件
  */
 public static boolean sendhtmlemail(string to,int code) throws exception {
  // 創建session實例對象
  session session1 = session.getinstance(props, new myauthenticator());
 
  // 創建mimemessage實例對象
  mimemessage message = new mimemessage(session1);
  // 設置郵件主題
  message.setsubject("內燃機注冊");
  // 設置發送人
  message.setfrom(new internetaddress(from));
  // 設置發送時間
  message.setsentdate(new date());
  // 設置收件人
  message.setrecipients(recipienttype.to, internetaddress.parse(to));
  // 設置html內容為郵件正文,指定mime類型為text/html類型,并指定字符編碼為gbk
  message.setcontent("<div style='width: 600px;margin: 0 auto'><h3 style='color:#003e64; text-align:center; '>內燃機注冊驗證碼</h3><p style=''>尊敬的用戶您好:</p><p style='text-indent: 2em'>您在注冊內燃機賬號,此次的驗證碼是:"+code+",有效期10分鐘!如果過期請重新獲取。</p><p style='text-align: right; color:#003e64; font-size: 20px;'>中國內燃機學會</p></div>","text/html;charset=utf-8");
 
  //設置自定義發件人昵稱
  string nick="";
  try {
   nick=javax.mail.internet.mimeutility.encodetext("中國內燃機學會");
  } catch (unsupportedencodingexception e) {
   e.printstacktrace();
  }
  message.setfrom(new internetaddress(nick+" <"+from+">"));
  // 保存并生成最終的郵件內容
  message.savechanges();
 
  // 發送郵件
  try {
   transport.send(message);
   return true;
  } catch (exception e) {
   e.printstacktrace();
   return false;
  }
 
 }
 
 /**
  * 發送帶內嵌圖片的html郵件
  */
 public static void sendhtmlwithinnerimageemail() throws messagingexception {
  // 創建session實例對象
  session session = session.getdefaultinstance(props, new myauthenticator());
 
  // 創建郵件內容
  mimemessage message = new mimemessage(session);
  // 郵件主題,并指定編碼格式
  message.setsubject("帶內嵌圖片的html郵件", "utf-8");
  // 發件人
  message.setfrom(new internetaddress(from));
  // 收件人
  message.setrecipients(recipienttype.to, internetaddress.parse(to));
  // 抄送
  message.setrecipient(recipienttype.cc, new internetaddress("[email protected]"));
  // 密送 (不會在郵件收件人名單中顯示出來)
  message.setrecipient(recipienttype.bcc, new internetaddress("[email protected]"));
  // 發送時間
  message.setsentdate(new date());
 
  // 創建一個mime子類型為“related”的mimemultipart對象
  mimemultipart mp = new mimemultipart("related");
  // 創建一個表示正文的mimebodypart對象,并將它加入到前面創建的mimemultipart對象中
  mimebodypart htmlpart = new mimebodypart();
  mp.addbodypart(htmlpart);
  // 創建一個表示圖片資源的mimebodypart對象,將將它加入到前面創建的mimemultipart對象中
  mimebodypart imagepart = new mimebodypart();
  mp.addbodypart(imagepart);
 
  // 將mimemultipart對象設置為整個郵件的內容
  message.setcontent(mp);
 
  // 設置內嵌圖片郵件體
  datasource ds = new filedatasource(new file("resource/firefoxlogo.png"));
  datahandler dh = new datahandler(ds);
  imagepart.setdatahandler(dh);
  imagepart.setcontentid("firefoxlogo.png"); // 設置內容編號,用于其它郵件體引用
 
  // 創建一個mime子類型為"alternative"的mimemultipart對象,并作為前面創建的htmlpart對象的郵件內容
  mimemultipart htmlmultipart = new mimemultipart("alternative");
  // 創建一個表示html正文的mimebodypart對象
  mimebodypart htmlbodypart = new mimebodypart();
  // 其中cid=androidlogo.gif是引用郵件內部的圖片,即imagepart.setcontentid("androidlogo.gif");方法所保存的圖片
  htmlbodypart.setcontent("<span style='color:red;'>這是帶內嵌圖片的html郵件哦!!!<img src=\"cid:firefoxlogo.png\" /></span>","text/html;charset=utf-8");
  htmlmultipart.addbodypart(htmlbodypart);
  htmlpart.setcontent(htmlmultipart);
 
  // 保存并生成最終的郵件內容
  message.savechanges();
 
  // 發送郵件
  transport.send(message);
 }
 
 /**
  * 發送帶內嵌圖片、附件、多收件人(顯示郵箱姓名)、郵件優先級、閱讀回執的完整的html郵件
  */
 public static void sendmultipleemail() throws exception {
  string charset = "utf-8"; // 指定中文編碼格式
  // 創建session實例對象
  session session = session.getinstance(props,new myauthenticator());
 
  // 創建mimemessage實例對象
  mimemessage message = new mimemessage(session);
  // 設置主題
  message.setsubject("使用javamail發送混合組合類型的郵件測試");
  // 設置發送人
  message.setfrom(new internetaddress(from,"新浪測試郵箱",charset));
  // 設置收件人
  message.setrecipients(recipienttype.to,
    new address[] {
      // 參數1:郵箱地址,參數2:姓名(在客戶端收件只顯示姓名,而不顯示郵件地址),參數3:姓名中文字符串編碼
      new internetaddress("[email protected]", "張三_sohu", charset),
      new internetaddress("[email protected]", "李四_163", charset),
    }
  );
  // 設置抄送
  message.setrecipient(recipienttype.cc, new internetaddress("[email protected]","王五_gmail",charset));
  // 設置密送
  message.setrecipient(recipienttype.bcc, new internetaddress("[email protected]", "趙六_qq", charset));
  // 設置發送時間
  message.setsentdate(new date());
  // 設置回復人(收件人回復此郵件時,默認收件人)
  message.setreplyto(internetaddress.parse("\"" + mimeutility.encodetext("田七") + "\" <[email protected]>"));
  // 設置優先級(1:緊急 3:普通 5:低)
  message.setheader("x-priority", "1");
  // 要求閱讀回執(收件人閱讀郵件時會提示回復發件人,表明郵件已收到,并已閱讀)
  message.setheader("disposition-notification-to", from);
 
  // 創建一個mime子類型為"mixed"的mimemultipart對象,表示這是一封混合組合類型的郵件
  mimemultipart mailcontent = new mimemultipart("mixed");
  message.setcontent(mailcontent);
 
  // 附件
  mimebodypart attach1 = new mimebodypart();
  mimebodypart attach2 = new mimebodypart();
  // 內容
  mimebodypart mailbody = new mimebodypart();
 
  // 將附件和內容添加到郵件當中
  mailcontent.addbodypart(attach1);
  mailcontent.addbodypart(attach2);
  mailcontent.addbodypart(mailbody);
 
  // 附件1(利用jaf框架讀取數據源生成郵件體)
  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/如何學好c語言.txt");
  datahandler dh2 = new datahandler(ds2);
  attach2.setdatahandler(dh2);
  attach2.setfilename(mimeutility.encodetext("如何學好c語言.txt"));
 
  // 郵件正文(內嵌圖片+html文本)
  mimemultipart body = new mimemultipart("related"); //郵件正文也是一個組合體,需要指明組合關系
  mailbody.setcontent(body);
 
  // 郵件正文由html和圖片構成
  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郵件內容
  mimemultipart htmlmultipart = new mimemultipart("alternative");
  htmlpart.setcontent(htmlmultipart);
  mimebodypart htmlcontent = new mimebodypart();
  htmlcontent.setcontent(
    "<span style='color:red'>這是我自己用java mail發送的郵件哦!" +
      "<img src='cid:firefoxlogo.png' /></span>"
    , "text/html;charset=gbk");
  htmlmultipart.addbodypart(htmlcontent);
 
  // 保存郵件內容修改
  message.savechanges();
 
  /*file eml = buildemlfile(message);
  sendmailforeml(eml);*/
 
  // 發送郵件
  transport.send(message);
 }
 
 /**
  * 將郵件內容生成eml文件
  * @param message 郵件內容
  */
 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;
 }
 
 /**
  * 發送本地已經生成好的email文件
  */
 public static void sendmailforeml(file eml) throws exception {
  // 獲得郵件會話
  session session = session.getinstance(props,new myauthenticator());
  // 獲得郵件內容,即發生前生成的eml文件
  inputstream is = new fileinputstream(eml);
  mimemessage message = new mimemessage(session,is);
  //發送郵件
  transport.send(message);
 }
 
 /**
  * 向郵件服務器提交認證信息
  */
 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 {
 //發送郵件
 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", "[email protected]");
   props.setproperty("mail.pass", "new***");
   props.setproperty("mail.smtp.host","mail.tdb.com");
 
   props.setproperty("mail.smtp.from","[email protected]");
   props.setproperty("mail.smtp.fromname","tdbvc");
 
   // 創建一個程序與郵件服務器的通信
   session mailconnection = session.getinstance(props, null);
   message msg = new mimemessage(mailconnection);
 
   // 設置發送人和接受人
   address sender = new internetaddress(props.getproperty("mail.smtp.from"));
   // 多個接收人
   msg.setfrom(sender);
 
   set<internetaddress> touserset = new hashset<internetaddress>();
   // 郵箱有效性較驗
   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]));
   // 設置抄送
   if (copyto != null) {
    set<internetaddress> copytouserset = new hashset<internetaddress>();
    // 郵箱有效性較驗
    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]));
   }
   // 設置郵件主題
   msg.setsubject(mimeutility.encodetext(subject, "utf-8", "b")); // 中文亂碼問題
 
   // 設置郵件內容
   bodypart messagebodypart = new mimebodypart();
   messagebodypart.setcontent(content, "text/html; charset=utf-8"); // 中文
   multipart multipart = new mimemultipart();
   multipart.addbodypart(messagebodypart);
   msg.setcontent(multipart);
 
   /********************** 發送附件 ************************/
   if (attbody != null) {
    string[] filepath = attbody.split(";");
    for (string filepath : filepath) {
     //設置信件的附件(用本地機上的文件作為附件)
     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作為消息對象的內容
    msg.setcontent(multipart);
   };
   if (attbodys != null) {
    for (string filepath : attbodys) {
     //設置信件的附件(用本地機上的文件作為附件)
     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作為消息對象的內容
    msg.setcontent(multipart);
   }
   ;
   /********************** 發送附件結束 ************************/
 
   // 先進行存儲郵件
   msg.savechanges();
   system.out.println("正在發送郵件....");
   transport trans = mailconnection.gettransport(props.getproperty("mail.protocal"));
   // 郵件服務器名,用戶名,密碼
   trans.connect(props.getproperty("mail.smtp.host"), props.getproperty("mail.user"), props.getproperty("mail.pass"));
   trans.sendmessage(msg, msg.getallrecipients());
   system.out.println("發送郵件成功!");
 
   // 關閉通道
   if (trans.isconnected()) {
    trans.close();
   }
   return true;
  } catch (exception e) {
   system.err.println("郵件發送失敗!" + e);
   return false;
  } finally {
  }
 }
 
 // 發信人,收信人,回執人郵件中有中文處理亂碼,res為獲取的地址
 // http默認的編碼方式為iso8859_1
 // 對含有中文的發送地址,使用mimeutility.decodetex方法
 // 對其他則把地址從iso8859_1編碼轉換成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;
 }
 
 // 轉換為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[]{"[email protected]"});
  email.setsubject("test測件");
  email.setcontent("test測試");
  sendmail(email);
 }
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:https://www.cnblogs.com/xibei666/p/9016593.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: jazz中国在线视频 | 日韩毛片免费在线观看 | 青青草国产一区二区三区 | 国产亚洲欧美在线中文bt天堂网 | 国产一区二区三区四卡 | 亚洲爱视频 | 幻女free性摘花第一次 | 免费看成人毛片日本久久 | 国产成人精品一区二三区2022 | 男女做受快插大片 | 免费看一级大片 | 免费看一级a一片毛片 | 国产主播99 | yjzz视频| 美女黑人做受xxxxxⅹ | 单身男女韩剧在线看 | 王淑兰与铁柱全文免费阅读 | 国产图片一区 | 国产精品视频自拍 | 国产成人性色视频 | 513热点网深夜影院影院诶 | 亚洲AV国产福利精品在现观看 | 免费看一级 | 色综合网天天综合色中文男男 | 极品虎白女在线观看一线天 | 日本大乳护士的引诱图片 | 欧美久草在线 | 亚洲老头老太hd | 东北恋哥在线播放免费播放 | 精品一区二区三区五区六区七区 | 午夜影院网页 | 九九99精品| 女色在线观看免费视频 | a v在线男人的天堂观看免费 | 无人区国产大片 | 暖暖 免费 高清 日本 在线1 | 喷奶水榨乳ova动漫无修 | 免费一区在线观看 | 爱爱一级视频 | 国产精品免费视频能看 | 性夜影院爽黄A爽免费动漫 性色欲情网站IWWW九文堂 |