上一篇文章 基于 JavaMail 的 Java 郵件發(fā)送:簡單郵件發(fā)送 講解了郵件的基本協(xié)議,JavaMail組件,創(chuàng)建并發(fā)送一封簡單郵件的詳細(xì)步驟。本文將介紹如何創(chuàng)建并發(fā)送一封包含圖片和附件的復(fù)雜郵件。
一封復(fù)雜的郵件內(nèi)容可以看做是由很多節(jié)點(或者可以說是“片段”/“部分”/“零件”)組成,文本、圖片、附件等都可以看成是郵件內(nèi)容中的一個節(jié)點。這些節(jié)點之間又可以相互關(guān)聯(lián)組合成一個節(jié)點。最終組合成一個大節(jié)點就是郵件的正文內(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
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
|
package com.xiets.javamaildemo; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message.RecipientType; 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; /** * 創(chuàng)建并發(fā)送一封包含文本、圖片、附件的復(fù)雜郵件 * * JavaMail 版本: 1.6.0 * JDK 版本: JDK 1.7 以上(必須) * * @author xietansheng */ public class Main { // 發(fā)件人的 郵箱 和 密碼(替換為自己的郵箱和密碼) public static String myEmailAccount = "xxxxxxxx@163.com" ; public static String myEmailPassword = "xxxxxxxx" ; // 發(fā)件人郵箱的 SMTP 服務(wù)器地址, 必須準(zhǔn)確, 不同郵件服務(wù)器地址不同, 一般格式為: smtp.xxx.com // 網(wǎng)易163郵箱的 SMTP 服務(wù)器地址為: smtp.163.com public static String myEmailSMTPHost = "smtp.163.com" ; // 收件人郵箱(替換為自己知道的有效郵箱) public static String receiveMailAccount = "xxxxxxxxx@qq.com" ; public static void main(String[] args) throws Exception { // 1. 創(chuàng)建參數(shù)配置, 用于連接郵件服務(wù)器的參數(shù)配置 Properties props = new Properties(); // 參數(shù)配置 props.setProperty( "mail.transport.protocol" , "smtp" ); // 使用的協(xié)議(JavaMail規(guī)范要求) props.setProperty( "mail.smtp.host" , myEmailSMTPHost); // 發(fā)件人的郵箱的 SMTP 服務(wù)器地址 props.setProperty( "mail.smtp.auth" , "true" ); // 需要請求認(rèn)證 // 開啟 SSL 連接, 以及更詳細(xì)的發(fā)送步驟請看上一篇: 基于 JavaMail 的 Java 郵件發(fā)送:簡單郵件發(fā)送 // 2. 根據(jù)配置創(chuàng)建會話對象, 用于和郵件服務(wù)器交互 Session session = Session.getDefaultInstance(props); session.setDebug( true ); // 設(shè)置為debug模式, 可以查看詳細(xì)的發(fā)送 log // 3. 創(chuàng)建一封郵件 MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount); // 也可以保持到本地查看 // message.writeTo(file_out_put_stream); // 4. 根據(jù) Session 獲取郵件傳輸對象 Transport transport = session.getTransport(); // 5. 使用 郵箱賬號 和 密碼 連接郵件服務(wù)器 // 這里認(rèn)證的郵箱必須與 message 中的發(fā)件人郵箱一致,否則報錯 transport.connect(myEmailAccount, myEmailPassword); // 6. 發(fā)送郵件, 發(fā)到所有的收件地址, message.getAllRecipients() 獲取到的是在創(chuàng)建郵件對象時添加的所有收件人, 抄送人, 密送人 transport.sendMessage(message, message.getAllRecipients()); // 7. 關(guān)閉連接 transport.close(); } /** * 創(chuàng)建一封復(fù)雜郵件(文本+圖片+附件) */ public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail) throws Exception { // 1. 創(chuàng)建郵件對象 MimeMessage message = new MimeMessage(session); // 2. From: 發(fā)件人 message.setFrom( new InternetAddress(sendMail, "我的測試郵件_發(fā)件人昵稱" , "UTF-8" )); // 3. To: 收件人(可以增加多個收件人、抄送、密送) message.addRecipient(RecipientType.TO, new InternetAddress(receiveMail, "我的測試郵件_收件人昵稱" , "UTF-8" )); // 4. Subject: 郵件主題 message.setSubject( "TEST郵件主題(文本+圖片+附件)" , "UTF-8" ); /* * 下面是郵件內(nèi)容的創(chuàng)建: */ // 5. 創(chuàng)建圖片“節(jié)點” MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler( new FileDataSource( "FairyTail.jpg" )); // 讀取本地文件 image.setDataHandler(dh); // 將圖片數(shù)據(jù)添加到“節(jié)點” image.setContentID( "image_fairy_tail" ); // 為“節(jié)點”設(shè)置一個唯一編號(在文本“節(jié)點”將引用該ID) // 6. 創(chuàng)建文本“節(jié)點” MimeBodyPart text = new MimeBodyPart(); // 這里添加圖片的方式是將整個圖片包含到郵件內(nèi)容中, 實際上也可以以 http 鏈接的形式添加網(wǎng)絡(luò)圖片 text.setContent( "這是一張圖片<br/><img src='cid:image_fairy_tail'/>" , "text/html;charset=UTF-8" ); // 7. (文本+圖片)設(shè)置 文本 和 圖片 “節(jié)點”的關(guān)系(將 文本 和 圖片 “節(jié)點”合成一個混合“節(jié)點”) MimeMultipart mm_text_image = new MimeMultipart(); mm_text_image.addBodyPart(text); mm_text_image.addBodyPart(image); mm_text_image.setSubType( "related" ); // 關(guān)聯(lián)關(guān)系 // 8. 將 文本+圖片 的混合“節(jié)點”封裝成一個普通“節(jié)點” // 最終添加到郵件的 Content 是由多個 BodyPart 組成的 Multipart, 所以我們需要的是 BodyPart, // 上面的 mm_text_image 并非 BodyPart, 所有要把 mm_text_image 封裝成一個 BodyPart MimeBodyPart text_image = new MimeBodyPart(); text_image.setContent(mm_text_image); // 9. 創(chuàng)建附件“節(jié)點” MimeBodyPart attachment = new MimeBodyPart(); DataHandler dh2 = new DataHandler( new FileDataSource( "妖精的尾巴目錄.doc" )); // 讀取本地文件 attachment.setDataHandler(dh2); // 將附件數(shù)據(jù)添加到“節(jié)點” attachment.setFileName(MimeUtility.encodeText(dh2.getName())); // 設(shè)置附件的文件名(需要編碼) // 10. 設(shè)置(文本+圖片)和 附件 的關(guān)系(合成一個大的混合“節(jié)點” / Multipart ) MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text_image); mm.addBodyPart(attachment); // 如果有多個附件,可以創(chuàng)建多個多次添加 mm.setSubType( "mixed" ); // 混合關(guān)系 // 11. 設(shè)置整個郵件的關(guān)系(將最終的混合“節(jié)點”作為郵件的內(nèi)容添加到郵件對象) message.setContent(mm); // 12. 設(shè)置發(fā)件時間 message.setSentDate( new Date()); // 13. 保存上面的所有設(shè)置 message.saveChanges(); return message; } } |
發(fā)送后查看收件人的收件箱:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/xietansheng/article/details/51722660