一. 简单类图
见附件
二. 说明
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("test@163.com", "test");
email.setFrom("test@163.com", "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("test@163.com", "test");
email.setFrom("test@163.com", "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("test@163.com", "test");
email.setFrom("test@163.com", "test");
email.setSubject("test");
email.setHtmlMsg("<html><body><a href=www.163.com>163</a></body></html>");
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下:
commons email相关包
- 大小: 12.1 KB
分享到:
相关推荐
用 apache commons email 发送带附件,HTML 格式的 邮件 格式例子 还包括commons-email的jar包,及其他相关资料
使用apche commons-email.jar写的一个demo,与大家交流
一个简单的struts2+spring+hibernate+commons-email整合的例子
java发邮件,可以带附件.使用了commons-email-1.0.jar包.
不错的发邮件jar包,下载下来可以直接放到项目中用,分享给大家.
如果发现系统有BUG,请给我发Email: 演示地址 Easyui 中文社区 更多资源 v20131021 在sy.test包下,添加了POI读取excel和写入excel的小例子 在sy.test包下,添加了apache的commons包下的lang3的一些常用工具示例 在...
增加了页面分页和后台分页方法 (如果看了我第一个例子的朋友.应该看到我在service层的find方法上的注释.说当前版本因为原来使用find方法是从代理中取.而spring默认的数据持久话只能包含业务层和数据层.不能包含...
《Java开发实战1200例》分为I、II两卷共计1200个例子,包括了开发中各个方面最常用的实例,是目前市场上实例最全面的开发类图书;书中实例来源于多位工程师的多年积累,具有很强的实用性。 本书是第II卷,以开发...