apache common email組件
需要mail.jar, commons-email-1.2.jar
package utils; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.MultiPartEmail; public class EmailUtil { public static void sendmail(String to, String subject, String msg) throws Exception { HtmlEmail email = new HtmlEmail(); email.setHostName(PropertiesUtil.readStringValue("email_host")); email.setCharset("utf-8"); email.addTo(to); email.setFrom(PropertiesUtil.readStringValue("email_from")); email.setAuthentication(PropertiesUtil.readStringValue("email_from"), PropertiesUtil.readStringValue("email_password")); email.setSubject(subject); email.setHtmlMsg(msg); email.send(); } public static void sendmailWithAttachment(String to, String subject, String msg, EmailAttachment[] attachs) throws EmailException{ HtmlEmail email = new HtmlEmail(); email.setHostName(PropertiesUtil.readStringValue("email_host")); email.setCharset("utf-8"); email.addTo(to); email.setFrom(PropertiesUtil.readStringValue("email_from")); email.setAuthentication(PropertiesUtil.readStringValue("email_from"), PropertiesUtil.readStringValue("email_password")); email.setSubject(subject); email.setHtmlMsg(msg); for(int i=0;i<attachs.length;i++){ email.attach(attachs[i]); } email.send(); } public static void sendMultiPartMail(String to, String subject, String msg, String fileName) { // 創建附件郵件對象 MultiPartEmail multipartMail = new MultiPartEmail(); multipartMail.setCharset("utf-8"); multipartMail.setHostName(PropertiesUtil.readStringValue("email_host")); // 創建一個附件(附件可以是一個本地路徑也可以是一個網絡url) EmailAttachment emailAttachment = new EmailAttachment(); emailAttachment.setPath(fileName); emailAttachment.setDescription(EmailAttachment.ATTACHMENT); emailAttachment.setDescription("This is a attach test!"); try { System.out.println("email_from=======" + PropertiesUtil.readStringValue("email_from")); multipartMail.setFrom(PropertiesUtil.readStringValue("email_from")); multipartMail.setAuthentication(PropertiesUtil .readStringValue("email_from"), PropertiesUtil .readStringValue("email_password")); multipartMail.addTo(to); // multipartMail.addCc(""); multipartMail.setSubject(subject); multipartMail.setMsg(msg); // 添加附件 // multipartMail.attach(emailAttachment); // multipartMail.attach(emailAttachment); // multipartMail.attach(emailAttachment); // multipartMail.attach(emailAttachment); // 發送 multipartMail.send(); } catch (Exception e) { // TODO Auto-generated catch e.printStackTrace(); } } public static void main(String[] args) throws Exception { System.out.println("22"); EmailUtil.sendmail("xm.163.happy@163.com", "1222","1oooooooooooooooooooo"); //EmailUtil.sendMultiPartMail("xm.163.happy@163.com", "nihaodjfajs", "111sdfgsdfgsdfgsd","D:/text.pdf"); EmailAttachment[] emailAttachments=new EmailAttachment[2]; EmailAttachment emailAttachment = new EmailAttachment(); emailAttachment.setPath("D:/text1.pdf"); emailAttachments[0]=emailAttachment; EmailAttachment agreement = new EmailAttachment(); agreement.setPath("D:/text.pdf"); /* agreement.setDisposition(EmailAttachment.ATTACHMENT); agreement.setDescription("訂房業務合作協議(上海多途國旅)"); agreement.setName(MimeUtility.encodeText("訂房業務合作協議(上海多途國旅).doc"));*/ emailAttachments[1]=agreement; EmailUtil.sendmailWithAttachment("xm.163.happy@163.com", "newnewnihaodjfajs", "111sdfgsdfgsdfgsd",emailAttachments); System.out.println("success"); } }
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 {
/**
* @param args
*/
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("abc@tom.com","來函請教問題"); //收信人
email.setFrom("abc@163.com","來函請教問題"); //發信人
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("jackyrong@tom.com","來函請教問題"); //收信人
email.setFrom("jackyrong@163.com","來函請教問題"); //發信人
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("jackyrong@tom.com","來函請教問題"); //收信人
email.setFrom("jackyrong@163.com","來函請教問題"); //發信人
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();
}
}