激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - Java Web stmp發(fā)送帶附件郵件(附SSL版)

Java Web stmp發(fā)送帶附件郵件(附SSL版)

2021-05-04 12:03luck-cheng Java教程

這篇文章主要為大家詳細介紹了Java Web stmp發(fā)送帶附件郵件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java web stmp發(fā)送帶附件郵件的具體代碼,供大家參考,具體內(nèi)容如下

?
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
public class mailfilesendutils {
 
 private properties props; //系統(tǒng)屬性
 private session session; //郵件會話對象
 private mimemessage mimemsg; //mime郵件對象
 private multipart mp; //multipart對象,郵件內(nèi)容,標題,附件等內(nèi)容均添加到其中后再生成mimemessage對象
 
 /**
  * constructor
  * @param
  */
 public mailfilesendutils(){
  props = system.getproperties();
  props.put("mail.smtp.auth","false");
  session = session.getdefaultinstance(props, null);
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * constructor
  * @param smtp 郵件發(fā)送服務(wù)器
  */
 public mailfilesendutils(string smtp, string username, string password){
  props = system.getproperties();
  props.put("mail.smtp.auth","true");
  props.put("mail.smtp.host", smtp);
  props.put("username", username);
  props.put("password", password);
  session = session.getdefaultinstance(props, null);
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * 發(fā)送郵件
  */
 public boolean sendmail(string from, string[] to, string subject, string content, string filename) {
  try {
   //設(shè)置發(fā)信人
   mimemsg.setfrom(new internetaddress(from));
   //設(shè)置接收人
   for (int i = 0; i < to.length; i++) {
    mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i]));
   }
   //設(shè)置抄送人
//   for (int i = 0; i < copyto.length; i++) {
//    mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i]));
//   }
   //設(shè)置主題
   mimemsg.setsubject(subject);
   //設(shè)置正文
   bodypart bp = new mimebodypart();
   bp.setcontent(content, "text/html;charset=utf-8");
   mp.addbodypart(bp);
   //設(shè)置附件
   bp = new mimebodypart();
   filedatasource fileds = new filedatasource(filename);
   bp.setdatahandler(new datahandler(fileds));
   bp.setfilename(mimeutility.encodetext(fileds.getname(),"utf-8","b"));
   mp.addbodypart(bp);
   mimemsg.setcontent(mp);
   mimemsg.savechanges();
   //發(fā)送郵件
   if(props.get("mail.smtp.auth").equals("true")){
    transport transport = session.gettransport("smtp");
    transport.connect((string)props.get("mail.smtp.host"), (string)props.get("username"), (string)props.get("password"));
    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.to));
//    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.cc));
    transport.close();
   }else{
    transport.send(mimemsg);
   }
   system.out.println("郵件發(fā)送成功");
  } catch (messagingexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (unsupportedencodingexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
  return true;
 }
 
// public void tosendmail(sendmailparam sendmailparam){
//  mailfilesendutils email = new mailfilesendutils(sendmailparam.getsmtp(), sendmailparam.getusername(), sendmailparam.getpassword());
//  email.sendmail(sendmailparam.getfrom(), sendmailparam.getto(), sendmailparam.getsubject(), sendmailparam.getcontent(), sendmailparam.getfilepath());
// }
 
 
 public static void main(string[] args) {
  string smtp = "smtp.exmail.qq.com";
  string username = "發(fā)送的郵箱賬號";
  string password = "發(fā)送的郵箱密碼";
  string from = "發(fā)送的郵箱";
  string[] to = {"接收郵件的郵箱"};
//  string[] copyto = {"抄送的郵箱"};
  string subject = "主題6";
  string content = "郵件內(nèi)容6";
  string filename = "附件的文件";
  mailfilesendutils email = new mailfilesendutils(smtp, username, password);
//  email.sendmail(from, to, copyto, subject, content, filename);
  email.sendmail(from, to, subject, content, filename);
 }
 
}

(附:ssl版)

 

?
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
public class mailfilesendutils {
 
