多發(fā)和用戶驗證
下面先來介紹如何實現(xiàn)將郵件發(fā)送給多個收件人和如何利用Authenticators對象實現(xiàn)用戶驗證。
指定收件人的時候,我們可以有兩種方法來指定。上篇博客是在發(fā)送郵件的時候臨時指定收件人,其實還可以在Message對象中指定。
1
|
message.addRecipient(Message.RecipientType.TO, new InternetAddress(” 995812509 @99 .com ”)); |
這個只是發(fā)送給一個收件人而言,但是有多個收件人如何處理?同樣有兩種方法來處理。
1、在發(fā)送郵件時Transport的sendMessage()方法指定收件人時是使用數(shù)組來指定收件人的,這個時候我們只需要多添加收件人地址即可完成。
2、在使用Message對象來添加收件人我們可以使用InternetAddress對象的parse(String string)方法,該方法返回的是InternetAddress數(shù)組,這樣同樣可以實現(xiàn)發(fā)送給多個收件人。
我們知道在進行JavaMail開發(fā)時我們必須要進行授權校驗,授權校驗目的是阻止他人任意亂發(fā)郵件,減少垃圾郵件的產(chǎn)生。
我們可以在獲取Session對象的時候進行校驗。在Session對象中有這兩個方法:
- getDefaultInstance(prop,authenticator),
- getInstance(prop,authenticator),
這兩個方法都有一個共同的參數(shù)authenticator,該參數(shù)是一個Authenticator對象。Authenticator對象就是幫助用戶進行信息驗證的,完成授權校驗。Authenticator對象中有getPasswordAuthentication()方法,該方法返回返回一個PasswordAuthentication對象,PasswordAuthentication對象中有兩個方法:getPassword()、getUserName()也就說我們將password、userName封裝在PasswordAuthentication對象,通過這兩個方法就可以獲取用戶名和密碼了。即可完成用戶信息驗證。
實例如下:
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
|
public class JavaMail_02 { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty( "mail.smtp.auth" , "true" ); props.setProperty( "mail.transport.protocol" , "smtp" ); props.setProperty( "mail.host" , "smtp.163.com" ); Session session = Session.getInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication( "********" , "*********" ); } }); session.setDebug( true ); Message msg = new MimeMessage(session); msg.setFrom( new InternetAddress( "chenssy995812509@163.com" )); msg.setSubject( "JavaMail測試程序..." ); msg.setContent( "<span style='color:red'>這是我的第二個javaMail測試程序....</span>" , "text/html;charset=gbk" ); //msg.setRecipients(RecipientType.TO, new Address[]{new InternetAddress("1111@@qq.com"),new InternetAddress("2222@qq.cpm")}); msg.setRecipients(RecipientType.TO, InternetAddress.parse( "995812509@qq.com,1247723213@qq.com" )); Transport.send(msg); } } |
帶有圖片和附件的郵件
在實際的電子郵件中我們一般都會涉及到更加復雜電子郵件結構,例如有附件、郵件正文里面包含圖片、包含歌曲等等,在這個時候我們就必須要對郵件的結構有著很清晰的認識。在進行復合郵件開發(fā)之前需要對復合郵件的結構有一定的了解。
上面這幅圖片展示了一封復合郵件的整體結構,我們可以看出一封復雜的電子郵件由多個部分組成。它有一個頭部和正文,但是正文并不是像以前那么簡單了,而是由幾個部分組成。頭部需要起到一個指示的作用,它需要說明正文需要使用什么樣的分隔符來分開,正文幾個部分之間使用什么樣的組合關系。對于上面電子郵件它由三個部分組成,每一部分都有自己頭和體,第一部分也由兩個部分組成。
復合郵件的組合關系:
正文部分之間有多種組合關系。組合關系如下圖:
alternative:選擇關系。上面的純文本和超文本之間就是一種選擇關系。
related:關聯(lián)關系。假如上面的超文本正文是展示一幅圖片,那么我們在發(fā)送郵件的時候必須要將這幅圖片包含到郵件中,也就是所謂的內嵌資源,這個內嵌資源是給超文本用的。所以他們兩者之間是一個關聯(lián)關系。
mixed:混合關系。在純文本、超文本和內嵌資源組成一個整體和,他們與附件并列著,兩者之間就是一個混合關系了。
復合郵件組織結構的API:
MimeMessage類表示整封電子郵件。
MimeBodyPart類表示郵件的一個MiME消息。
MimeMultipart類表示一個由多個MIME消息組合成的組合MIME消息。
下面一個實例:該郵件里面包含兩個附件、正文部分包括純文本和超文本,超文本表示展示一張圖片。源代碼如下:
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
|
public class JavaMail_03 { public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty( "mail.smtp.auth" , "true" ); props.setProperty( "mail.transport.protocol" , "smtp" ); props.setProperty( "mail.host" , "smtp.163.com" ); Session session = Session.getInstance(props, new Authenticator(){ protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication( "*****" , "******" ); } }); Message message = new MimeMessage(session); message.setSubject( "第三個JavaMail測試程序" ); message.setFrom( new InternetAddress( "\"" +MimeUtility.encodeText( "陳明" )+ "\"<chenssy995812509@163.com>" )); message.setRecipients(RecipientType.TO, new Address[]{ new InternetAddress( "995812509@qq.com" )}); //郵件正文 MimeMultipart multipart = new MimeMultipart( "mixed" ); message.setContent(multipart); /* * 創(chuàng)建郵件的內容 * 包括一個郵件正文和兩個附件 */ MimeBodyPart content = new MimeBodyPart(); //郵件內容 MimeBodyPart attch1 = new MimeBodyPart(); //附件1 MimeBodyPart attch2 = new MimeBodyPart(); //附件2 //將郵件內容添加到multipart中 multipart.addBodyPart(content); multipart.addBodyPart(attch1); multipart.addBodyPart(attch2); //設置附件1 DataSource ds1 = new FileDataSource("G:\\電子書\\oracle口令.txt"); DataHandler dh1 = new DataHandler(ds1); attch1.setDataHandler(dh1); attch1.setFileName("oracle.txt"); //設置附件2 DataSource ds2 = new FileDataSource("G:\\電子書\\賬號.txt"); DataHandler dh2 = new DataHandler(ds2); attch2.setDataHandler(dh2); attch2.setFileName(MimeUtility.encodeText("賬號.txt")); /* * 設置內容(正文)---是一個復雜體 * 包括HTML正文和顯示一張圖片 */ MimeMultipart bodyMultipart = new MimeMultipart( "related" ); content.setContent(bodyMultipart); //構造正文 MimeBodyPart htmlBody = new MimeBodyPart(); MimeBodyPart gifBody = new MimeBodyPart(); bodyMultipart.addBodyPart(htmlBody); bodyMultipart.addBodyPart(gifBody); //設置圖片 DataSource gifds = new FileDataSource( "F:\\圖片\\圖片\\4.jpg" ); DataHandler gifdh = new DataHandler(gifds); gifBody.setDataHandler(gifdh); gifBody.setHeader( "Content-ID" , "<" +gifds.getName()+ ">" ); //gifBody.setHeader("Content-Location", "http://www.jfrwli.cn/logo.gif"); //設置HTML正文 htmlBody.setContent( "<span style='color:red;font-size:16px'>這是我的第三個JavaMail測試哦!包括了附件和圖片,有點兒復雜...</span><br>" + "顯示的圖片<img src='cid:4.jpg'/>" , "text/html;charset=UTF-8" ); message.saveChanges(); //生成郵件 Transport.send(message); } } |