java利用commons-email发送邮件并进行封装
本例中利用commons-email发送邮件并进行封装,支持html内容和附件;Commons Email是Apache的Commons子项目下的一个邮件客户端组件,它是基于JavaMail的,大大简化了邮件的收发操作。
所需jar包:commons-email-1.4.jar和mail.jar ,jar包下载地址 http://files.cnblogs.com/files/haha12/%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E6%89%80%E9%9C%80jar%E5%8C%85.rar
代码如下:
MailInfo.java:邮件实体类
package com.test.mail; import java.util.List; import org.apache.commons.mail.EmailAttachment; /**
* 邮件相关信息
*
*/
public class MailInfo { // 收件人
private List<String> toAddress = null;
// 抄送人地址
private List<String> ccAddress = null;
// 密送人
private List<String> bccAddress = null;
// 附件信息
private List<EmailAttachment> attachments = null;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content; public List<String> getToAddress() {
return toAddress;
} public void addToAddress(String toAddress) {
this.toAddress.add(toAddress);
} public void addToAddress(List<String> toAddress) {
this.toAddress.addAll(toAddress);
} public void addCcAddress(List<String> ccAddress) {
if (null != ccAddress && ccAddress.size() > 0)
this.ccAddress.addAll(ccAddress);
} public List<EmailAttachment> getAttachments() {
return attachments;
} public void setAttachments(List<EmailAttachment> attachments) {
this.attachments = attachments;
} public List<String> getBccAddress() {
return bccAddress;
} public void setBccAddress(List<String> bccAddress) {
this.bccAddress = bccAddress;
} public String getSubject() {
return subject;
} public void setSubject(String subject) {
this.subject = subject;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public void setToAddress(List<String> toAddress) {
this.toAddress = toAddress;
} public List<String> getCcAddress() {
return ccAddress;
} public void setCcAddress(List<String> ccAddress) {
this.ccAddress = ccAddress;
} }
MailUtil.java:发送邮件工具类
package com.test.mail; import java.util.List; import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail; /**
* 发送邮件Util
*/
public class MailUtil { //邮箱
private static String mailServerHost = "smtp.163.com";
private static String mailSenderAddress = "test@163.com";
private static String mailSenderNick = "test";
private static String mailSenderUsername = "test@163.com";
private static String mailSenderPassword = "xxx"; /**
* 发送 邮件方法 (Html格式,支持附件)
*
* @return void
*/
public static void sendEmail(MailInfo mailInfo) { try {
HtmlEmail email = new HtmlEmail();
// 配置信息
email.setHostName(mailServerHost);
email.setFrom(mailSenderAddress,mailSenderNick);
email.setAuthentication(mailSenderUsername,mailSenderPassword);
email.setCharset("UTF-8");
email.setSubject(mailInfo.getSubject());
email.setHtmlMsg(mailInfo.getContent()); // 添加附件
List<EmailAttachment> attachments = mailInfo.getAttachments();
if (null != attachments && attachments.size() > 0) {
for (int i = 0; i < attachments.size(); i++) {
email.attach(attachments.get(i));
}
} // 收件人
List<String> toAddress = mailInfo.getToAddress();
if (null != toAddress && toAddress.size() > 0) {
for (int i = 0; i < toAddress.size(); i++) {
email.addTo(toAddress.get(i));
}
}
// 抄送人
List<String> ccAddress = mailInfo.getCcAddress();
if (null != ccAddress && ccAddress.size() > 0) {
for (int i = 0; i < ccAddress.size(); i++) {
email.addCc(ccAddress.get(i));
}
}
//邮件模板 密送人
List<String> bccAddress = mailInfo.getBccAddress();
if (null != bccAddress && bccAddress.size() > 0) {
for (int i = 0; i < bccAddress.size(); i++) {
email.addBcc(ccAddress.get(i));
}
}
email.send();
System.out.println("邮件发送成功!");
} catch (EmailException e) {
e.printStackTrace();
} }
}
TestMail.java:发送邮件测试类
package com.test.mail; import java.util.ArrayList;
import java.util.List; import org.apache.commons.mail.EmailAttachment; public class TestMail { /**
* @return void
*/
public static void main(String[] args) {
MailInfo mailInfo = new MailInfo();
List<String> toList = new ArrayList<String>();
toList.add("my@163.com"); List<String> ccList = new ArrayList<String>();
ccList.add("my@163.com"); List<String> bccList = new ArrayList<String>();
bccList.add("my@163.com"); //添加附件
EmailAttachment att = new EmailAttachment();
att.setPath("g:\\测试.txt");
att.setName("测试.txt");
List<EmailAttachment> atts = new ArrayList<EmailAttachment>();
atts.add(att);
mailInfo.setAttachments(atts); mailInfo.setToAddress(toList);//收件人
mailInfo.setCcAddress(ccList);//抄送人
mailInfo.setBccAddress(bccList);//密送人 mailInfo.setSubject("测试主题");
mailInfo.setContent("内容:<h1>test,测试</h1>"); MailUtil.sendEmail(mailInfo); }
}
代码执行后收到邮件的截图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUkAAACzCAIAAACLlnk0AAAOeklEQVR4nO3d729bVx3H8fsHMWlbAMk8AGkPgAEeYoKJKUgwrTOeoBrdJLohD7IxsnXzcFiniZkVgfLAbDTS4iiWG+ImlHSeimitSttqMtZ4XbrdwdobO3HiXzk8OM7x8bk/fGxfO/XXn5csNfGPm2vHb59zr69Tgzl56OQlnHDCaaRPhmPbADDq0DYATWgbgCa0DUAT2gagybXtzVuSxz057FUbYQN4XsHhI9X2AB4f+vC4UTXCbT908pKywspdWF1dXV1dLZXK9i9WVlb29vZ8fBxHl/1xAxqIt12v1ZaXl69c+XejUedf1Ou15eXlCxcupNPpreKWjw/liLI/bkCDbtvvvXWSn945/9LlczMXz/72wtIL5xdP/H1+OjP3zNIbT6dffyr9+lO3VNsrKyu1WnWjcG1z85NGo86/2N9vbBSunT9/vlwunzlzZmtr3PNG21R10XZj95+MrTOWZyzP2JWD03us/jarvLl97eXI0e849bg2NTE5653s7OTE1Jr8reFyC3kNO7adyWSq1UqtVq3XavV6rdGoNxr1/f3G/n5jaWkpnU6n0+mlpaXWDVIhI5RibD0WMFpCKX7heiwQSjGWCgVi6+qDtR4LBEKhgLjuKEHbVOm2/e75k8UPfr93I83q/2C1ZVZLseocq7zBdv/QKL2y9/Fzq/G7jj/8rY5tr01NiGrEuSLt5hc+tb28vFyt7FWrlVq1Wq9Vlbyvb25uXL3a1jbjecdigYN+edCpUCC23mybsVTIUPNuXrbeumHf1mMBI5Rqf5kxDEP86FRIee1pJy5WbmNfP7RNlW7bl8/9rl5e/fDsj7evvcB2X2uedl5tFF+pmNG3/nT3tbNHjoe+oUbd/rycnBWhS8GvTU20p+1X20tLS2+/u+F9SqfTtrvOE02FeFu2thlbj4VisZDhrK01XqjbY+x+qeOrROvqrXVJhZzrljOWruOwXLRNlW7bF8/G2P6l8ubLHyzev/3hNCvH962XGzdfqpon/vXGt4sbT5YuHn30wa+pbU9MrR1kzP9pfjM7KeJdm5poljw7aS+lba7eZdvpdHpvt1zZ261U9qoVafSut0Zvue2DHjq2bec4U2c9t93cONC5tuNY3H522w1ti0bbVOm2/faZKGtcLn/6508vHP3Pwn3F/BM189ndj55Z++NdG5nvbl+NlP7506M/uEuvbXnzenayOTtvDd9+jduLi4sdx+3FxUXpFmIi7NZ2c45sS8yxbWleHIilYgFxu1TIMEIh+VLbFN++OOeIXdKWz29/TbAtG21Tpdv2uYXntq6/+c7iQ7XrT+y8fyw/982Lr33hb7HPmxePltd/lnn1y9dXHgx//ytq225z8oOQ16YmJiYnpQHdz7YXFhbKOzu75Z320btSa47etUajvrCwYLvrHcZt/oW8JRyIxXTG7WZvrezcxm2nYVuJ+ODHu00KHLe3HReOtqnSbfvs3K8upX9RuvY825lhN080Pv5l9cPj7OPH2X9/s3/j+Zvv/fwvz3419L0v6Y3bzYAnptZmp6bW7PvRfWp7fn6+47g9Pz8v32Q9FgjEUs22RdBObbO2L90m7Eq9vDjnybKyEraR3HX23nFO3r5Rri4dbVOl23b69ac++yTNdv/Kbr7IbrzIbkT3P3tu/7MT/FTZePLq0pEH7v1it203rzY567ixbd/gltewY9tzc3M7pdLO9nZ5Z5uP3nu7u5U9afSu1ebm5uSbHFQciMVCBh+dXdpOxWLrbfu0HAfuntq2j9tem+06c3VlzxrG7bHQRdts/93KR6/uvD+9fSVSuny8eOkx68IjWxceuZkN31h74H/njvzongmttqf4XP2g2oGN26dPn+44bp8+fVq6hXjeN6e8yiy41XZIvBPevgOa/7MeCzRv6jAnb13o1bY6jW77vn3m0N24bfuZaJuqLtp++tjXnz8WePbh23595HORB257bPL2Y5O3P3r/HT+5744j90788J4777/7TrVt1/fA2q82mLZPnTpVKha3i8XW6L2zs1sWo/detVo5depU6watt6mb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H+8uS23dtt22KyLe6RuEiIRCLKH28T18Se8/Hh85HV4XDYsix+GCn/2n5l/o4Rb5I/BcXNPV4L+KUiPHmL16NGzRWTw+jq2S/ui3ymyJX3LL513LHfbdvKopQtf2Uhjn/LTf/ewUjz7TetfO4qHA57JOd4sLTjyCYOd3GkM8PUWTHlp+jfa8eb2Le0lU+MyXpomx80rjwUbjsU5VdStD1W8JtuEi83+u8Jy++6izP5HizlEx3yh7359EG8vnT7N5UMwzBNUz6e3Hv3mPxJG7Q9VvCbbuFPff3dbHKxPDAetuMSxLDJ/8ipYeN9KI595E8kEpo7xpRZkua9g1GH33STcqiJJsuykslkJpMRb3q5JWqaZjQalWtUNhY6HkibTCZ73r3f1V9KBRrQNmNS2L0973m38Xi8q//ch0kT8iG87WxZFt/2xl8pHxNou/Uh8G7LBLiVoW0AmtA2AE1oG4AmtA1AE9oGoAltA9CEtgFoQtsANKFtAJrQNgBNaBuAJrQNQBPaBqAJbQPQhLYBaELbADShbQCa0DYATWgbgCa0DUAT2gagCW0D0IS2AWhC2wA0oW0AmtA2AE1oG4AmtA1AE9oGoAltA9CEtgFoQtsANKFtAJrQNgBNaBuAJrQNQBPaBqAJbQPQhLYBaELbADShbQCa0DYATWgbgCa0DUAT2gagCW0D0IS2AWhC2wA0oW0AmtA2AE1oG4Cm/wMw98sSXpXRLgAAAABJRU5ErkJggg==" alt="" />
java利用commons-email发送邮件并进行封装的更多相关文章
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
java 利用spring JavaMailSenderImpl发送邮件,支持普通文本.附件.html.velocity模板 博客分类: Java Spring 本文主要介绍利用JavaMailS ...
- 使用Commons Email发送邮件
Commons Email是apache commons库中的一个组件,对java mail做了一些个封装,提供能为简化的API供开发者使用.它依赖于javax.mail . 首先下载commons- ...
- 使用Apache commons email发送邮件
今天研究了以下怎么用java代码发送邮件,用的是Apache的commons-email包. 据说这个包是对javamail进行了封装,简化了操作. 这里讲一下具体用法吧 一.首先你需要有邮箱账号和一 ...
- java mail实现Email的发送,完整代码
java mail实现Email的发送,完整代码 1.对应用程序配置邮件会话 首先, 导入jar <dependencies> <dependency> <groupId ...
- Apache commons email 使用过程中遇到的问题
apache-commons-email是对mail的一个封装,所以使用起来确实是很方便.特别的,官网上的tutorial也是极其的简单.但是我也仍然是遇到了没有解决的问题. jar包的添加 mail ...
- Commons Email使用
Apache Commons Email Apache的一个开源项目,是基于另一个开源项目Java Mail上进行封装的,使用起来更加简单方便: http://commons.apache.org/p ...
- Java 学习(18):Java 序列化& 网络编程& 发送邮件
--Java 序列化 -- 网络编程 -- 发送邮件 Java 序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信 ...
- C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
net2.0后,C#可以利用System.Net发送邮件了. 代码整理如下: 3. 增加IProcessMessage类,定义了一个消息方法,用于消息传递 /********************* ...
- java利用反射获取类的属性及类型
java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...
随机推荐
- django之JavaScript的简单学习2
前言:ajax预备知识:json进阶 1.JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javascript对象: 请大家记住一 ...
- MVC引用CSS文件の正确姿势
你的css文件目录结构: 将路径写入BundleConfig规则中: using System.Web; using System.Web.Optimization; namespace XXXX { ...
- memcached-win32-1.4.4-14 help doc
memcached-win32-1.4.4-14 cmd打开命令窗口,转到解压的目录,输入 “memcached.exe -d install”. 使用telnet命令 验证缓存服务器是否可用.tel ...
- 【C#】动态加载dll程序集
原文:http://www.cnblogs.com/bomo/archive/2013/03/01/2938165.html 很多时候我们需要用到引用其他程序集,有些程序集是.Net生成的,可以支持反 ...
- Network Wars
zoj2676:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 题意:给出一个带权无向图 ,每条边e有一个权 .求将点 ...
- 快速搭建Web服务器软件PHP+Apache+MySQL
搭建网站或者博客,需要一个合适的 Web 服务器.除了如下能在购买的虚拟空间上进行操作外,我们也可以在自己的电脑上搞定,因为可以用来方便快捷地测试网站或者博客主题,无论是 Wordpress.Joom ...
- HTML5的Server-Sent Events功能的使用
客户端代码示例 //创建一个新的 EventSource 对象,然后规定发送更新的页面的 URL. var source = new EventSource("http://localhos ...
- 转:三十、Java图形化界面设计——布局管理器之BorderLayout(边界布局)
http://blog.csdn.net/liujun13579/article/details/7772215 边界布局管理器把容器的的布局分为五个位置:CENTER.EAST.WEST.NORTH ...
- 【HDOJ】2707 Steganography
简单字符串,读懂题,很容易AC. #include <stdio.h> #include <string.h> ], line[], des[]; int main() { i ...
- Exception Handling Statements (C# Reference)
Exception Handling Statements (C# Reference) C# provides built-in support for handling anomalous sit ...