邮件工具-MailUtil

概述

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

使用

引入依赖

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

<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version>
</dependency>

邮件服务器配置

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

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

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

完整配置

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

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

发送邮件

  1. 发送普通文本邮件,最后一个参数可选是否添加多个附件:
MailUtil.send("hutool@foxmail.com", "测试", "邮件来自Hutool测试", false);
  1. 发送HTML格式的邮件并附带附件,最后一个参数可选是否添加多个附件:
MailUtil.send("hutool@foxmail.com", "测试", "<h1>邮件来自Hutool测试</h1>", true, FileUtil.file("d:/aaa.xml"));
  1. 群发邮件,可选HTML或普通文本,可选多个附件:
ArrayList<String> tos = CollUtil.newArrayList("person1@bbb.com", "person2@bbb.com", "person3@bbb.com", "person4@bbb.com");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("hutool@yeah.net");
account.setUser("hutool");
account.setPass("q1w2e3");MailUtil.send(account, CollUtil.newArrayList("hutool@foxmail.com"), "测试", "邮件来自Hutool测试", false);
  1. 使用SSL加密方式发送邮件 在使用QQ或Gmail邮箱时,需要强制开启SSL支持,此时我们只需修改配置文件即可:
# 发件人(必须正确,否则发送失败),“小磊”可以任意变更,<>内的地址必须唯一,以下方式也对
# from = hutool@yeah.net
from = 小磊<hutool@yeah.net>
# 密码(注意,某些邮箱需要为SMTP服务单独设置密码,详情查看相关帮助)
pass = q1w2e3
# 使用SSL安全连接
sslEnable = true

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

完整的配置文件如下:

# 邮件服务器的SMTP地址
host = smtp.yeah.net
# 邮件服务器的SMTP端口
port = 465
# 发件人(必须正确,否则发送失败)
from = hutool@yeah.net
# 用户名(注意:如果使用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号码或者XXXX@qq.comXXXX。即:

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

代码:

1.配置文件:


spring:mail:#163邮箱服务器配置host: smtp.163.comusername: XXXXX@163.compassword: XXXXX#protocol: smtpproperties.mail.smtp.auth: true#465或者994properties.mail.smtp.port: 465#ssl 有关配置,如果不用ssl配置,直接启用25端口即可,不用配置socketFactoryproperties.mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactoryproperties.mail.smtp.socketFactory.fallback: falseproperties.mail.smtp.socketFactory.port: 465default-encoding: utf-8from: xxxx@163.com

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;@Autowiredprivate JavaMailSender mailSender;@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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);}@Overridepublic 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*/@Overridepublic 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("<p>{email},您好!"+"</p>");templateContent.append("<p><h2>感谢您对CIIP平台的支持。</h2></p>");templateContent.append("<p>您的邮箱验证码:</p>");templateContent.append("<p>{code}</p>");templateContent.append("<p>(该验证码5分钟后需要重新获取。该邮箱由CIIP账号安全服务系统自动发送,请勿直接回复)</p>");//模板字符窜String templateContentStr = templateContent.toString();//填充模板参数Map<String,String> paramsMap = new HashMap<String,String>();//对方用户名称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());}

