本文實例講述了基于JavaMail API收發(fā)郵件的方法。分享給大家供大家參考。具體如下:
1.JavaMail API按其功能劃分通常可分為如下三大類
(1)創(chuàng)建和解析郵件內(nèi)容的API :Message類是創(chuàng)建和解析郵件的核心API,它的實例對象代表一封電子郵件。
(2)發(fā)送郵件的API:Transport類是發(fā)送郵件的核心API類,它的實例對象代表實現(xiàn)了某個郵件發(fā)送協(xié)議的郵件發(fā)送對象,例如SMTP協(xié)議。
(3)接收郵件的API:Store類是接收郵件的核心API類,它的實例對象代表實現(xiàn)了某個郵件接收協(xié)議的郵件接收對象,例如POP3協(xié)議。
2. Session類
Session類用于定義整個應用程序所需的環(huán)境信息,以及收集客戶端與郵件服務器建立網(wǎng)絡連接的會話信息,如郵件服務器的主機名、端口號、采用的郵件發(fā)送和接收協(xié)議等。Session對象根據(jù)這些信息構建用于郵件收發(fā)的Transport和Store對象,以及為客戶端創(chuàng)建Message對象時提供信息支持。
3.使用JavaMail發(fā)送一封簡單的郵件
創(chuàng)建包含郵件服務器的網(wǎng)絡連接信息的Session對象。
創(chuàng)建代表郵件內(nèi)容的Message對象。
創(chuàng)建Transport對象、連接服務器、發(fā)送Message、關閉連接。
4.實例
(1)JavaMail郵件只發(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
|
public class SendMail { public static void main(String[] args) throws Exception { //屬性文件 Properties props= new Properties(); //設置郵件的發(fā)送主機名、發(fā)送協(xié)議和是否驗證 props.setProperty( "mail.host" , "smtp.sohu.com" ); props.setProperty( "mail.transport.protocol" , "smtp" ); props.setProperty( "mail.smtp.auth" , "true" ); //獲取發(fā)送接收郵件環(huán)境的對象 Session session=Session.getInstance(props); Message message=createMessage(session); //創(chuàng)建發(fā)送郵件的對象 Transport tsp=session.getTransport(); tsp.connect( "jb51" , "jb51" ); tsp.sendMessage(message, message.getAllRecipients()); tsp.close(); } public static Message createMessage(Session session) throws Exception{ //根據(jù)環(huán)境對象創(chuàng)建一份郵件 MimeMessage message= new MimeMessage(session); //設置郵件屬性 message.setSubject( "hello" ); //創(chuàng)建郵件體 MimeBodyPart text= new MimeBodyPart(); text.setContent( "你好?" , "text/html;charset=utf-8" ); //設置描述關系 MimeMultipart mm= new MimeMultipart(); mm.addBodyPart(text); message.setContent(mm); message.saveChanges(); return message; } } |
(2)JavaMail郵件發(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
|
public class SendImageMail { public static void main(String[] args) throws Exception { //屬性文件 Properties props= new Properties(); //設置郵件的發(fā)送主機名、發(fā)送協(xié)議和是否驗證 props.setProperty( "mail.host" , "smtp.sohu.com" ); props.setProperty( "mail.transport.protocol" , "smtp" ); props.setProperty( "mail.smtp.auth" , "true" ); //獲取發(fā)送接收郵件環(huán)境的對象 Session session=Session.getInstance(props); Message message=createMessage(session); //創(chuàng)建發(fā)送郵件的對象 Transport tsp=session.getTransport(); tsp.connect( "jb51" , "jb51" ); tsp.sendMessage(message, message.getAllRecipients()); tsp.close(); } public static Message createMessage(Session session) throws Exception{ MimeMessage message= new MimeMessage(session); message.setSubject( "圖片" ); MimeBodyPart text= new MimeBodyPart(); text.setContent( "好看嗎?<br/><img src='cid:xx.jpg'>" , "text/html;charset=utf-8" ); MimeBodyPart image= new MimeBodyPart(); image.setDataHandler( new DataHandler( new FileDataSource( "src//f.jpg" ))); image.setContentID( "xx.jpg" ); MimeMultipart mm= new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType( "related" ); message.setContent(mm); message.saveChanges(); return message; } } |
(3)JavaMail郵件發(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
|
public class SendAttchImageMail { public static void main(String[] args) throws Exception { //屬性文件 Properties props= new Properties(); //設置郵件的發(fā)送主機名、發(fā)送協(xié)議和是否驗證 props.setProperty( "mail.host" , "smtp.sohu.com" ); props.setProperty( "mail.transport.protocol" , "smtp" ); props.setProperty( "mail.smtp.auth" , "true" ); //獲取發(fā)送接收郵件環(huán)境的對象 Session session=Session.getInstance(props); Message message=createMessage(session); //創(chuàng)建發(fā)送郵件的對象 Transport tsp=session.getTransport(); tsp.connect( "jb51" , "jb51" ); tsp.sendMessage(message, message.getAllRecipients()); tsp.close(); } public static Message createMessage(Session session) throws Exception{ MimeMessage message= new MimeMessage(session); message.setSubject( "圖片" ); MimeBodyPart text= new MimeBodyPart(); text.setContent( "好看嗎?<br/><img src='cid:xx.jpg'>" , "text/html;charset=utf-8" ); MimeBodyPart image= new MimeBodyPart(); image.setDataHandler( new DataHandler( new FileDataSource( "src//f.jpg" ))); image.setContentID( "xx.jpg" ); MimeBodyPart attch= new MimeBodyPart(); DataHandler dh= new DataHandler( new FileDataSource( "src//默默無 語.mp3" )); attch.setDataHandler(dh); String name=dh.getName(); attch.setFileName(MimeUtility.encodeText(name)); MimeMultipart mm= new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType( "related" ); MimeBodyPart part= new MimeBodyPart(); part.setContent(mm); MimeMultipart m= new MimeMultipart(); m.addBodyPart(part); m.addBodyPart(attch); m.setSubType( "mixed" ); message.setContent(m); message.saveChanges(); return message; } } |
注:郵箱地址必須是真實的
希望本文所述對大家的java程序設計有所幫助。