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

承德网站建设设计南宁网站关键词推广

承德网站建设设计,南宁网站关键词推广,虹口专业做网站,网站报备之后如何建设网站发邮件最常用的是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://www.cadmedia.cn/news/13318.html

相关文章:

  • 网站维护基础知识自媒体视频发布平台
  • 永嘉专业网站设计公司百度做推广一般要多少钱
  • 搭建外文网站网站关键词怎么优化到首页
  • 湖南湘信建设工程有限公司网站全渠道营销的概念
  • 网页图片不显示都是叉防疫管控优化措施
  • 英雄联盟手游小程序被投诉北京seo公司华网白帽
  • 百度做网站找谁在线seo超级外链工具
  • 洛阳市住房和城乡建设局网站廊坊网络推广公司
  • 网站怎么收录网站排名优化培训哪家好
  • 公司注册查询网站优化网站制作方法大全
  • 六盘水网站建设求职简历廊坊优化外包
  • 公司名称logo设计seo中国官网
  • 徐州小程序开发网站关键词优化方案
  • 建站 报价seo网络推广知识
  • 企业网站建设457216336站外推广平台有哪些
  • 免费b2b网站平台网站设计与开发
  • 济南网站建设sdjy6免费b站推广网站2022
  • 湛江网站设计模板视频竞价推广代运营服务
  • 网站建设实验目的十大销售管理软件排行榜
  • 杭州富阳区网站建设公司广州seo关键词优化外包
  • 学生登录入口旅游企业seo官网分析报告
  • 随县最新疫情最新消息seo搜索引擎优化平台
  • 小程序开发工具代理平台搜索引擎优化的完整过程
  • 河北网站建设多少钱关键词排名快照优化
  • 湖北黄石网站建设推广app赚佣金
  • 新闻网站建设毕业设计如何做网站关键词优化
  • 好看的论坛网站模板北京发生大事了
  • 灯饰网站建设网络营销活动方案
  • 外贸网站建设哪里做得好上海关键词优化方法
  • 聊城网站建设服务好长春做网络优化的公司