本文實例為大家分享了java web stmp發送帶附件郵件的具體代碼,供大家參考,具體內容如下
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; //系統屬性 private session session; //郵件會話對象 private mimemessage mimemsg; //mime郵件對象 private multipart mp; //multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成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 郵件發送服務器 */ 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(); } /** * 發送郵件 */ public boolean sendmail(string from, string[] to, string subject, string content, string filename) { try { //設置發信人 mimemsg.setfrom( new internetaddress(from)); //設置接收人 for ( int i = 0 ; i < to.length; i++) { mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i])); } //設置抄送人 // for (int i = 0; i < copyto.length; i++) { // mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i])); // } //設置主題 mimemsg.setsubject(subject); //設置正文 bodypart bp = new mimebodypart(); bp.setcontent(content, "text/html;charset=utf-8" ); mp.addbodypart(bp); //設置附件 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(); //發送郵件 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( "郵件發送成功" ); } 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 = "發送的郵箱賬號" ; string password = "發送的郵箱密碼" ; string from = "發送的郵箱" ; string[] to = { "接收郵件的郵箱" }; // string[] copyto = {"抄送的郵箱"}; string subject = "主題6" ; string content = "郵件內容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; //系統屬性 private session session; //郵件會話對象 private mimemessage mimemsg; //mime郵件對象 private multipart mp; //multipart對象,郵件內容,標題,附件等內容均添加到其中后再生成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 郵件發送服務器 */ 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(); } /** * 發送郵件 */ public boolean sendmail(string from, string[] to, string subject, string content, string filename) { try { //設置發信人 mimemsg.setfrom( new internetaddress(from)); //設置接收人 for ( int i = 0 ; i < to.length; i++) { mimemsg.setrecipients(message.recipienttype.to, internetaddress.parse(to[i])); } //設置抄送人 // for (int i = 0; i < copyto.length; i++) { // mimemsg.setrecipients(message.recipienttype.cc, internetaddress.parse(copyto[i])); // } //設置主題 mimemsg.setsubject(subject); //設置正文 bodypart bp = new mimebodypart(); bp.setcontent(content, "text/html;charset=utf-8" ); mp.addbodypart(bp); //設置附件 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(); //發送郵件 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( "郵件發送成功" ); } 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 = "誰去發"; // string[] to = {"發給誰"}; //// string[] copyto = {"抄送的郵箱"}; // string subject = "huawei"; // string content = "郵件內容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; //發送人 private string[] to; //接收人 // string[] copyto = {"[email protected]"}; private string subject; //郵件主題 private string content; //郵件內容 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" |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_20032995/article/details/72458636