java利用commons-email发送邮件并进行封装

Posted 敲代码的卡卡罗特

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java利用commons-email发送邮件并进行封装相关的知识,希望对你有一定的参考价值。

本例中利用commons-email发送邮件并进行封装,支持html内容和附件;Commons Email是Apache的Commons子项目下的一个邮件客户端组件,它是基于JavaMail的,大大简化了邮件的收发操作。

 

该工具类支持多个发送人,多个抄送人,多个密送人,多个附件等功能。非常强大。

  pom.xml

 

技术分享图片
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.10</version>
</dependency>
<!-- email -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-email</artifactId>
    <version>1.4</version>
</dependency>
View Code

 

 

 

MailInfo.java
技术分享图片
package com.example.demo.email;

import lombok.Data;
import org.apache.commons.mail.EmailAttachment;

import java.util.List;

/**
 * Created by 敲代码的卡卡罗特 on 2018/3/29.
 */
@Data
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 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);
    }

}
View Code

MailUtil.java
技术分享图片
package com.example.demo.email;

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

import java.util.List;

/**
 * Created by 敲代码的卡卡罗特 on 2018/3/29.
 */
public class MailUtil {
    //邮箱
    private static String mailServerHost = "smtp.163.com";
    private static String mailSenderAddress = "[email protected]";
    private static String mailSenderNick = "敲代码的卡卡罗特";
    private static String mailSenderUsername = "[email protected]";
    private static String mailSenderPassword = "xxxxx";

    /**
     * 发送 邮件方法 (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();
        }

    }
}
View Code

 

Test.java
技术分享图片
package com.example.demo.email;

import org.apache.commons.mail.EmailAttachment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 敲代码的卡卡罗特 on 2018/3/29.
 */
public class Test {
    /**
     * @return void
     */
    public static void main(String[] args) {
        MailInfo mailInfo = new MailInfo();
        List<String> toList = new ArrayList<String>();
        toList.add("[email protected]");

        List<String> ccList = new ArrayList<String>();
        ccList.add("[email protected]");

        List<String> bccList = new ArrayList<String>();
        bccList.add("[email protected]");

        //添加附件
        EmailAttachment att = new EmailAttachment();
        att.setPath("E:\\liuzhonghua\\备份\\xx.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);

    }
}
View Code

 

ok,亲测可用!!!!!!
 

 

以上是关于java利用commons-email发送邮件并进行封装的主要内容,如果未能解决你的问题,请参考以下文章

一个简单易用的邮件工具类库:Commons-Email

一个简单易用的邮件工具类库:Commons-Email

java email发送(附件中文的处理)

发送邮件

通过HtmlEmail 发送邮件

发送邮件工具类,非常实用