spring框架是由于軟件開發的復雜性而創建的。spring使用的是基本的javabean來完成以前只可能由ejb完成的事情。然而,spring的用途不僅僅限于服務器端的開發。從簡單性、可測試性和松耦合性角度而言,絕大部分java應用都可以從spring中受益。
首先進入自己的qq郵箱,在設置中修改賬戶信息
然后來至底部
點擊開啟,再用手機發送對應信息到指定號碼,然后點擊我已發送
獲取授權碼
注意提示:
到這里,相信你已經開通了smtp服務,這樣就可以在java code發送郵件了
接下來的是spring 中使用郵件服務
首先是配置信息使用的是587端口,剛開始用465端口我糾結了好久(使用465端口的錯誤詳情),用不了,你可以嘗試,默認的25端口應該也是不適合的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<!-- 郵件服務 --> <bean id= "mailsender" class = "org.springframework.mail.javamail.javamailsenderimpl" > <property name= "host" value= "smtp.qq.com" /> <property name= "port" value= "587" /> //或許你可以用465端口,默認的25不適合 <property name= "protocol" value= "smtp" /> <property name= "password" value= "xxxxxxxxxxxx" /> //這里的是你通過短信后,獲取的授權碼 <property name= "defaultencoding" value= "utf-8" /> <property name= "javamailproperties" > <props> <prop key= "mail.smtp.auth" > true </prop> <prop key= "mail.smtp.timeout" > 25000 </prop> </props> </property> </bean> <!-- this is a template message that we can pre-load with default state --> <bean id= "templatemessage" class = "org.springframework.mail.simplemailmessage" > <property name= "subject" value= "嘗試發郵件" /> </bean> <bean id= "ordermanager" class = "cn.cherish.common.simpleordermanager" > <property name= "mailsender" ref= "mailsender" /> <property name= "templatemessage" ref= "templatemessage" /> </bean> |
用maven引入的jar包
1
2
3
4
5
6
7
8
9
10
11
|
<!-- 郵件 --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupid>javax.mail</groupid> <artifactid>mail</artifactid> <version> 1.4 . 7 </version> </dependency> |
下面只是一個工具類作簡單例子,請勿見怪
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
|
package cn.cherish.common; import java.io.bufferedreader; import java.io.file; import java.io.inputstream; import java.io.inputstreamreader; import java.net.url; import java.net.urlconnection; import java.util.properties; import javax.mail.messagingexception; import javax.mail.internet.mimemessage; import org.springframework.core.io.filesystemresource; import org.springframework.mail.mailexception; import org.springframework.mail.simplemailmessage; import org.springframework.mail.javamail.javamailsenderimpl; import org.springframework.mail.javamail.mimemessagehelper; /** * 項目名稱:springmvc_hibernate * 類名稱:mailutil * 類描述: * 創建人:cherish * 聯系方式:[email protected] * 創建時間:2016年4月22日 下午3:51:48 * @version 1.0 */ public class mailutil { private static final string host = "smtp.qq.com" ; private static final string smtp = "smtp" ; private static final string password = "xxxxxxxxxx" ; private static final int port = 587 ; //587/465 private static final string defaultencoding = "utf-8" ; private static javamailsenderimpl senderimpl = new javamailsenderimpl(); private static properties prop = new properties(); static { // 設定mail server senderimpl.sethost(host); senderimpl.setprotocol(smtp); senderimpl.setusername(username); senderimpl.setpassword(password); senderimpl.setport(port); senderimpl.setdefaultencoding(defaultencoding); // 設定properties prop.put( "mail.smtp.auth" , "true" ); prop.put( "mail.smtp.timeout" , "25000" ); //設置調試模式可以在控制臺查看發送過程 prop.put( "mail.debug" , "true" ); senderimpl.setjavamailproperties(prop); } public static void main(string args[]) { // 設置收件人,寄件人 用數組發送多個郵件 // string[] array = new string[] {"[email protected]","[email protected]","[email protected]",username}; string[] array = new string[] {username}; string subject = "cherish內嵌圖片、音樂的郵件" ; // stringbuffer sb = new stringbuffer(); // try { // url url = new url("http://www.imooc.com/");//http://android-studio.org/ // // urlconnection conn = url.openconnection(); // inputstream is = conn.getinputstream(); // // bufferedreader reader = new bufferedreader(new inputstreamreader(is)); // // string string = null; // while ((string = reader.readline()) != null) { // sb.append(string); // } // // //system.out.println(sb.tostring()); // // } catch (exception e) { // e.printstacktrace(); // } // // boolean result = htmlmail(array, subject, sb.tostring()); string filepath = "e:/javaxmail.png" ; string html = "<html><head>" + "</head><body>" + "<audio src='http://m10.music.126.net/20160422225433/25b43b999bcdaf3425b9194514340596/ymusic/8c94/b9af/69e3/7ebe35b8e00154120822550b21b0c9c5.mp3' autoplay='autoplay' controls='controls' loop='-1'>愛你</audio>" + "<h1>hello,nice to meet you!</h1>" + "<span style='color:red;font-size:36px;'>并摸了一把你的小奶</span>" + "<img src='cid:javaxmail.png'>" + "</body></html>" ; boolean result = inlinefilemail(array, subject, html, filepath); if (result) { system.out.println( "發送郵件成功。。。。" ); } } /** * 發送簡單郵件 * @param to 收件人郵箱 * @param subject 主題 * @param content 內容 * @return */ public static boolean singlemail(string to, string subject, string content){ string[] array = new string[] {to}; return singlemail(array, subject, content); } /** * 發送簡單文本郵件 * @param to 收件人郵箱數組 * @param subject 主題 * @param content 內容 * @return */ public static boolean singlemail(string[] to, string subject, string content){ boolean result = true ; simplemailmessage mailmessage = new simplemailmessage(); // 設置收件人,寄件人 用數組發送多個郵件 mailmessage.setto(to); mailmessage.setfrom(username); mailmessage.setsubject(subject); mailmessage.settext(content); // 發送郵件 try { senderimpl.send(mailmessage); } catch (mailexception e) { e.printstacktrace(); result = false ; } return result; } /** * 發送html郵件 * @param to 收件人 * @param subject 主題 * @param html html代碼 * @return */ public static boolean htmlmail(string[] to, string subject, string html){ boolean result = true ; mimemessage mailmessage = senderimpl.createmimemessage(); mimemessagehelper messagehelper = new mimemessagehelper(mailmessage); try { // 設置收件人,寄件人 用數組發送多個郵件 messagehelper.setto(to); messagehelper.setfrom(username); messagehelper.setsubject(subject); // true 表示啟動html格式的郵件 messagehelper.settext(html, true ); // 發送郵件 senderimpl.send(mailmessage); } catch (messagingexception e) { result = false ; e.printstacktrace(); } return result; } /** * 發送內嵌圖片的郵件 (cid:資源名) * @param to 收件人郵箱 * @param subject 主題 * @param html html代碼 * @param imgpath 圖片路徑 * @return */ public static boolean inlinefilemail(string[] to, string subject, string html, string filepath){ boolean result = true ; mimemessage mailmessage = senderimpl.createmimemessage(); try { //設置true開啟嵌入圖片的功能 mimemessagehelper messagehelper = new mimemessagehelper(mailmessage, true ); // 設置收件人,寄件人 用數組發送多個郵件 messagehelper.setto(to); messagehelper.setfrom(username); messagehelper.setsubject(subject); // true 表示啟動html格式的郵件 messagehelper.settext(html, true ); filesystemresource file = new filesystemresource( new file(filepath)); messagehelper.addinline(file.getfilename(), file); // 發送郵件 senderimpl.send(mailmessage); } catch (messagingexception e) { result = false ; e.printstacktrace(); } return result; } /** * 發送帶附件的郵件 * @param to * @param subject * @param html * @param filepath * @return */ public static boolean attachedfilemail(string[] to, string subject, string html, string filepath){ boolean result = true ; mimemessage mailmessage = senderimpl.createmimemessage(); try { // multipart模式 為true時發送附件 可以設置html格式 mimemessagehelper messagehelper = new mimemessagehelper(mailmessage, true , "utf-8" ); // 設置收件人,寄件人 用數組發送多個郵件 messagehelper.setto(to); messagehelper.setfrom(username); messagehelper.setsubject(subject); // true 表示啟動html格式的郵件 messagehelper.settext(html, true ); filesystemresource file = new filesystemresource( new file(filepath)); // 這里的方法調用和插入圖片是不同的。 messagehelper.addattachment(file.getfilename(), file); // 發送郵件 senderimpl.send(mailmessage); } catch (messagingexception e) { result = false ; e.printstacktrace(); } return result; } |
溫馨提示:
<img src='cid:javaxmail.png'>
這是內嵌圖片的方式 javaxmail.png 要和 messagehelper.addinline(file.getfilename(), file);
這里的 file.getfilename()
相一致就可以
現在只差一步了,那就是ctrl + f11,有不當之處敬請提出,共同進步
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
|
** 使用javax.mail發郵件代碼 ** package cn.cherish.utils; import java.io.file; import java.io.filenotfoundexception; import java.io.ioexception; import java.util.date; import java.util.properties; import javax.activation.datahandler; import javax.activation.datasource; import javax.activation.filedatasource; import javax.mail.authenticator; import javax.mail.bodypart; import javax.mail.message; import javax.mail.messagingexception; import javax.mail.multipart; 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.mimemessage.recipienttype; import javax.mail.internet.mimemultipart; import javax.mail.internet.mimeutility; /** * 項目名稱:springmvc_hibernate * 類名稱:emailutil * 類描述:發送郵件工具類 * 創建人:cherish * 聯系方式:[email protected] * 創建時間:2016年4月23日 上午9:48:21 * @version 1.0 */ public class emailutil { // properties配置文件地址 //private static final string properties_path = "standard_data.properties"; private static session session; private static properties props = new properties(); private static final string host = "smtp.qq.com" ; private static int port = 587 ; private static final string isauth = "true" ; private static final string password = "xxxxxxxxxxxxxxxx" ; private static final string timeout = "25000" ; private static final string debug = "true" ; // 初始化session static { props.put( "mail.smtp.host" , host); props.put( "mail.smtp.port" , port); props.put( "mail.smtp.auth" , isauth); props.put( "fromer" , from); props.put( "username" , username); props.put( "password" , password); props.put( "mail.smtp.timeout" , timeout); props.put( "mail.debug" , debug); session = session.getinstance(props, new authenticator() { @override protected passwordauthentication getpasswordauthentication() { return new passwordauthentication(username, password); } }); } public static void main(string[] args) { try { string html = "<html><head>" + "</head><body>" + "<audio src='http://219.128.78.22/m10.music.126.net/20160423105749/3cee5688a7dc87d28a265fd992ecb0a2/ymusic/8c94/b9af/69e3/7ebe35b8e00154120822550b21b0c9c5.mp3?wshc_tag=1&wsts_tag=571aded1&wsid_tag=b73f773e&wsiphost=ipdbm' autoplay='autoplay' controls='controls' loop='-1'>愛你</audio>" + "<video controls='controls'>" + "<source src='http://v2.mukewang.com/45ad4643-87d7-444b-a3b9-fbf32de63811/h.mp4?auth_key=1461379796-0-0-e86cefa71cef963875fd68f8a419dd8a' type='video/mp4' />" + "your browser does not support the video tag." + "</video>" + "<h1>hello,nice to fuck you!</h1>" + "<span style='color:red;font-size:36px;'>并抓了一把你的小雞雞</span>" + "</body></html>" ; //sendemail("[email protected]", "yeah", html, true); } catch (exception e) { e.printstacktrace(); } } /** * * @title sendemail * @description 通過ishtml判斷發送的郵件的內容 * @param to 郵件接收者 * @param content 郵件內容 * @param ishtml 是否發送html * @throws messagingexception * @throws ioexception * @throws filenotfoundexception * @throws emailexception */ public static void sendemail(string to, string title, string content, boolean ishtml) throws filenotfoundexception, ioexception, messagingexception { string fromer = props.getproperty( "fromer" ); if (ishtml) { sendhtmlemail(fromer, to, title, content); } else { sendtextemail(fromer, to, title, content); } } // 發送純文字郵件 public static void sendtextemail(string from, string to, string subject, string content) throws filenotfoundexception, ioexception, messagingexception { message message = new mimemessage(session); message.setfrom( new internetaddress(from)); message.setrecipient(recipienttype.to, new internetaddress(to)); message.setsubject(subject); message.settext(content); message.setsentdate( new date()); transport.send(message); } // 發送有html格式郵件 public static void sendhtmlemail(string from, string to, string subject, string htmlconent) throws filenotfoundexception, ioexception, messagingexception { message message = new mimemessage(session); message.setfrom( new internetaddress(from)); message.setrecipient(recipienttype.to, new internetaddress(to)); message.setsubject(subject); message.setsentdate( new date()); multipart multi = new mimemultipart(); bodypart html = new mimebodypart(); html.setcontent(htmlconent, "text/html; charset=utf-8" ); multi.addbodypart(html); message.setcontent(multi); transport.send(message); } // 發送帶附件的郵件 public static void sendfileemail(string to, string subject, string htmlconent, file attachment) throws filenotfoundexception, ioexception, messagingexception { message message = new mimemessage(session); string fromer = props.getproperty( "fromer" ); message.setfrom( new internetaddress(fromer)); message.setrecipient(recipienttype.to, new internetaddress(to)); message.setsubject(subject); message.setsentdate( new date()); // 向multipart對象中添加郵件的各個部分內容,包括文本內容和附件 multipart multipart = new mimemultipart(); // 添加郵件正文 bodypart contentpart = new mimebodypart(); contentpart.setcontent(htmlconent, "text/html;charset=utf-8" ); multipart.addbodypart(contentpart); // 添加附件的內容 if (attachment != null ) { bodypart attachmentbodypart = new mimebodypart(); datasource source = new filedatasource(attachment); attachmentbodypart.setdatahandler( new datahandler(source)); // 網上流傳的解決文件名亂碼的方法,其實用mimeutility.encodeword就可以很方便的搞定 // 這里很重要,通過下面的base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼 // sun.misc.base64encoder enc = new sun.misc.base64encoder(); // messagebodypart.setfilename("=?gbk?b?" + // enc.encode(attachment.getname().getbytes()) + "?="); // mimeutility.encodeword可以避免文件名亂碼 attachmentbodypart.setfilename(mimeutility.encodeword(attachment.getname())); multipart.addbodypart(attachmentbodypart); } message.setcontent(multipart); transport.send(message); } } |
總結
以上所述是小編給大家介紹的使用spring框架中的組件發送郵件,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/langui/p/7802651.html