Java郵件開發—–JavaMail(三)

來源:互聯網
上載者:User

         前面兩篇部落格都只是最基本的郵件最簡單的郵件發送了,在實際的電子郵件中我們一般都會涉及到更加複雜電子郵件結構,例如有附件、郵件內文裡麵包含圖片、包含歌曲等等,在這個時候我們就必須要對郵件的結構有著很清晰的認識。在進行複合郵件開發之前需要對複合郵件的結構有一定的瞭解。

        複合郵件的整體結構

 

         上面這幅圖片展示了一封複合郵件的整體結構,我們可以看出一封複雜的電子郵件由多個部分組成。它有一個頭部和本文,但是本文並不是像以前那麼簡單了,而是由幾個部分組成。頭部需要起到一個指示的作用,它需要說明本文需要使用什麼樣的分隔字元來分開,本文幾個部分之間使用什麼樣的組合關係。對於上面電子郵件它由三個部分組成,每一部分都有自己頭和體,第一部分也由兩個部分組成。

 

         複合郵件的組合關係

         本文部分之間有多種組合關係。組合關係如:

        alternative:選擇關係。上面的純文字和超文本之間就是一種選擇關係。

        related:關聯關係。假如上面的超文本本文是展示一幅圖片,那麼我們在發送郵件的時候必須要將這幅圖片包含到郵件中,也就是所謂的內嵌資源,這個內嵌資源是給超文本用的。所以他們兩者之間是一個關聯關係。

        mixed:混合關係。在純文字、超文本和內嵌資源群組成一個整體和,他們與附件並列著,兩者之間就是一個混合關係了。

 

        複合郵件組織圖的API

 

        MimeMessage類表示整封電子郵件。

        MimeBodyPart類表示郵件的一個MiME訊息。

        MimeMultipart類表示一個由多個MIME訊息組合成的組合MIME訊息。

 

        下面一個執行個體:該郵件裡麵包含兩個附件、本文部分包括純文字和超文本,超文本表示展示一張圖片。原始碼如下:

 1 public class JavaMail_03 { 2  3     public static void main(String[] args) throws Exception { 4         Properties props = new Properties(); 5         props.setProperty("mail.smtp.auth", "true"); 6         props.setProperty("mail.transport.protocol", "smtp"); 7         props.setProperty("mail.host", "smtp.163.com"); 8         Session session = Session.getInstance(props, 9                 new Authenticator(){10                     protected PasswordAuthentication getPasswordAuthentication(){11                         return new PasswordAuthentication("*****","******");12                     }13         });14         15         Message message = new MimeMessage(session);16         message.setSubject("第三個JavaMail測試程式");17         message.setFrom(new InternetAddress("\""+MimeUtility.encodeText("陳明")+"\"<chenssy995812509@163.com>"));18         message.setRecipients(RecipientType.TO, new Address[]{new InternetAddress("995812509@qq.com")});19         20         //郵件內文21         MimeMultipart multipart = new MimeMultipart("mixed");22         message.setContent(multipart);23         /*24          * 建立郵件的內容25          * 包括一個郵件內文和兩個附件26          */27         MimeBodyPart content = new MimeBodyPart();      //郵件內容28         MimeBodyPart attch1 = new MimeBodyPart();      //附件129         MimeBodyPart attch2 = new MimeBodyPart();      //附件230         //將郵件內容添加到multipart中31         multipart.addBodyPart(content);32         multipart.addBodyPart(attch1);33         multipart.addBodyPart(attch2);34         35         //設定附件136         DataSource ds1 = new FileDataSource("G:\\電子書\\oracle口令.txt");37         DataHandler dh1 = new DataHandler(ds1);38         attch1.setDataHandler(dh1);39         attch1.setFileName("oracle.txt");40         //設定附件241         DataSource ds2 = new FileDataSource("G:\\電子書\\帳號.txt");42         DataHandler dh2 = new DataHandler(ds2);43         attch2.setDataHandler(dh2);44         attch2.setFileName(MimeUtility.encodeText("帳號.txt"));45         /*46          * 設定內容(本文)---是一個複雜體47          * 包括HTML本文和顯示一張圖片48          */49         MimeMultipart bodyMultipart = new MimeMultipart("related");50         content.setContent(bodyMultipart);51         //構造本文52         MimeBodyPart htmlBody = new MimeBodyPart();53         MimeBodyPart gifBody = new MimeBodyPart();54         bodyMultipart.addBodyPart(htmlBody);55         bodyMultipart.addBodyPart(gifBody);56     57         //設定圖片58         DataSource gifds = new FileDataSource("F:\\圖片\\圖片\\4.jpg");59         DataHandler gifdh = new DataHandler(gifds);60         gifBody.setDataHandler(gifdh);61         gifBody.setHeader("Content-ID", "<"+gifds.getName()+">");62         //gifBody.setHeader("Content-Location", "http://www.itcast.cn/logo.gif");63         //設定HTML本文64         htmlBody.setContent("<span style='color:red;font-size:16px'>這是我的第三個JavaMail測試哦!包括了附件和圖片,有點兒複雜...</span><br>" +65                 "顯示的圖片<img src='cid:4.jpg'/>", "text/html;charset=UTF-8");66         67         68         message.saveChanges();        //產生郵件69         Transport.send(message);70     }71 72 }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.