 private properties props; //系統(tǒng)屬性
 private session session; //郵件會話對象
 private mimemessage mimemsg; //mime郵件對象
 private multipart mp; //multipart對象,郵件內(nèi)容,標題,附件等內(nèi)容均添加到其中后再生成mimemessage對象
 
 /**
  * constructor
  * @param
  */
 public mailfilesendutils(){
  props = system.getproperties();
  props.put("mail.smtp.auth","false");
  session = session.getdefaultinstance(props, null);
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * constructor
  * @param smtp 郵件發(fā)送服務(wù)器
  */
 public mailfilesendutils(string smtp,
        string username,
        string password){
  security.addprovider(new com.sun.net.ssl.internal.ssl.provider());
  final string ssl_factory = "javax.net.ssl.sslsocketfactory";
  props = system.getproperties();
  mailsslsocketfactory sf = null;
  try {
   sf = new mailsslsocketfactory();
  } catch (generalsecurityexception e) {
  }
  sf.settrustallhosts(true);
  props.put("mail.smtp.auth","true");
  props.put("mail.smtp.host", smtp);
  props.put("mail.smtp.socketfactory.class", ssl_factory);
  props.put("mail.smtp.socketfactory.fallback", "false");
  props.put("mail.smtp.ssl.enable", "true");
  props.put("mail.smtp.port", "465");
  props.put("mail.smtp.ssl.socketfactory", sf);
 
//  props.put("username", username);
//  props.put("password", password);
  session = session.getinstance(props, new authenticator() {
   @override
   protected passwordauthentication getpasswordauthentication() {
    return new passwordauthentication(username, password);
   }
  });
  session.setdebug(true);
  mimemsg = new mimemessage(session);
  mp = new mimemultipart();
 }
 
 /**
  * 發(fā)送郵件
  */
 public boolean sendmail(string from,
       string[] to,
       string subject,
       string content,
       string filename) {
  try {
   //設(shè)置發(fā)信人
   mimemsg.setfrom(new internetaddress(from));
   //設(shè)置接收人
   for (int i = 0; i < to.length; i++) {
    mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i]));
   }
   //設(shè)置抄送人
//   for (int i = 0; i < copyto.length; i++) {
//    mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i]));
//   }
   //設(shè)置主題
   mimemsg.setsubject(subject);
   //設(shè)置正文
   bodypart bp = new mimebodypart();
   bp.setcontent(content, "text/html;charset=utf-8");
   mp.addbodypart(bp);
   //設(shè)置附件
   bp = new mimebodypart();
   filedatasource fileds = new filedatasource(filename);
   bp.setdatahandler(new datahandler(fileds));
   bp.setfilename(mimeutility.encodetext(fileds.getname(),"utf-8","b"));
   mp.addbodypart(bp);
   mimemsg.setcontent(mp);
   mimemsg.savechanges();
   //發(fā)送郵件
   if(props.get("mail.smtp.auth").equals("true")){
    transport transport = session.gettransport("smtp");
    transport.connect((string)props.get("mail.smtp.host"), (string)props.get("username"), (string)props.get("password"));
    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.to));
//    transport.sendmessage(mimemsg, mimemsg.getrecipients(message.recipienttype.cc));
    transport.close();
   }else{
    transport.send(mimemsg);
   }
   system.out.println("郵件發(fā)送成功");
  } catch (messagingexception e) {
   e.printstacktrace();
  } catch (unsupportedencodingexception e) {
   e.printstacktrace();
  }
  return true;
 }
 
 public boolean tosendmail(sendmailparam sendmailparam){
  mailfilesendutils email = new mailfilesendutils(
    sendmailparam.getsmtp(),
    sendmailparam.getusername(),
    sendmailparam.getpassword());
  email.sendmail(
    sendmailparam.getfrom(),
    sendmailparam.getto(),
    sendmailparam.getsubject(),
    sendmailparam.getcontent(),
    sendmailparam.getfilepath());
  return true;
 }
 
 
