標籤:des blog http io os ar java for sp
apache common email組件小結。這是個很好的玩意,可以支援單發簡單郵件,發附件,發HTML格式郵件,小結如下:
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
public class TestEmail {
private String emailServer = "smtp.163.com";
private String userName = "test";
private String password = "123456";
private String emailEncoding = "GBK"; //Email編碼
/**
* 測試發送不包含附件的簡單純文字郵件
* @throws EmailException
*/
public void testSimpleEmail() throws EmailException{
SimpleEmail email = new SimpleEmail();
email.setHostName(emailServer); //伺服器名
email.setAuthentication(userName, password); //使用者名稱,密碼
email.setCharset(emailEncoding); //郵件編碼方式
email.addTo("[email protected]","來函請教問題"); //收信人
email.setFrom("[email protected]","來函請教問題"); //發信人
email.setSubject("來函請教問題"); //標題
email.setMsg("來函請教問題"); //本文
email.send(); //發送
}
/**
* 測試發送包含附件的郵件
* @throws UnsupportedEncodingException
* @throws EmailException
* @throws MalformedURLException
*/
public void testMultiPartEmail() throws UnsupportedEncodingException, EmailException, MalformedURLException{
//本地附件
EmailAttachment att1 = new EmailAttachment();
att1.setPath("C:/測試郵件.pdf"); //附件原始路徑
att1.setDisposition(EmailAttachment.ATTACHMENT);
//是否亂碼無法測試,若亂碼可參考下一句解決方案
att1.setDescription("attachemnt description gril 中文"); //附件描述
//防止附件名亂碼
att1.setName(MimeUtility.encodeText("測試郵件.pdf")); //附件名稱
//網路附件
EmailAttachment att2 = new EmailAttachment();
att2.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
att2.setDisposition(EmailAttachment.ATTACHMENT);
att2.setDescription("attachemnt description logo 中文");
att2.setName(MimeUtility.encodeText("logo 中文.gif"));
MultiPartEmail email = new MultiPartEmail();
email.setHostName(emailServer);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo("[email protected]","來函請教問題"); //收信人
email.setFrom("[email protected]","來函請教問題"); //發信人
email.setSubject("來函請教問題"); //標題
email.setMsg("廣州廖煜嶸來函請教問題"); //本文
email.attach(att1); //發送
email.attach(att2);
email.send();
}
/**
* 測試發送HTML格式的郵件,經測試發現:用到的圖片在QQ的郵箱介面中沒有附件的表徵圖
* @throws UnsupportedEncodingException
* @throws EmailException
* @throws MalformedURLException
*/
public void testHtmlEmail() throws UnsupportedEncodingException, EmailException, MalformedURLException{
HtmlEmail email = new HtmlEmail();
email.setHostName(emailServer);
email.setAuthentication(userName, password);
email.setCharset(emailEncoding);
email.addTo("[email protected]","來函請教問題"); //收信人
email.setFrom("[email protected]","來函請教問題"); //發信人
email.setSubject("來函請教問題"); //標題
//本地圖片
File file = new File("C:/測試郵件.pdf");
String cid1 = email.embed(file, "測試郵件.pdf");
//網狀圖片
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid2 = email.embed(url, "logo 中文.gif");
email.setHtmlMsg("pretty gril - " + ""+ "The apache logo - "+ "");
email.setTextMsg("Your email client does not support HTML messages");
email.send();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestEmail test = new TestEmail();
try {
test.testSimpleEmail();
test.testMultiPartEmail();
test.testHtmlEmail();
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
}
apache common email組件小結