- 一、准备
- 二、使用步骤
- 1.导入依赖
- 2.在ymal配置文件中写入配置
- 3.ftl模板
- 4.发送邮件方法
- 5.结果展示
一、准备
目前国内大部分的邮件服务商都不允许直接使用用户名/密码的方式来在代码中发送邮件,都是要先申请授权码,这里以 QQ 邮箱为例,向大家演示授权码的申请流程:首先我们需要先登录 QQ 邮箱网页版,点击上方的设置按钮:
然后点击账户选项卡,在账户选项卡中找到开启POP3/SMTP选项,点击开启,开启相关功能,开启过程需要手机号码验证,按照步骤 *** 作即可,不赘述。开启成功之后,即可获取一个授权码
二、使用步骤 1.导入依赖2.在ymal配置文件中写入配置org.springframework.boot spring-boot-starter-mailorg.springframework.boot spring-boot-starter-freemarker
代码如下(示例):
spring: mail: username: 749888745@qq.com password: pcuvzfecwkozbbdh host: smtp.qq.com
配置参数含义:
username:邮箱名称
password:第一步申请到的授权码
host:SMTP 服务器地址
4.发送邮件方法Title 你好!
你已经成功订阅如下信息,若信息发生变更时会及时通知您~
<#if scheduleType==1>排期ID 排期名称 当前节点 节目日期 节目开始时间 节目结束时间 授权产品 授权地区 水印 合作方 主路接入方式 主路信源地址 备路接入方式 备路信源地址 收费策略 <#list schedules as schedule><#if (schedule.id)??>${schedule.id}<#else>${(schedule.id)!}#if> <#if (schedule.scheduleName)??>${schedule.scheduleName}<#else>${(schedule.scheduleName)!}#if> <#if (schedule.scheduleNode)??>${schedule.scheduleNode}<#else>${(schedule.scheduleNode)!}#if> <#if (schedule.playingDate)??>${schedule.playingDate}<#else>${(schedule.playingDate)!}#if> <#if (schedule.liveStartTime)??>${schedule.liveStartTime}<#else>${(schedule.liveStartTime)!}#if> <#if (schedule.liveEndTime)??>${schedule.liveEndTime}<#else>${(schedule.liveEndTime)!}#if> <#if (schedule.authProduct)??>${schedule.authProduct}<#else>${(schedule.authProduct)!}#if> <#if (schedule.authArea)??>${schedule.authArea}<#else>${(schedule.authArea)!}#if> <#if (schedule.waterMarker)??>${schedule.waterMarker}<#else>${(schedule.waterMarker)!}#if> <#if (schedule.collaborator)??>${schedule.collaborator}<#else>${(schedule.collaborator)!}#if> <#if (schedule.mainUrlType)??>${schedule.mainUrlType}<#else>${(schedule.mainUrlType)!}#if> <#if (schedule.mainUrl)??>${schedule.mainUrl}<#else>${(schedule.mainUrl)!}#if> <#if (schedule.backupUrlType)??>${schedule.backupUrlType}<#else>${(schedule.backupUrlType)!}#if> <#if (schedule.backupUrl)??>${schedule.backupUrl}<#else>${(schedule.backupUrl)!}#if> <#if (schedule.chargeStrategy)??>${schedule.chargeStrategy}<#else>${(schedule.chargeStrategy)!}#if> #list> <#else>排期ID 排期名称 当前节点 节目日期 节目开始时间 比赛时间 节目结束时间 赛事 轮次/项目 演播室 解说/嘉宾 收费策略 <#list schedules as schedule><#if (schedule.id)??>${schedule.id}<#else>${(schedule.id)!}#if> <#if (schedule.scheduleName)??>${schedule.scheduleName}<#else>${(schedule.scheduleName)!}#if> <#if (schedule.scheduleNode)??>${schedule.scheduleNode}<#else>${(schedule.scheduleNode)!}#if> <#if (schedule.playingDate)??>${schedule.playingDate}<#else>${(schedule.playingDate)!}#if> <#if (schedule.liveStartTime)??>${schedule.liveStartTime}<#else>${(schedule.liveStartTime)!}#if> <#if (schedule.playingTime)??>${schedule.playingTime}<#else>${(schedule.playingTime)!}#if> <#if (schedule.liveEndTime)??>${schedule.liveEndTime}<#else>${(schedule.liveEndTime)!}#if> <#if (schedule.liveBusiness)??>${schedule.liveBusiness}<#else>${(schedule.liveBusiness)!}#if> <#if (schedule.round)??>${schedule.round}<#else>${(schedule.round)!}#if> <#if (schedule.studio)??>${schedule.studio}<#else>${(schedule.studio)!}#if> <#if (schedule.narrator)??>${schedule.narrator}<#else>${(schedule.narrator)!}#if> <#if (schedule.chargeStrategy)??>${schedule.chargeStrategy}<#else>${(schedule.chargeStrategy)!}#if> #list> #if>
- - - - - - - - - - - - - - - - - - - - - -
订阅通知
public void sendMail(List5.结果展示scheduleInfos, String emails, Integer emailType, Integer node, JavaMailSender javaMailSender) { try { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); //发件人,邮箱地址,别名 helper.setFrom("749888745@qq.com", "小王"); helper.setTo(emails.split(",")); helper.setSentDate(new Date()); //构建 Freemarker 的基本配置 // 配置模板位置 Map map = new HashMap<>(); //此处的scheduleTemplateVos对应模板中的集合参数 map.put("schedules", scheduleTemplateVos); map.put("node", node); Configuration configuration = new Configuration(Configuration.VERSION_2_3_28); configuration.setClassForTemplateLoading(this.getClass(), "/static"); //设置参数不让数值化 configuration.setNumberFormat("#"); String ftl; String subject; switch (emailType) { case 1: ftl = "subscribe.ftl"; //主题 subject = "订阅成功通知"; break; case 2: ftl = "change.ftl"; subject = "订阅信息变更通知"; break; case 3: ftl = "nodeflow.ftl"; subject = "节点流转通知"; break; default: ftl = ""; subject = ""; break; } String emailContent = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(ftl), map); helper.setSubject(subject); helper.setText(emailContent, true); javaMailSender.send(mimeMessage); } catch (Exception e) { log.error("---------->error:{}", e.getMessage(), e); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)