// public static void main(string[] args) {
//  string smtp = "smtp.mxhichina.com";
//  string username = "郵箱";
//  string password = "郵箱密碼";
//  string from = "誰去發(fā)";
//  string[] to = {"發(fā)給誰"};
////  string[] copyto = {"抄送的郵箱"};
//  string subject = "huawei";
//  string content = "郵件內(nèi)容6666";
//  string filename = "gdt-3583118353-ad-20170823.xls";
//  mailfilesendutils email = new mailfilesendutils(smtp, username, password);
////  email.sendmail(from, to, copyto, subject, content, filename);
//  email.sendmail(from, to, subject, content, filename);
// }
 
}

在項目中使用這套工具,main方法我注釋掉,然后使用tosendmail(sendmailparam sendmailparam)。
這里定義的sendmailparam 為:

 

?
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
public class sendmailparam {
 private string smtp;
 private string username;
 private string password;
 private string from;//發(fā)送人
 private string[] to;//接收人
 //  string[] copyto = {"[email protected]"};
 private string subject;//郵件主題
 private string content;//郵件內(nèi)容
 private string filepath;//文件拿到的路徑
 
 public sendmailparam(){
  this.smtp = "smtp.exmail.qq.com";//例子
  this.username = "郵箱賬號";
  this.password = "郵箱密碼";
  this.from = "郵箱";
  this.subject = "";
  this.content = "";
  this.filepath = "";
 }
 
 public string getsmtp() {
  return smtp;
 }
 
 public void setsmtp(string smtp) {
  this.smtp = smtp;
 }
 
 public string getusername() {
  return username;
 }
 
 public void setusername(string username) {
  this.username = username;
 }
 
 public string getpassword() {
  return password;
 }
 
 public void setpassword(string password) {
  this.password = password;
 }
 
 public string getfrom() {
  return from;
 }
 
 public void setfrom(string from) {
  this.from = from;
 }
 
 public string[] getto() {
  return to;
 }
 
 public void setto(string[] to) {
  this.to = to;
 }
 
 public string getsubject() {
  return subject;
 }
 
 public void setsubject(string subject) {
  this.subject = subject;
 }
 
 public string getcontent() {
  return content;
 }
 
 public void setcontent(string content) {
  this.content = content;
 }
 
 public string getfilepath() {
  return filepath;
 }
 
 public void setfilepath(string filepath) {
  this.filepath = filepath;
 }
}

maven依賴包

?
1
2
3
4
5
<dependency>
  <groupid>javax.mail</groupid>
  <artifactid>mail</artifactid>
  <version>1.4.7</version>
 </dependency>

gradle依賴包

?
1
compile "javax.mail:mail:1.4.7"

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_20032995/article/details/72458636

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人在线观看网 | 午夜久久电影 | 91av99| 红杏网站永久免费视频入口 | 中文字幕在线观看视频一区 | 91专区在线观看 | 亚洲午夜激情网 | 久久久久亚洲视频 | 久久久久久久久久网 | 中国黄色一级生活片 | 日韩在线播放中文字幕 | 成人午夜在线观看视频 | 日本黄色片免费播放 | 黄污免费网站 | 欧美一级不卡视频 | 亚洲精品7777xxxx青睐 | 91性高湖久久久久久久久网站 | 国产另类一区 | 国产大片中文字幕在线观看 | xxxxhd18hd日本hd| 日本成人在线免费 | 亚洲国产精品一 | 久久恋 | 91九色网址 | omofun 动漫在线观看 | aa国产视频一区二区 | 色啪综合 | 中国免费一级毛片 | 亚洲国产视频网 | 日本中文字幕网址 | sm高h视频| 性欧美xxxx免费岛国不卡电影 | www.99av| 7777欧美| 国产精品免费一区二区三区都可以 | 91久久国产 | 午夜亚洲视频 | 成人在线视频免费看 | 亚洲精品久久久久久久久久久 | 最新亚洲国产 | 日韩激情一区二区三区 |