邮件工具-MailUtil(发送邮件)

邮件工具-MailUtil

概述

在Java中发送邮件主要品依靠javax.mail包,但是由于使用比较繁琐,因此Hutool针对其做了封装。由于依赖第三方包,因此将此工具类归类到extra模块中。

使用

引入依赖

Hutool对所有第三方都是可选依赖,因此在使用MailUtil时需要自行引入第三方依赖。


    javax.mail
    mail
    1.4.7

邮件服务器配置

在classpath(在标准Maven项目中为src/main/resources)的config目录下新建mail.setting文件,最小配置内容如下,在此配置下,smtp服务器和用户名都将通过from参数识别:

# 发件人(必须正确,否则发送失败)
from = [email protected]
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
pass = q1w2e3

有时候一些非标准邮箱服务器(例如企业邮箱服务器)的smtp地址等信息并不与发件人后缀一致,端口也可能不同,此时Hutool可以提供完整的配置文件:

完整配置

# 邮件服务器的SMTP地址,可选,默认为smtp.<发件人邮箱后缀>
host = smtp.yeah.net
# 邮件服务器的SMTP端口,可选,默认25
port = 25
# 发件人(必须正确,否则发送失败)
from = [email protected]
# 用户名,默认为发件人邮箱前缀
user = hutool
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
pass = q1w2e3

注意 邮件服务器必须支持并打开SMTP协议,详细请查看相关帮助说明 配置文件的样例中提供的是我专门为测试邮件功能注册的yeah.net邮箱,帐号密码公开,供Hutool用户测试使用。

发送邮件

  1. 发送普通文本邮件,最后一个参数可选是否添加多个附件:
MailUtil.send("[email protected]", "测试", "邮件来自Hutool测试", false);
  1. 发送HTML格式的邮件并附带附件,最后一个参数可选是否添加多个附件:
