組件實現email

在 apache 站點有一個 jarkata/commons/email 子項目,爲我們實現了發送 Email 的功能,在 http://jakarta.apache.org/commons/email/ 把包 commons-email-1.0.jar 下來,自己要寫的代碼就十分少了,並且非常明瞭。

這個包的大小隻有23K,也就是9個類而已,卻能讓您省不少事。
還需要mail.jar&activation.jar(google去找o(∩_∩)o...)commons-email是apache提供的一個開源的API,是對javamail的封裝,因此在使用時要將javamail.jar加到 class path中,主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四個類。

SimpleEmail:發送簡單的email,不能添加附件
MultiPartEmail:文本郵件,可以添加多個附件
HtmlEmail:HTML格式郵件,同時具有MultiPartEmail類所有“功能”
EmailAttchment:附件類,可以添加本地資源,也可以指定網絡上資源,在發送時自動將網絡上資源下載發送。

 

SimpleEmail email = new SimpleEmail();
//smtp host
email.setHostName("mail.test.com");
//登陸郵件服務器的用戶名和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("[email protected]", "John Doe");
//發送人
email.setFrom("[email protected]", "Me");
//標題
email.setSubject("Test message");
//郵件內容
email.setMsg("This is a simple test of commons-email");
//發送
email.send();

發送文本格式,帶附件郵件:
==================
//附件,可以定義多個附件對象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e:\\1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host
email.setHostName("mail.test.com");
//登陸郵件服務器的用戶名和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("[email protected]", "John Doe");
//發送人
email.setFrom("[email protected]", "Me");
//標題
email.setSubject("Test message");
//郵件內容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//發送
email.send();

發送HTML格式帶附件郵件:
=================
//附件,可以定義多個附件對象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e:\\1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host
email.setHostName("mail.test.com");
//登陸郵件服務器的用戶名和密碼
email.setAuthentication("test","testpassword");
//接收人
email.addTo("[email protected]", "John Doe");
//發送人
email.setFrom("[email protected]", "Me");
//標題
email.setSubject("Test message");
//郵件內容
email.setHtmlMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//發送
下面提供一個完整的程序示例:

package zieckey

importorg.apache.commons.mail.*;

publicclass SendEMail
{
    publicstaticvoid main (String[] arg )throwsException
    {
         SimpleEmail email =new SimpleEmail ();


        // smtp host
         email.setHostName ("smtp.163.com");
        // 登陸郵件服務器的用戶名和密碼
         email.setAuthentication ("zieckey","123456");
        // 接收人
         email.addTo("[email protected]","Zieckey");
        // 發送人
         email.setFrom ("[email protected]","Me");
        // 標題
         email.setSubject("Test message");
        // 郵件內容
         email.setMsg ("This is a simple test of commons-email");
        // 發送
         email.send();
        
        System.out.println("Send email successful!");

    }
}

commons-email 提供了 SimpleEmail、MultiPartEmail、HtmlEmail、EmailAttachment 等類,只需要您按正常思維簡單的寫幾行代碼就能發各種類型的 Email,一般我們用 JavaMail 發送 Email 會碰到中文亂碼問題,主要是出現在把代碼放在英文系統中執行時,處理方法是主題和內容使用 GBK 或 UTF-8 字符集。

http://jakarta.apache.org/commons/email/userguide.html 有 commons-email 的使用示例,如果直接用第一個例子放在英文環境中發送帶中文主題或內容的郵件也會出現亂碼,

這裏對第一個例子稍做改造,可以讓發出的 Email 中文不出現亂碼,如下:

package com.unmi;   

import org.apache.commons.mail.SimpleEmail;    

 publicclass MailTo   {     

      /**       * @Author Unmi       * @param args       * @throws Exception         */     

       publicstaticvoid main(String[] args) throws Exception        {          

                        //發送簡單郵件          

                        SimpleEmail email = new SimpleEmail();                       

                       email.setHostName("mail.2911.net");                     

                        //需要郵件發送服務器驗證,用戶名/密碼           

                        email.setAuthentication("broodwar", "xxxxxx");            

                       email.addTo("[email protected]", "fantasia");           

                        email.setFrom("[email protected]", "Broodwar");                     

                        //設置主題的字符集爲UTF-8            

                       email.setCharset("UTF-8");            

                       email.setSubject("測試郵件主題");                     

                        email.buildMimeMessage();                     

                        //設置內容的字符集爲UTF-8,先buildMimeMessage才能設置內容文本            

                       email.getMimeMessage().setText("測試郵件內容","UTF-8");            

                       email.sendMimeMessage();     

                         }

  }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章