当前位置: 首页 > wzjs >正文

大型服装网站开发泾川门户网

大型服装网站开发,泾川门户网,外包网易,惠州seo关键词推广发邮件最常用的是Spring Boot的JavaMailSender 发送邮件 首先这里先理解一个基础概念,所以邮箱都遵循一个协议叫SMTP SMTP(Simple Mail Transfer Protocol,简单邮件传输协议) 是电子邮件传输的核心协议 无论是qq邮箱还是网易邮…

发邮件最常用的是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>

文章转载自:

http://Sx5fLAXK.jtdzn.cn
http://LFrWQQf7.jtdzn.cn
http://i33MD20P.jtdzn.cn
http://tioPmKxy.jtdzn.cn
http://2oWFEo58.jtdzn.cn
http://bAE2ePpg.jtdzn.cn
http://SDhCAWv0.jtdzn.cn
http://gPn36X0H.jtdzn.cn
http://hqiPC9cc.jtdzn.cn
http://bcruZ0xK.jtdzn.cn
http://IIjXysoa.jtdzn.cn
http://eHuO76ny.jtdzn.cn
http://eIG1Qa8q.jtdzn.cn
http://r05oILxP.jtdzn.cn
http://MtgyqC2T.jtdzn.cn
http://vp0uZVsH.jtdzn.cn
http://g3JOWiVC.jtdzn.cn
http://wInKD5cv.jtdzn.cn
http://sUZMN4Cv.jtdzn.cn
http://9M5gbIMf.jtdzn.cn
http://EJqHx9qU.jtdzn.cn
http://1Y7Na0yl.jtdzn.cn
http://BghXRrhT.jtdzn.cn
http://iMXEd4Mi.jtdzn.cn
http://65CCKApL.jtdzn.cn
http://jC4ihB13.jtdzn.cn
http://coXCjLun.jtdzn.cn
http://5Igl7irF.jtdzn.cn
http://LFIjfNIX.jtdzn.cn
http://0d5Ovf8q.jtdzn.cn
http://www.dtcms.com/wzjs/618446.html

相关文章:

  • 南昌网站搜索排名网站合同需要注意什么呢
  • 越秀网站建设价格wordpress首页不显示最新文章
  • jn建站系统网吧网络维护公司
  • 两学一做网站进不去wordpress主题开发编辑器
  • 广东地区建网站的公司有谁用2008做网站服务器
  • 泉州网站建设企业什么样的笔记本电脑适合网站开发
  • 福州建站模板搭建视频优化软件
  • 韩城市网站建设一个做网站的团队需要哪些
  • 自己电脑做服务器上传网站 需要备案吗漳州seo顾问
  • 网站建设栏目怎么介绍兰州网站建设技能论文
  • 网站做app开发最新网站推广
  • 柳城企业网站建设公司西安加盟代理网站建设
  • 做百科需要用什么网站做参考建设互联网站的目的
  • 南宁网站建设地方做网站gzip压缩
  • 商城网站开发项目描述中国空间站简笔画
  • 比较好的室内设计网站厦门市建设工程质监站网站
  • 学校网站 建设 价格软件项目管理期末考试
  • 网站建设需要哪些工作室网站域名使用费用
  • 最便宜做网站的方法网址站点异常怎么解决
  • 新动力网站建设可以做照片书的网站
  • 惠州公司网站建设龙岩网站建设大概费用
  • 厦门php商城网站建设易居房产网下载
  • 杭州互助盘网站开发互联网营销师培训机构哪家好
  • 做相册的网站有哪些google谷歌搜索
  • 个人网站建设方案策划书好看的网站设计网站
  • 长春网站设计制作成都自助建站软件
  • 柳州网站制作推荐网络游戏带来的危害
  • 网站建设的目的意义涉县专业做网站
  • WordPress下载统计呼市网站优化
  • 公司网站建设与维护方案广州专业的网站推广工具