MailUtil.send("[email protected]", "测试", "

邮件来自Hutool测试

", true, FileUtil.file("d:/aaa.xml"));
  1. 群发邮件,可选HTML或普通文本,可选多个附件:
ArrayList tos = CollUtil.newArrayList(
    "[email protected]", 
    "[email protected]", 
    "[email protected]", 
    "[email protected]");

MailUtil.send(tos, "测试", "邮件来自Hutool群发测试", false);

发送邮件非常简单,只需一个方法即可搞定其中按照参数顺序说明如下:

  1. tos: 对方的邮箱地址,可以是单个,也可以是多个(Collection表示)
  2. subject:标题
  3. content:邮件正文,可以是文本,也可以是HTML内容
  4. isHtml: 是否为HTML,如果是,那参数3识别为HTML内容
  5. files: 可选:附件,可以为多个或没有,将File对象加在最后一个可变参数中即可

其它

  1. 自定义邮件服务器

除了使用配置文件定义全局账号以外,MailUtil.send方法同时提供重载方法可以传入一个MailAccount对象,这个对象为一个普通Bean,记录了邮件服务器信息。

MailAccount account = new MailAccount();
account.setHost("smtp.yeah.net");
account.setPort("25");
account.setAuth(true);
account.setFrom("[email protected]");
account.setUser("hutool");
account.setPass("q1w2e3");

MailUtil.send(account, CollUtil.newArrayList("[email protected]"), "测试", "邮件来自Hutool测试", false);
  1. 使用SSL加密方式发送邮件 在使用QQ或Gmail邮箱时,需要强制开启SSL支持,此时我们只需修改配置文件即可:
# 发件人(必须正确,否则发送失败),“小磊”可以任意变更,<>内的地址必须唯一,以下方式也对
# from = [email protected]
from = 小磊
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
pass = q1w2e3
# 使用SSL安全连接
sslEnable = true

在原先极简配置下只需加入sslEnable即可完成SSL连接,当然,这是最简单的配置,很多参数根据已有参数已设置为默认。

完整的配置文件如下:

# 邮件服务器的SMTP地址
host = smtp.yeah.net
# 邮件服务器的SMTP端口
port = 465
# 发件人(必须正确,否则发送失败)
from = [email protected]
# 用户名(注意:如果使用foxmail邮箱,此处user为qq号)
user = hutool
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
pass = q1w2e3
#使用 STARTTLS安全连接,STARTTLS是对纯文本通信协议的扩展。
startttlsEnable = true

# 使用SSL安全连接
sslEnable = true
# 指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字
socketFactoryClass = javax.net.ssl.SSLSocketFactory
# 如果设置为true,未能创建一个套接字使用指定的套接字工厂类将导致使用java.net.Socket创建的套接字类, 默认值为true
socketFactoryFallback = true
# 指定的端口连接到在使用指定的套接字工厂。如果没有设置,将使用默认端口456
socketFactoryPort = 465

# SMTP超时时长,单位毫秒,缺省值不超时
timeout = 0
# Socket连接超时值,单位毫秒,缺省值不超时
connectionTimeout = 0

3、针对QQ邮箱和Foxmail邮箱的说明

(1) QQ邮箱中SMTP密码是单独生成的授权码,而非你的QQ密码,至于怎么生成,见腾讯的帮助说明:http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

使用帮助引导生成授权码后,配置如下即可:

pass = 你生成的授权码

(2) Foxmail邮箱本质上也是QQ邮箱的一种别名,你可以在你的QQ邮箱中设置一个foxmail邮箱,不过配置上有所区别。在Hutool中user属性默认提取你邮箱@前面的部分,但是foxmail邮箱是无效的,需要单独配置为与之绑定的qq号码或者[email protected]XXXX。即:

host = smtp.qq.com
from = [email protected]
user = foxmail邮箱对应的QQ号码或者qq邮箱@前面部分
...

代码:

1.配置文件: 


spring:
  mail:
   #163邮箱服务器配置
   host: smtp.163.com
   username: [email protected]
   password: XXXXX
   #protocol: smtp
   properties.mail.smtp.auth: true
   #465或者994
   properties.mail.smtp.port: 465
   #ssl 有关配置,如果不用ssl配置,直接启用25端口即可,不用配置socketFactory
   properties.mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
   properties.mail.smtp.socketFactory.fallback: false
   properties.mail.smtp.socketFactory.port: 465
   default-encoding: utf-8
   from: [email protected]

 

2.JavaMailSender接口(发送邮件)

import javax.mail.MessagingException;

/**
 * Title: MailService
 * Description: JavaMailSender接口发送邮件
 * 
 */
public interface MailService {

    /**
     * 发送文本邮件
     * @param to
     * @param subject
     * @param content
     */
    void sendSimpleMail(String to, String subject, String content);

    void sendSimpleMail(String to, String subject, String content, String... cc);

    /**
     * 发送HTML邮件
     * @param to
     * @param subject
     * @param content
     * @throws MessagingException
     */
    void sendHtmlMail(String to, String subject, String content) throws Exception;

    void sendHtmlMail(String to, String subject, String content, String... cc)  throws MessagingException;

    /**
     * 发送带附件的邮件
     * @param to
     * @param subject
     * @param content
     * @param filePath
     * @throws MessagingException
     */
    void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException;

    void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)  throws MessagingException;

    /**
     * 发送正文中有静态资源的邮件
     * @param to
     * @param subject
     * @param content
     * @param rscPath
     * @param rscId
     * @throws MessagingException
     */
     void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException;

     void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException ;

}

2.JavaMailSender接口实现类

import com.ciip.cloud.core.usercenter.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;

/**
 * Title: MailServiceImpl
 * Description: JavaMailSender发送邮件
 */
@Service
public class MailServiceImpl implements MailService {

    //邮件发送者email
    @Value("${spring.mail.from}")
    private String from;

    //邮件系统发送方
    @Value("${ciip.email.systemSenderName}")
    private String systemSenderName;

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendSimpleMail(String to, String subject, String content, String... cc) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setCc(cc);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        //邮件发送人=网站名称+<邮箱>
        helper.setFrom(MimeUtility.encodeWord(systemSenderName) +"<"+from+">");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
    }

    @Override
    public void sendHtmlMail(String to, String subject, String content, String... cc)  throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        helper.setCc(cc);
        mailSender.send(message);
    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        mailSender.send(message);
    }

    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)  throws MessagingException{
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        helper.setCc(cc);

        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        mailSender.send(message);
    }

    @Override
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
    }

    @Override
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);

        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
    }
}

