java使用HtmlEmail类发送邮件(带抄送,带附件)

Java 专栏收录该内容
86 篇文章 0 订阅

首先 需要导入jar 包

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-email</artifactId>
   <version>1.4</version>
 </dependency>

建立一个发送邮件的 Email 类 JavaBean

package com.sendemail.demo.model;

import java.io.File;
import java.io.Serializable;
import java.util.Map;

public class Email implements Serializable {

    private static final long serialVersionUID = -6390720891150157552L;
    public static final String ENCODEING = "gb2312";

    /**
     *设置服务器
     *@param host
     *@return
     */
    private String host;

    /**
     *设置发送人
     *@param sender
     *@return
     */
    private String sender;

    /**
     *设置发送人姓名
     *@param name
     *@return
     */
    private String name;

    /**
     *设置用户账号
     *@param username
     *@return
     */
    private String username;

    /**
     *设置用户账号的密码
     *@param password
     *@return
     */
    private String password;

    /**
     *设置收件人
     *@param receiver
     *@return
     */
    private String receiver;

    /**
     *设置收件人姓名
     *@param receiverName
     *@return
     */
    private String receiverName;

    /**
     *收件人
     *@param to
     *@return
     */
    private Map<String, String> to;

    /**
     *抄送
     *@param cc
     *@return
     */
    private String cc;

    /**
     *秘密抄送
     *@param bcc
     *@return
     */
    private String bcc;

    /**
     *邮件主题
     *@param subject
     *@return
     */
    private String subject;

    /**
     *邮件内容
     *@param content
     *@return
     */
    private String content;

    /**
     * 附件
     *@param file
     *@return
     */
    private File file;


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getReceiverName() {
        return receiverName;
    }

    public void setReceiverName(String receiverName) {
        this.receiverName = receiverName;
    }

    public Map<String, String> getTo() {
        return to;
    }

    public void setTo(Map<String, String> to) {
        this.to = to;
    }

    public String getCc() {
        return cc;
    }

    public void setCc(String cc) {
        this.cc = cc;
    }

    public String getBcc() {
        return bcc;
    }

    public void setBcc(String bcc) {
        this.bcc = bcc;
    }

    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 File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }
}

创建一个发送邮件的工具类 MailUtil:

/**
     * @author 犀角
     * @date 2019/10/15 13:38
     * @description 发送邮件
     */
    public boolean send(Email email)  {
        HtmlEmail htmlEmail=new HtmlEmail();
        try {


            htmlEmail.setHostName(email.getHost());

            htmlEmail.setCharset(Email.ENCODEING);

            htmlEmail.setAuthentication(email.getUsername(), email.getPassword());

            htmlEmail.addTo(email.getReceiver());

            htmlEmail.setSubject(email.getSubject());

            htmlEmail.setFrom(email.getSender(),email.getName());

            if (email.getFile() != null) {

            htmlEmail.embed(email.getFile());
            }


            htmlEmail.setMsg(email.getContent());

            htmlEmail.addCc(email.getCc());

            htmlEmail.addBcc(email.getBcc());

            htmlEmail.send();
            return true;
        } catch (EmailException  e) {
            e.printStackTrace();
            return false;
        }
    }

测试类:

public class ServiceTest {

@Test
public static void main(String[] args) {
        Email email=new Email();
        email.setHost("smtp.jushuitan.com");
        email.setSender("xijiao@jushuitan.com");
        email.setName("犀角");
        email.setUsername("xijiao@jushuitan.com");
        email.setPassword("******");
        email.setContent("您好!加油!");
        email.setSubject("邮件1");
        email.setFile(new File("D:\\emailTest.jpg"));
        email.setReceiver("15456645@qq.com");
        email.setCc("5456451@jushuitan.com");
        email.setBcc("1546465254@qq.com");
        email.setReceiverName("犀角");
        new EmailUtil().send(email);
    }
}

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值