Commons Email 例子

一. 简单类图

见附件

二. 说明
1. Emails
定义了所有的Email的属性,是Email的基类。

2. MultiPartEmail
可以添加附件

3. SimpleEmail
简单的文本邮件

4. HtmlEmail
可以添加HTML格式的邮件信息

三. 例子
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SimpleEmailTest {

public void send() throws EmailException {
SimpleEmail email = new SimpleEmail();

email.setHostName("smtp.163.com");
email.setAuthentication("test", "******");
email.addTo("[email protected]", "test");
email.setFrom("[email protected]", "test");
email.setSubject("test");
email.setMsg("this is a test!!");
email.send();
}

public static void main(String... strings) {
SimpleEmailTest test = new SimpleEmailTest();
try {
test.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



import java.net.MalformedURLException;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class MultiEmailTest {

public void send() throws EmailException, MalformedURLException {
MultiPartEmail email = new MultiPartEmail();
email.setAuthentication("hacker_zxf", "******");
email.setHostName("smtp.163.com");
email.addTo("[email protected]", "test");
email.setFrom("[email protected]", "test");
EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("test attachment!");
attachment.setPath("/home/test/image /http_imgload.jpg");
attachment.setName("image");
attachment.setDisposition("11111111111111111111111");
// URL url = new URL("http://mimg.163.com/logo/163logo.gif");
// attachment.setURL(url);
email.attach(attachment);
email.setSubject("test");
email.setMsg("this is a test!!");
email.send();
}

public static void main(String... strings) {
MultiEmailTest test = new MultiEmailTest();
try {
test.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



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

public class HtmlEmailTest {

public void send() throws EmailException {
HtmlEmail email = new HtmlEmail();

email.setHostName("smtp.163.com");
email.setAuthentication("test", "******");
email.addTo("[email protected]", "test");
email.setFrom("[email protected]", "test");
email.setSubject("test");
email.setHtmlMsg("163");
email.send();
}

public static void main(String... strings) {
HtmlEmailTest test = new HtmlEmailTest();
try {
test.send();
} catch (EmailException e) {
// TODO Auto-generated catch blockssss
e.printStackTrace();
}
}
}


必须要导入activation.jar 和 mail.jar 两个包,否则java.lang.NoClassDefFoundError: javax/mail/Message
可以去我的CSND下: [url=http://download.csdn.net/source/880035]commons email相关包[/url]

你可能感兴趣的:(java应用,Apache,.net,HTML)