Posted on 2008-01-25 00:44
thomas.chen 閱讀(2166)
評論(1) 編輯 收藏 所屬分類:
Java Enter Tech
1. 基礎
1.1. 簡介
Jarkata Common Email 是一個用來發送email的組件,其目的是用來簡化應用系統發送email的要求。他的功能:
u 發送簡單文本的email
u 支持附件的email
u 支持html格式的email
1.2. Email主要類
Email包提供了如下的幾個類:
u SimpleEmail : 用來發送基本的文本email
u MultipartEmail:該類用來發送Multipart 信息。他允許發送帶附件的文本信息
u HtmlEmil:用來發送HTML格式的email,除了有MultipartEmail的所有能力,還可以發送內嵌的圖象;
u EmailAttachment:方便發送email的時候,進行附件處理。主要提供給MultipartEmail和HtmlEmail來使用。
1.3. 主要代碼例子
1.3.1. 發送簡單的文本郵件
發送簡單的文本郵件使用SimpleEmail類即可。下面的例子是通過Gmail Server來發送郵件。
package org.apache.commons.mail.study;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class SimpleEmailStudy {
public static void main(String[] args) throws EmailException {
SimpleEmail email = new SimpleEmail();
//通過Gmail Server 發送郵件
email.setHostName("smtp.gmail.com"); //設定smtp服務器
email.setSSL(Boolean.TRUE); //設定是否使用SSL
email.setSslSmtpPort("465"); //設定SSL端口
email.setAuthentication("username", "password"); //設定smtp服務器的認證資料信息
email.addTo("reciever@gmail.com","reciever"); //設定收件人
email.setCharset("UTF-8");//設定內容的語言集
email.setFrom("from@126.com");//設定發件人
email.setSubject("Hello");//設定主題
email.setMsg("中國\n ");//設定郵件內容
email.send();//發送郵件
}
}
1.3.2. 發送帶附件的Email
發送帶附件的Email可以使用MultipartEmail,他可以同時發送多個附件。
package org.apache.commons.mail.study;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
public class MultiPartEmailStudy {
public static void main(String[] args) throws EmailException {
MultiPartEmail email = new MultiPartEmail();
// 通過Gmail Server 發送郵件
email.setHostName("smtp.gmail.com"); // 設定smtp服務器
email.setSSL(Boolean.TRUE); // 設定是否使用SSL
email.setSslSmtpPort("465"); // 設定SSL端口
email.setAuthentication("username", "password"); // 設定smtp服務器的認證資料信息
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("logo.png");
//設定合法的URL指向文件
//attachment.setURL(new URL(http://www.apache.org/images/asf_logo_wide.gif));
attachment.setDisposition(EmailAttachment.ATTACHMENT);//設定附件的方式(內嵌,附件)
attachment.setDescription("Picture");
attachment.setName("logo.png"); //附件的文件名
email.addTo("reciever@gmail.com", "reciever"); // 設定收件人
email.setCharset("UTF-8"); // 設定內容的語言集
email.setFrom("froom@126.com"); // 設定發件人
email.setSubject("common email"); // 設定主題
email.setMsg("這是我所設計的Logo,請審核"); // 設定郵件內容
email.attach(attachment);
email.send();
}
}
EmailAttachment的可以指向合法的URL資源,發送郵件的時候,該URL指向的文件會首先下載下來,然后發送出去。
1.3.3. 發送HTML格式的郵件
使用HTMLEmail來發送HTML格式的電子郵件
package org.apache.commons.mail.study;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class HtmlEmailStudy {
public static void main(String[] args) throws EmailException,MalformedURLException{
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.gmail.com"); // 設定smtp服務器
email.setSSL(Boolean.TRUE); // 設定是否使用SSL
email.setSslSmtpPort("465"); // 設定SSL端口
email.setAuthentication("username", "password"); // 設定smtp服務器的認證資料信息
email.addTo("reciever@gmail.com", "reciever"); // 設定收件人
email.setFrom("from@126.com", "From");
email.setSubject("Test email with inline image");
// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");
// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");
// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
// send the email
email.send();
}
}
1.4. 其他問題
1.4.1. 設定debug輸出
emali.setDebug(true); //設定Debug輸出信息