java发送邮件
发邮件最常用的是Spring Boot的JavaMailSender
发送邮件
首先这里先理解一个基础概念,所以邮箱都遵循一个协议叫SMTP
SMTP(Simple Mail Transfer Protocol,简单邮件传输协议) 是电子邮件传输的核心协议
无论是qq邮箱还是网易邮箱,只要遵循smtp协议就能互相发邮件
因此我们在项目中使用的协议也是SMTP
引入依赖
在pom.xml中添加Spring Boot Starter Mail依赖:
<!-- 邮箱依赖包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
该依赖会自动引入JavaMailSender,简化邮件配置和发送流程
配置SMTP服务器:
在application.properties或application.yml中配置邮箱服务商(如Gmail、QQ邮箱)的SMTP信息:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your-email@gmail.com
spring.mail.password=your-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
这里的信息需要你自己在邮箱中开通,获取信息,然后填入
因为本质就是利用你的邮箱往其他人的邮箱发送信息
代码
代码就很简单了,直接注入JavaMailSender 对象,设置message信息 调用send方法
@Service
public class MailService {@Autowiredprivate JavaMailSender mailSender;public void sendSimpleMail(String to, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom("your-email@gmail.com");message.setTo(to);message.setSubject(subject);message.setText(content);mailSender.send(message);}
}
自定义HTML
其他的没有什么变动,主要就是mimeHelper.setText(content, true);
@Overridepublic void sendHtmlEmail(EmailHtmlSendRpcCmd cmd){try {MimeMessage message = emailSender.createMimeMessage();MimeMessageHelper mimeHelper = new MimeMessageHelper(message, true);mimeHelper.setFrom(config.getUserName());mimeHelper.setTo(cmd.getReceivers());mimeHelper.setSubject(cmd.getTitle());// 模板处理String content = getTemplateHtml(cmd.getTemplate().getTemplate(),cmd.getParams());mimeHelper.setText(content, true);emailSender.send(message);} catch (Exception e) {throw ExFactory.bizException(EMAIL_SEND_ERROR);}}
获取模板内容
private String getTemplateHtml(String templateName, Map<String, Object> params) {try {Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);Writer writer =new StringWriter();template.process(params,writer);String html =writer.toString();return html;} catch (Exception e) {throw new BizException(EMAIL_SEND_ERROR.getKey(), "模板转换失败");}}
freeMark的配置信息
spring.freemarker.cache=false
spring.freemarker.template-loader-path=classpath:/email-templates/
spring.freemarker.suffix=.html
spring.freemarker.charset=UTF-8
这里freeMark是页面渲染工具
需要引入依赖
<!-- freemarker页面渲染依赖包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>
模板内容如下
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>邮件发送</title>
</head>
<body>
<div><div align="center"><div class="open_email" style="margin-left: 8px; margin-top: 8px; margin-bottom: 8px; margin-right: 8px;"><div><br><span class="genEmailContent"><div id="cTMail-Wrap"style="word-break: break-all;box-sizing:border-box;text-align:center;min-width:320px; max-width:660px; border:1px solid #f6f6f6; background-color:#f7f8fa; margin:auto; padding:20px 0 30px; font-family:'helvetica neue',PingFangSC-Light,arial,'hiragino sans gb','microsoft yahei ui','microsoft yahei',simsun,sans-serif"><div class="main-content" style=""><table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse"><tbody><tr style="font-weight:300"><td style="width:3%;max-width:30px;"></td><td style="max-width:600px;"><!-- {{-- LOGO --}} --><div id="cTMail-logo" style="width:92px; height:25px;"><!-- {{-- 替换跳转链接 --}} --><a href=""><!-- {{-- 替换LOGO图片 --}} --><img border="0" src="https://imgcache.qq.com/open_proj/proj_qcloud_v2/mc_2014/cdn/css/img/mail/logo-pc.png"style="width:92px; height:25px;display:block"></a></div><!-- {{-- 页面上边的蓝色分割线 --}} --><p style="height:2px;background-color: #00a4ff;border: 0;font-size:0;padding:0;width:100%;margin-top:20px;"></p><div id="cTMail-inner" style="background-color:#fff; padding:23px 0 20px;box-shadow: 0px 1px 1px 0px rgba(122, 55, 55, 0.2);text-align:left;"><table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse;text-align:left;"><tbody><!-- {{-- 第一个单元格 --}} --><tr style="font-weight:300"><!-- {{-- 左侧表格,设置左边距用的 --}} --><td style="width:3.2%;max-width:30px;"></td><!-- {{-- 中间表格,正文使用 --}} --><td style="max-width:480px;text-align:left;"><!-- {{-- 以下是正文 --}} --><!-- {{-- 可以是标题 --}} --><h1 id="cTMail-title" style="font-size: 20px; line-height: 36px; margin: 0px 0px 22px;">【华天云测】欢迎注册检测系统</h1><p id="cTMail-userName" style="font-size:14px;color:#333; line-height:24px; margin:0;">尊敬的${userName},您好!</p><p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;"><span style="color: rgb(51, 51, 51); font-size: 14px;">欢迎注册。</span></p><p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;"><span style="color: rgb(51, 51, 51); font-size: 14px;">完成注册,请点击下面按钮验证邮箱。<span style="font-weight: bold;">非本人操作可忽略。</span></span></p><!-- {{-- 按钮 --}} --><p class="cTMail-content"style="font-size: 14px; color: rgb(51, 51, 51); line-height: 24px; margin: 6px 0px 0px; word-wrap: break-word; word-break: break-all;"><!-- {{-- 下面替换成自己的链接 --}} --><a id="cTMail-btn" href="" title=""style="font-size: 16px; line-height: 45px; display: block; background-color: rgb(0, 164, 255); color: rgb(255, 255, 255); text-align: center; text-decoration: none; margin-top: 20px; border-radius: 3px;">点击此处验证邮箱</a></p><p class="cTMail-content" style="line-height: 24px; margin: 6px 0px 0px; overflow-wrap: break-word; word-break: break-all;"><span style="color: rgb(51, 51, 51); font-size: 14px;"><br>无法正常显示?请复制以下链接至浏览器打开:<br><a href="" title=""style="color: rgb(0, 164, 255); text-decoration: none; word-break: break-all; overflow-wrap: normal; font-size: 14px;">这里是激活账号的链接</a></span></p><!-- {{-- 来个署名 --}} --><dl style="font-size: 14px; color: rgb(51, 51, 51); line-height: 18px;"><dd style="margin: 0px 0px 6px; padding: 0px; font-size: 12px; line-height: 22px;"><p id="cTMail-sender" style="font-size: 14px; line-height: 26px; word-wrap: break-word; word-break: break-all; margin-top: 32px;">此致<br><strong>检测组团队</strong></p></dd></dl></td><!-- {{-- 右侧表格,设置右边距用的 --}} --><td style="width:3.2%;max-width:30px;"></td></tr></tbody></table></div><!-- {{-- 页面底部的推广 --}} --><div id="cTMail-copy" style="text-align:center; font-size:12px; line-height:18px; color:#999"><table style="width:100%;font-weight:300;margin-bottom:10px;border-collapse:collapse"><tbody><tr style="font-weight:300">{<!-- {-- 左,左边距 --}} --><td style="width:3.2%;max-width:30px;"></td><!-- {{-- 中,正文 --}} --><td style="max-width:540px;"><p style="text-align:center; margin:20px auto 14px auto;font-size:12px;color:#999;">此为系统邮件,请勿回复。<!-- {{-- 可以加个链接 --}} --><a href=""style="text-decoration:none;word-break:break-all;word-wrap:normal; color: #333;" target="_blank">取消订阅</a></p><!-- {{-- 可以加个图片,公众号二维码之类的 --}} --><p id="cTMail-rights" style="max-width: 100%; margin:auto;font-size:12px;color:#999;text-align:center;line-height:22px;"><img border="0" src="http://imgcache.qq.com/open_proj/proj_qcloud_v2/tools/edm/css/img/wechat-qrcode-2x.jpg"style="width:64px; height:64px; margin:0 auto;"><br>关注服务号,移动管理云资源<br><img src="https://imgcache.qq.com/open_proj/proj_qcloud_v2/gateway/mail/cr.svg" style="margin-top: 10px;"></p></td><!-- {{-- 右,右边距 --}} --><td style="width:3.2%;max-width:30px;"></td></tr></tbody></table></div></td><td style="width:3%;max-width:30px;"></td></tr></tbody></table></div></div></span></div></div></div>
</div>
</body>
</html>