package com.mail;
import java.net.MalformedURLException;
import java.net.URL;
import javax.mail.MessagingException;
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;
/**
* author:pangxinhua date: 2010-10-29
*/
public class TestMail {
public static void main(String[] args) throws EmailException,
MessagingException, MalformedURLException {
multipartEmail();
}
public static void multipartEmail() throws EmailException, MalformedURLException {
/*設置附件start---------------*/
EmailAttachment attachment = new EmailAttachment();
//附件路徑
attachment.setPath("C://Documents and Settings//pangxinhua//桌面//monitor.sh");
//設置爲附件
attachment.setDisposition(EmailAttachment.ATTACHMENT);
//描述
//attachment.setDescription("Picture of John");
//設置文件名(默認原名)
attachment.setName("monitor.sh");
/*設置附件結束*/
// Create the email message
MultiPartEmail email = new MultiPartEmail();
//服務器
email.setHostName("smtpcom.263xmail.com");
//驗證
email.setAuthentication("[email protected]", "into123456");
//發件人
email.setFrom("[email protected]", "pangxinhua");
//收件人
email.addTo("[email protected]", "pangxinhua");
//編碼
email.setCharset("UTF-8");
//主題
email.setSubject("The picture");
//正文
email.setMsg("Here is the picture you wanted");
//添加附件
email.attach(attachment);
//發送
email.send();
}
public static void htmlEmail() throws EmailException, MalformedURLException {
HtmlEmail m = new HtmlEmail();
m.setAuthentication("[email protected]", "into123456");
m.setHostName("smtpcom.263xmail.com");
m.setFrom("[email protected]", "pangxinhua");
m.addTo("[email protected]", "pangxinhua");
// m.setMsg("java mail test");
// m.setSubject("subject");
// //設置主題的字符集爲UTF-8
m.setCharset("UTF-8");
m.setSubject("測試郵件主題");
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = m.embed(url, "Apache logo");
// set the html message
m.setHtmlMsg("<html>The apache logo - <img src=/"cid:" + cid
+ "/"></html>");
// set the alternative message
m.setTextMsg("Your email client does not support HTML messages");
// send the email
m.send();
}
public static void simpleEmail() throws EmailException {
SimpleEmail m = new SimpleEmail();
m.setAuthentication("[email protected]", "into123456");
m.setHostName("smtpcom.263xmail.com");
m.setFrom("[email protected]", "pangxinhua");
m.addTo("[email protected]", "pangxinhua");
m.setCharset("UTF-8");
m.setSubject("測試郵件主題");
m.setMsg("test mail");
// m.buildMimeMessage();
//設置內容的字符集爲UTF-8,先buildMimeMessage才能設置內容文本
// m.getMimeMessage().setText("測試郵件內容","UTF-8");
// m.sendMimeMessage();
m.send();
System.out.println("Send email successful!");
}
}