邮件工具-MailUtil(发送邮件)相关推荐

  1. JAVA SpringBoot 使用 hutool 工具实现发送邮件功能

    官方文档  邮件工具-MailUtil (hutool.cn)https://hutool.cn/docs/#/extra/%E9%82%AE%E4%BB%B6%E5%B7%A5%E5%85%B7-M ...

  2. 发送邮件功能:使用Spring Email、邮件工具类、使用Thymeleaf模板引擎 发送html邮件

    发送邮件 Spring Email 开启自己邮箱的POP3/SMTP服务 导入spring mail 依赖 <!-- https://mvnrepository.com/artifact/org ...

  3. Java Email 发HTML邮件工具 采用 freemarker模板引擎渲染

    Java Email 发HTML邮件工具 采用 freemarker模板引擎 1.常用方式对比 Java发送邮件有很多的实现方式 第一种:Java 原生发邮件mail.jar和activation.j ...

  4. 10.16-17 mailqmail:显示邮件传输队列发送邮件

    mailq命令 是mail queue(邮件队列)的缩写,它会显示待发送的邮件队列,显示的条目包括邮件队列ID.邮件大小.加入队列时间.邮件发送者和接受者.如果邮件进行最后一次尝试后还没有将邮件投递出 ...

  5. spring 框架-java配置163邮件服务器,发送邮件

    java配置163邮件服务器,发送邮件 首先保证spring框架包的正确导入 1.利用spring的邮件服务 进行配置邮件服务器 再导入:javax.mail.jar这是邮件服务的插件实现 sprin ...

  6. php邮件发送tp,Thinkphp5 邮件发送Thinkphp发送邮件

    在项目的开发中 用户修改密码,需要发送验证码到用户邮箱, 在common.php公共文件中加入以下代码: /** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 ...

  7. 用ping查看邮件服务器的ip,发送邮件直接访问服务器ip_邮件工具教程

    作者:代代   来源:电脑报 通常我们在设置邮件服务器时,都是直接设置邮件接收.发送服务器的域名,例如搜狐邮箱的发送邮件服务器设置为:smtp.sohu.com,接收邮件服务器:pop.sohu.co ...

  8. 用java开发Email工具之发送邮件 (1)作者:冯睿

    作者:冯睿 来源:赛迪网 本文介绍了如何利用Java的网络API来实现一个电子邮件工具程序.通常Email工具都 是使用SMTP(简单邮件传输协议, Simple Mail Transfer Prot ...

  9. java邮件抄送_Java发送邮件遇到的常见需求汇总

    基于SMTP发送一个简单的邮件 首先,需要一个认证器: package No001_基于SMTP的文本邮件; import javax.mail.Authenticator; import javax ...

最新文章

  1. hdu5094(上海邀请赛E) 状态压缩bfs:取钥匙开门到目的地
  2. Linux下apache服务器安装,sqlite安装,apache启动,关闭,重启,编写cig程序进行测试,浏览器访问cig程序
  3. MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式
  4. (转)TDI FILTER 网络过滤驱动完全解析
  5. GP学习整理(一)—Geoprocessing assembly and Geoprocessor managed assembly
  6. virtualenv搭建虚拟环境
  7. 浏览器,图片格式及特点
  8. Spring Data JPA持久层中的一对一和一对多
  9. 教你炒股票21:缠中说禅买卖点分析的完备性
  10. 【数据结构(郝斌)】01-数据结构概述
  11. DAY1——sql 建表/插入数据
  12. 2年工作经验杂谈(java开发)
  13. Win7 和 xp 双系统安装
  14. 欧美html游戏,欧美HTML社区服务游戏
  15. Flexbox 基础知识
  16. sRGB,RAW图像意义
  17. 服务器硬件规格常用查看命令——CPU相关命令
  18. 内存溢出(out of memory)是内存不足吗?Outofmemory error怎么解决?
  19. CSS 2 emmet语法 复合选择器 元素显示模式
  20. Microk8s单机安装

热门文章

  1. Oracle Linux 8.5上架微软商店;英特尔发力 RISC-V;200 亿参数 GPT-NeoX 即将开源 | 开源日报
  2. 李开复:搞无人车在电车难题上论争个没完,这样子不行的
  3. 2020年全年财报稳健高增长,除了赛道利好之外微盟是如何做到的?
  4. p0f - 被动探测操作系统工具
  5. 轻量级网络——MobileNet系列学习(理论篇)
  6. 传奇开服怎么开服?不会技术自己能开服吗?传奇开服需要准备什么?前期需要投入多少?
  7. 从Word中读取内容将word转换成txt
  8. Python Ews exchange发送邮件demo
  9. Java从入门到实战总结-4.2、数据库高级
  10. 解决vmware虚拟机和宿主机之间不能复制粘贴问题