[Java教程]java利用commons
0 2015-08-14 17:00:27 本例中利用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 toAddress = null; // 抄送人地址 private List ccAddress = null; // 密送人 private List bccAddress = null; // 附件信息 private List attachments = null; // 邮件主题 private String subject; // 邮件的文本内容 private String content; public List getToAddress() { return toAddress; } public void addToAddress(String toAddress) { this.toAddress.add(toAddress); } public void addToAddress(List toAddress) { this.toAddress.addAll(toAddress); } public void addCcAddress(List ccAddress) { if (null != ccAddress && ccAddress.size() > 0) this.ccAddress.addAll(ccAddress); } public List getAttachments() { return attachments; } public void setAttachments(List attachments) { this.attachments = attachments; } public List getBccAddress() { return bccAddress; } public void setBccAddress(List 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 toAddress) { this.toAddress = toAddress; } public List getCcAddress() { return ccAddress; } public void setCcAddress(List 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 attachments = mailInfo.getAttachments(); if (null != attachments && attachments.size() > 0) { for (int i = 0; i < attachments.size(); i++) { email.attach(attachments.get(i)); } } // 收件人 List toAddress = mailInfo.getToAddress(); if (null != toAddress && toAddress.size() > 0) { for (int i = 0; i < toAddress.size(); i++) { email.addTo(toAddress.get(i)); } } // 抄送人 List ccAddress = mailInfo.getCcAddress(); if (null != ccAddress && ccAddress.size() > 0) { for (int i = 0; i < ccAddress.size(); i++) { email.addCc(ccAddress.get(i)); } } //邮件模板 密送人 List 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 toList = new ArrayList(); toList.add("my@163.com"); List ccList = new ArrayList(); ccList.add("my@163.com"); List bccList = new ArrayList(); bccList.add("my@163.com"); //添加附件 EmailAttachment att = new EmailAttachment(); att.setPath("g:\\测试.txt"); att.setName("测试.txt"); List atts = new ArrayList(); atts.add(att); mailInfo.setAttachments(atts); mailInfo.setToAddress(toList);//收件人 mailInfo.setCcAddress(ccList);//抄送人 mailInfo.setBccAddress(bccList);//密送人 mailInfo.setSubject("测试主题"); mailInfo.setContent("内容:
test,测试
"); MailUtil.sendEmail(mailInfo); }}代码执行后收到邮件的截图:
本文网址:http://www.shaoqun.com/a/134086.html
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。
JAVA
0