一、RFC882文檔簡單說明
RFC882文檔規(guī)定了如何編寫一封簡單的郵件(純文本郵件),一封簡單的郵件包含郵件頭和郵件體兩個部分,郵件頭和郵件體之間使用空行分隔。
郵件頭包含的內(nèi)容有:
from字段 --用于指明發(fā)件人
to字段 --用于指明收件人
subject字段 --用于說明郵件主題
cc字段 -- 抄送,將郵件發(fā)送給收件人的同時抄送給另一個收件人,收件人可以看到郵件抄送給了誰
bcc字段 -- 密送,將郵件發(fā)送給收件人的同時將郵件秘密發(fā)送給另一個收件人,收件人無法看到郵件密送給了誰
郵件體指的就是郵件的具體內(nèi)容。
二、MIME協(xié)議簡單介紹
在我們的實際開發(fā)當中,一封郵件既可能包含圖片,又可能包含有附件,在這樣的情況下,RFC882文檔規(guī)定的郵件格式就無法滿足要求了。
MIME協(xié)議是對RFC822文檔的升級和補充,它描述了如何生產(chǎn)一封復雜的郵件。通常我們把MIME協(xié)議描述的郵件稱之為MIME郵件。MIME協(xié)議描述的數(shù)據(jù)稱之為MIME消息。
對于一封復雜郵件,如果包含了多個不同的數(shù)據(jù),MIME協(xié)議規(guī)定了要使用分隔線對多段數(shù)據(jù)進行分隔,并使用Content-Type頭字段對數(shù)據(jù)的類型、以及多個數(shù)據(jù)之間的關系進行描述。
三、使用JavaMail創(chuàng)建郵件和發(fā)送郵件
JavaMail創(chuàng)建的郵件是基于MIME協(xié)議的。因此可以使用JavaMail創(chuàng)建出包含圖片,包含附件的復雜郵件。
3.1、JavaMail API的簡單介紹
3.2、創(chuàng)建郵件發(fā)送測試項目
3.3、發(fā)送一封只包含文本的簡單郵件
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
|
package me.gacl.main; import java.util.Properties; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * @ClassName: Sendmail * @Description: 發(fā)送Email * @author: 孤傲蒼狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty( "mail.host" , "smtp.sohu.com" ); prop.setProperty( "mail.transport.protocol" , "smtp" ); prop.setProperty( "mail.smtp.auth" , "true" ); //使用JavaMail發(fā)送郵件的5個步驟 //1、創(chuàng)建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài) session.setDebug( true ); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、使用郵箱的用戶名和密碼連上郵件服務器,發(fā)送郵件時,發(fā)件人需要提交郵箱的用戶名和密碼給smtp服務器,用戶名和密碼都通過驗證之后才能夠正常發(fā)送郵件給收件人。 ts.connect( "smtp.sohu.com" , "gacl" , "郵箱密碼" ); //4、創(chuàng)建郵件 Message message = createSimpleMail(session); //5、發(fā)送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createSimpleMail * @Description: 創(chuàng)建一封只包含文本的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createSimpleMail(Session session) throws Exception { //創(chuàng)建郵件對象 MimeMessage message = new MimeMessage(session); //指明郵件的發(fā)件人 message.setFrom( new InternetAddress( "gacl@sohu.com" )); //指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā) message.setRecipient(Message.RecipientType.TO, new InternetAddress( "gacl@sohu.com" )); //郵件的標題 message.setSubject( "只包含文本的簡單郵件" ); //郵件的文本內(nèi)容 message.setContent( "你好啊!" , "text/html;charset=UTF-8" ); //返回創(chuàng)建好的郵件對象 return message; } } |
3.4、發(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
|
package me.gacl.main; import java.io.FileOutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; 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; /** * @ClassName: Sendmail * @Description: 發(fā)送Email * @author: 孤傲蒼狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty( "mail.host" , "smtp.sohu.com" ); prop.setProperty( "mail.transport.protocol" , "smtp" ); prop.setProperty( "mail.smtp.auth" , "true" ); //使用JavaMail發(fā)送郵件的5個步驟 //1、創(chuàng)建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài) session.setDebug( true ); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、連上郵件服務器,需要發(fā)件人提供郵箱的用戶名和密碼進行驗證 ts.connect( "smtp.sohu.com" , "gacl" , "郵箱密碼" ); //4、創(chuàng)建郵件 Message message = createImageMail(session); //5、發(fā)送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createImageMail * @Description: 生成一封郵件正文帶圖片的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createImageMail(Session session) throws Exception { //創(chuàng)建郵件 MimeMessage message = new MimeMessage(session); // 設置郵件的基本信息 //發(fā)件人 message.setFrom( new InternetAddress( "gacl@sohu.com" )); //收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress( "xdp_gacl@sina.cn" )); //郵件標題 message.setSubject( "帶圖片的郵件" ); // 準備郵件數(shù)據(jù) // 準備郵件正文數(shù)據(jù) MimeBodyPart text = new MimeBodyPart(); text.setContent( "這是一封郵件正文帶圖片<img src='cid:xxx.jpg'>的郵件" , "text/html;charset=UTF-8" ); // 準備圖片數(shù)據(jù) MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler( new FileDataSource( "src\\1.jpg" )); image.setDataHandler(dh); image.setContentID( "xxx.jpg" ); // 描述數(shù)據(jù)關系 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType( "related" ); message.setContent(mm); message.saveChanges(); //將創(chuàng)建好的郵件寫入到E盤以文件的形式進行保存 message.writeTo( new FileOutputStream( "E:\\ImageMail.eml" )); //返回創(chuàng)建好的郵件 return message; } } |
3.5、發(fā)送包含附件的郵件
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
|
package me.gacl.main; import java.io.FileOutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; 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; /** * @ClassName: Sendmail * @Description: 發(fā)送Email * @author: 孤傲蒼狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty( "mail.host" , "smtp.sohu.com" ); prop.setProperty( "mail.transport.protocol" , "smtp" ); prop.setProperty( "mail.smtp.auth" , "true" ); //使用JavaMail發(fā)送郵件的5個步驟 //1、創(chuàng)建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài) session.setDebug( true ); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、連上郵件服務器 ts.connect( "smtp.sohu.com" , "gacl" , "郵箱密碼" ); //4、創(chuàng)建郵件 Message message = createAttachMail(session); //5、發(fā)送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createAttachMail * @Description: 創(chuàng)建一封帶附件的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createAttachMail(Session session) throws Exception{ MimeMessage message = new MimeMessage(session); //設置郵件的基本信息 //發(fā)件人 message.setFrom( new InternetAddress( "gacl@sohu.com" )); //收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress( "xdp_gacl@sina.cn" )); //郵件標題 message.setSubject( "JavaMail郵件發(fā)送測試" ); //創(chuàng)建郵件正文,為了避免郵件正文中文亂碼問題,需要使用charset=UTF-8指明字符編碼 MimeBodyPart text = new MimeBodyPart(); text.setContent( "使用JavaMail創(chuàng)建的帶附件的郵件" , "text/html;charset=UTF-8" ); //創(chuàng)建郵件附件 MimeBodyPart attach = new MimeBodyPart(); DataHandler dh = new DataHandler( new FileDataSource( "src\\2.jpg" )); attach.setDataHandler(dh); attach.setFileName(dh.getName()); // //創(chuàng)建容器描述數(shù)據(jù)關系 MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(text); mp.addBodyPart(attach); mp.setSubType( "mixed" ); message.setContent(mp); message.saveChanges(); //將創(chuàng)建的Email寫入到E盤存儲 message.writeTo( new FileOutputStream( "E:\\attachMail.eml" )); //返回生成的郵件 return message; } } |
3.6、發(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
109
110
111
112
113
114
|
package me.gacl.main; import java.io.FileOutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; 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; /** * @ClassName: Sendmail * @Description: 發(fā)送Email * @author: 孤傲蒼狼 * @date: 2015-1-12 下午9:42:56 * */ public class Sendmail { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.setProperty( "mail.host" , "smtp.sohu.com" ); prop.setProperty( "mail.transport.protocol" , "smtp" ); prop.setProperty( "mail.smtp.auth" , "true" ); //使用JavaMail發(fā)送郵件的5個步驟 //1、創(chuàng)建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài) session.setDebug( true ); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、連上郵件服務器 ts.connect( "smtp.sohu.com" , "gacl" , "郵箱密碼" ); //4、創(chuàng)建郵件 Message message = createMixedMail(session); //5、發(fā)送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createMixedMail * @Description: 生成一封帶附件和帶圖片的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createMixedMail(Session session) throws Exception { //創(chuàng)建郵件 MimeMessage message = new MimeMessage(session); //設置郵件的基本信息 message.setFrom( new InternetAddress( "gacl@sohu.com" )); message.setRecipient(Message.RecipientType.TO, new InternetAddress( "xdp_gacl@sina.cn" )); message.setSubject( "帶附件和帶圖片的的郵件" ); //正文 MimeBodyPart text = new MimeBodyPart(); text.setContent( "xxx這是女的xxxx<br/><img src='cid:aaa.jpg'>" , "text/html;charset=UTF-8" ); //圖片 MimeBodyPart image = new MimeBodyPart(); image.setDataHandler( new DataHandler( new FileDataSource( "src\\3.jpg" ))); image.setContentID( "aaa.jpg" ); //附件1 MimeBodyPart attach = new MimeBodyPart(); DataHandler dh = new DataHandler( new FileDataSource( "src\\4.zip" )); attach.setDataHandler(dh); attach.setFileName(dh.getName()); //附件2 MimeBodyPart attach2 = new MimeBodyPart(); DataHandler dh2 = new DataHandler( new FileDataSource( "src\\波子.zip" )); attach2.setDataHandler(dh2); attach2.setFileName(MimeUtility.encodeText(dh2.getName())); //描述關系:正文和圖片 MimeMultipart mp1 = new MimeMultipart(); mp1.addBodyPart(text); mp1.addBodyPart(image); mp1.setSubType( "related" ); //描述關系:正文和附件 MimeMultipart mp2 = new MimeMultipart(); mp2.addBodyPart(attach); mp2.addBodyPart(attach2); //代表正文的bodypart MimeBodyPart content = new MimeBodyPart(); content.setContent(mp1); mp2.addBodyPart(content); mp2.setSubType( "mixed" ); message.setContent(mp2); message.saveChanges(); message.writeTo( new FileOutputStream( "E:\\MixedMail.eml" )); //返回創(chuàng)建好的的郵件 return message; } } |
以上就是使用JavaMail的API創(chuàng)建郵件和發(fā)送郵件的全部內(nèi)容,希望對大家的學習有所幫助。