4.controller层

    /**
     * 获取邮箱验证码
     *             
     *
     * @param email     邮箱
     * @param type      类型(绑定,更改)
     * @return
     */
    @ApiOperation(value="获取邮箱验证码", notes = "")
    @PostMapping(value = CIIPCommonConstant.ApiAuth.ANON + "/emailVerificationCode")
    public ResponseMessage emailVerificationCode(@RequestParam(value = "email") String email,
                                                 @RequestParam(value = "type") String type) throws Exception{
        return ciipUserService.emailVerificationCode(email,type);
    }

5.service层:

    /**
     * 获取邮箱验证码
     *             
     *
     * @param email     邮箱
     * @param type      类型(绑定,更改)
     * @return
     */
    ResponseMessage emailVerificationCode(String email,String type) throws Exception;

6.service实现层:


    @Value("${ciip.email.bindingEmailSubject}")
    private String bindingEmailSubject;

    @Value("${ciip.email.updateEmailSubject}")
    private String updateEmailSubject;   


    /**
     * 获取邮箱验证码          
     *
     * @param email     邮箱
     * @param type      类型(绑定,更改)
     * @return
     */
    @Override
    public ResponseMessage emailVerificationCode(String email,String type) throws  Exception {

        //根据邮箱获取用户信息
        CiipUser ciipUser = ciipUserRepository.findCiipUserByEmail(email);
        if(null == ciipUser){
            return ResultUtil.error(UserCenterStatusCode.MESSAGE_NOT_EXIST.getCode(),"用户"+UserCenterStatusCode.MESSAGE_NOT_EXIST.getMsg());
        }

        //失效时间,5分钟过期
        int time=300;

        //邮箱模板
        StringBuilder templateContent = new StringBuilder();
        templateContent.append("

{email},您好!"+"

"); templateContent.append("

感谢您对CIIP平台的支持。

"); templateContent.append("

您的邮箱验证码:

"); templateContent.append("

{code}

"); templateContent.append("

(该验证码5分钟后需要重新获取。该邮箱由CIIP账号安全服务系统自动发送,请勿直接回复)

"); //模板字符窜 String templateContentStr = templateContent.toString(); //填充模板参数 Map paramsMap = new HashMap(); //对方用户名称 paramsMap.put("email",ciipUser.getEmail()); //此处是生成6位数验证码工具类 String validateCode = CoreUtils.randomString(6, true); paramsMap.put("code",validateCode); //正文内容 String cotnent = StrUtil.format(templateContentStr,paramsMap); //绑定邮箱 if(type.equals(CIIPCommonConstant.MobileValidateCodeSouceType.BINDING)){ //发送Html形式的邮件 mailService.sendHtmlMail(ciipUser.getEmail(),bindingEmailSubject,cotnent); redisUtil.setWithExpireTime(CIIPCommonConstant.CIIP_PROTAL,CIIPCommonConstant.VALIDATE_CODE_KEY_EMAIL_BINDING+ciipUser.getEmail(), validateCode,time); } //变更邮箱 if(type.equals(CIIPCommonConstant.MobileValidateCodeSouceType.UPDATE)){ mailService.sendHtmlMail(ciipUser.getEmail(),updateEmailSubject,cotnent); redisUtil.setWithExpireTime(CIIPCommonConstant.CIIP_PROTAL,CIIPCommonConstant.VALIDATE_CODE_KEY_EMAIL_UPDATE+ciipUser.getEmail(), validateCode,time); } return ResultUtil.success(UserCenterStatusCode.SEND_EMAIL_SUCCESS.getCode(),UserCenterStatusCode.SEND_EMAIL_SUCCESS.getMsg()); }

 

你可能感兴趣的:(随笔)