如何使用org.apache.commons.mail發送郵件

1.前言
使用Apache-commons-email發送郵件需要這三個jar包:commons-email-1.0.jarmail.jaractivition.jar
後兩個包在Sun上下載jaf-1_1-fr.zipjavamail-1_4.zip。其中有所需要的jar包。
 
2.發送簡單郵件
下面這段代碼是發送一個簡單郵件的流程代碼,你可以將其封裝成一個方法,供調用。
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");//指定SMTP server
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();//發送
注意,文件內容爲中文,會出現亂碼,解決方法如下:
email.setMsg("This is a simple test of commons-email");
替換爲
email.setContent("This is a simple test of commons-email", "text/plain;charset=GBK");
 
或者如下方法:

工程目錄結構如下:

以下給出兩個簡單示例程序

// SendMail.java 使用SimpleEmail發郵件

package com.apache.commons.email.demo;

import org.apache.commons.mail.SimpleEmail;

public class SendMail
{
    public static void main ( String[] arg ) throws Exception
    {
    // 使用SimpleEmail對於中文內容,可能會產生亂碼
        SimpleEmail email = new SimpleEmail ( );

        // SMTP服務器名
        email.setHostName ( "smtp.163.com" );
        // 登陸郵件服務器的用戶名和密碼
        email.setAuthentication ( "peki", "123456" );
        // 接收人
        email.addTo ( "", "曦勤" );
        // 發送人
        email.setFrom ( "", "小陳" );
        // 標題
        email.setSubject ( "Test message" );
        // 郵件內容
        email.setMsg ( "This is a simple test of commons-email<br>我是小陳" );
        // 發送
        email.send ( );
       
        System.out.println ( "Send email successful!" );

    }
}

收信結果如下:

This is a simple test of commons-email<br>????

有亂碼產生,並且html內容沒有正常顯示

// MailSender.java   使用HtmlEmail發郵件

package com.apache.commons.email.demo;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class MailSender {


public static void main(String[] args) {
   // 不要使用SimpleEmail,會出現亂碼問題
   HtmlEmail email = new HtmlEmail();
   try {
    // 這裏是SMTP發送服務器的名字:,163的如下:
    email.setHostName("smtp.163.com");
    // 字符編碼集的設置
    email.setCharset("gbk");
    // 收件人的郵箱
    email.addTo("");
    // 發送人的郵箱
    email.setFrom("", "小陳");
    // 如果需要認證信息的話,設置認證:用戶名-密碼。分別爲發件人在郵件服務器上的註冊名稱和密碼
    email.setAuthentication("peki", "123456");
    email.setSubject("下午3:00會議室討論,請準時參加");
    // 要發送的信息,由於使用了HtmlEmail,可以在郵件內容中使用HTML標籤
    email.setMsg("下午3:00會議室討論,請準時參加<BR>呵呵~!");
    // 發送
    email.send();
   
    System.out.println ( "郵件發送成功!" );
   } catch (EmailException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println ( "郵件發送失敗!" );
   }

}

}

收信結果如下:

下午3:00會議室討論,請準時參加
3.發送帶附件的郵件
下面這段代碼是發送一個帶附件郵件的流程代碼,你可以將其封裝成一個方法,供調用。
// Create the attachment
 EmailAttachment attachment = new EmailAttachment();
 attachment.setPath("mypictures/john.jpg");//指定附件在本地的路徑
 attachment.setDisposition(EmailAttachment.ATTACHMENT);
 attachment.setDescription("Picture of John");//附件描述
 attachment.setName("John");//附件名稱
 
 // Create the email message
 MultiPartEmail email = new MultiPartEmail();
 email.setHostName("mail.myserver.com");
 email.addTo("[email protected]", "John Doe");
 email.setFrom("[email protected]", "Me");
 email.setSubject("The picture");
 email.setMsg("Here is the picture you wanted");
 
 // add the attachment
 email.attach(attachment);
 
 // send the email
 email.send();
 
同樣,你可以指定一個在網絡上的附件,只要給出該附件的地址,將上文綠色代碼部分替換爲下面這句。
attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
注意,如果你的附件的名稱是中文時,同樣會出現亂碼,解決的方法如下。
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");//附件描述
attachment.setName("需傳送的附件");//附件名稱
attachment.setName(MimeUtility.encodeText("需傳送的附件.txt"));
 
4.示例代碼
附件中給出了一個SendMail.java,將上述方法封裝成一個class
其中,構造子初始化所有最基本的變量,即發送簡單郵件時所使用的變量,看變量名稱即可“望文生義”。
public SendMail(String hostSMTP, String toMailAddress, String toMailUser,
                    String fromMailAddress, String fromMailUser, String mailSubject,
                    String mailMessage) {
方法send()可發送一個簡單郵件。
方法sendAttchment (String path,String des,String name)可發送一個帶附件的郵件。其中,path指定附件在本地的物理路徑,des指定附件的描述,name指定附件名。
方法sendAttachmentWithUrl (URL url,String des,String name)可發送一個指定附件URL的郵件。其中,url指定附件的URL,其餘參數和sendAttachment相同含義。
發送附件的這兩個方法沒有考慮處理中文文件名的問題,大家可自己添加。
 

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