国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java利用commons-email發送郵件并進行封裝

2019-11-15 01:08:24
字體:
來源:轉載
供稿:網友
java利用commons-email發送郵件并進行封裝

本例中利用commons-email發送郵件并進行封裝,支持html內容和附件;Commons Email是Apache的Commons子項目下的一個郵件客戶端組件,它是基于JavaMail的,大大簡化了郵件的收發操作。

所需jar包:commons-email-1.4.jar和mail.jar ,jar包下載地址 http://files.VEVb.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);    }}

代碼執行后收到郵件的截圖:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 敦化市| 买车| 五家渠市| 宜川县| 佛山市| 天长市| 曲麻莱县| 当涂县| 天柱县| 三河市| 晋城| 大同县| 简阳市| 双城市| 阿拉善盟| 浑源县| 陵水| 读书| 东源县| 静宁县| 读书| 宜良县| 三门峡市| 仙游县| 镇安县| 邹平县| 兴化市| 威信县| 仪陇县| 赞皇县| 宜章县| 昌邑市| 阳江市| 封丘县| 饶阳县| 库车县| 汉川市| 平顺县| 大渡口区| 沛县| 广汉市|