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

北京网站建设案例定制开发软件产品的税率

北京网站建设案例,定制开发软件产品的税率,2017国办网站建设规范,足球比赛直播app使用SpringBoot发送邮件 邮件发送其实是一个非常常见的需求#xff0c;用户注册#xff0c;找回密码等地方#xff0c;都会用到#xff0c;Spring Boot 中对于邮件发送#xff0c;提供了相关的自动化配置类#xff0c;使得邮件发送变得非常容易。 1、前置工作 目前国内…使用SpringBoot发送邮件 邮件发送其实是一个非常常见的需求用户注册找回密码等地方都会用到Spring Boot 中对于邮件发送提供了相关的自动化配置类使得邮件发送变得非常容易。 1、前置工作 目前国内大部分的邮件服务商都不允许直接使用用户名/密码的方式来在代码中发送邮件都是要先申请授权码这里以 QQ 邮箱为例向大家演示授权码的申请流程 首先我们需要先登录 QQ 邮箱网页版点击上方的设置按钮然后点击账户选项卡在账户选项卡中找到开启POP3/SMTP选项如下 点击开启开启相关功能开启过程需要手机号码验证按照步骤操作即可不赘述。开启成功之后即可获取一个授权码将该号码保存好一会使用。 2、引入依赖、配置邮箱基本信息 !--集成发送邮件的功能-- dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId /dependency然后在yml配置文件中进行配置 spring:mail:host: smtp.qq.com # 设置邮箱主机port: 587 # SMTP 服务器的端口username: yydsqq.com # 设置用户名password: yyds # 设置密码该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码mail:from: ${spring.mail.username}to: yyds163.com做完这些之后Spring Boot 就会自动帮我们配置好邮件发送类相关的配置在 org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration 类中部分源码如下 Configuration ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class }) ConditionalOnMissingBean(MailSender.class) Conditional(MailSenderCondition.class) EnableConfigurationProperties(MailProperties.class) Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class }) public class MailSenderAutoConfiguration {}可以看到导入了另外一个配置 MailSenderPropertiesConfiguration 类这个类中提供了邮件发送相关的工具类源码如下 Configuration ConditionalOnProperty(prefix spring.mail, name host) class MailSenderPropertiesConfiguration {private final MailProperties properties;MailSenderPropertiesConfiguration(MailProperties properties) {this.properties properties;}BeanConditionalOnMissingBeanpublic JavaMailSenderImpl mailSender() {JavaMailSenderImpl sender new JavaMailSenderImpl();applyProperties(sender);return sender;} } 可以看到这里创建了一个 JavaMailSenderImpl 的实例 JavaMailSenderImpl 是 JavaMailSender 的一个实现我们将使用 JavaMailSenderImpl 来完成邮件的发送工作。 3、Service层代码 自定义的MailProperties配置类用于解析mail开头的配置属性 package com.yyds.domain;import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;Data Component ConfigurationProperties(prefix mail) public class MailProperties {private String from;private String to;}service层 package com.yyds.service;import freemarker.template.TemplateException;import javax.mail.MessagingException; import java.io.IOException; import java.util.Map;public interface MailService {void sendSimpleMail(String subject, String text) ;void sendHtmlMail(String subject, String text, MapString, String attachmentMap) throws MessagingException;void sendTemplateMail(String subject, MapString, Object params) throws MessagingException, IOException, TemplateException; } package com.yyds.service.impl;import com.yyds.domain.MailProperties; import com.yyds.service.MailService; import freemarker.cache.ClassTemplateLoader; import freemarker.cache.TemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import org.springframework.beans.factory.annotation.Autowired; 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 org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; import java.io.IOException; import java.util.Map;Service public class MailServiceImpl implements MailService {Autowiredprivate JavaMailSender javaMailSender;Autowiredprivate MailProperties myMailProperties;/*** 发送简单文本邮件*/Overridepublic void sendSimpleMail(String subject, String text) {SimpleMailMessage mailMessage new SimpleMailMessage();mailMessage.setFrom(myMailProperties.getFrom());mailMessage.setTo(myMailProperties.getTo());mailMessage.setSubject(subject);mailMessage.setText(text);javaMailSender.send(mailMessage);}/*** 发送带有链接和附件的复杂邮件*/Overridepublic void sendHtmlMail(String subject, String text, MapString, String attachmentMap) throws MessagingException {MimeMessage mimeMessage javaMailSender.createMimeMessage();//是否发送的邮件是富文本附件图片html等MimeMessageHelper messageHelper new MimeMessageHelper(mimeMessage, true);messageHelper.setFrom(myMailProperties.getFrom());messageHelper.setTo(myMailProperties.getTo());messageHelper.setSubject(subject);messageHelper.setText(text, true);//重点默认为false显示原始html代码无效果if(attachmentMap ! null){attachmentMap.entrySet().stream().forEach(entrySet - {try {File file new File(entrySet.getValue());if(file.exists()){messageHelper.addAttachment(entrySet.getKey(), new FileSystemResource(file));}} catch (MessagingException e) {e.printStackTrace();}});}javaMailSender.send(mimeMessage);}/*** 发送模版邮件*/Overridepublic void sendTemplateMail(String subject, MapString, Object params) throws MessagingException, IOException, TemplateException {MimeMessage mimeMessage javaMailSender.createMimeMessage();MimeMessageHelper helper new MimeMessageHelper(mimeMessage, true);helper.setFrom(myMailProperties.getFrom());helper.setTo(myMailProperties.getTo());freemarker.template.Configuration configuration new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_19);TemplateLoader templateLoader new ClassTemplateLoader(this.getClass(), /templates/);configuration.setTemplateLoader(templateLoader);Template template configuration.getTemplate(mail.ftl);String html FreeMarkerTemplateUtils.processTemplateIntoString(template, params);helper.setSubject(subject);helper.setText(html, true);//重点默认为false显示原始html代码无效果javaMailSender.send(mimeMessage);} } 4、发送邮件 4.1 测试发送简单文本邮件 SpringBootTest(classes BootStartApplication.class) public class MimeMailTest {Autowiredprivate MailService mailService;Testpublic void sendMail() {mailService.sendSimpleMail(测试Springboot发送邮件, 发送邮件...);} }4.2 测试发送带有链接和附件的复杂邮件 package com.yyds;import com.yyds.service.MailService; import freemarker.template.TemplateException; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import javax.mail.MessagingException; import java.io.IOException; import java.util.HashMap; import java.util.Map;SpringBootTest(classes BootStartApplication.class) public class MimeMailTest {Autowiredprivate MailService mailService;Testpublic void testMail() throws MessagingException {MapString, String attachmentMap new HashMap();attachmentMap.put(附件, D:\\D_ENL_MRO数据统计.xlsx);mailService.sendHtmlMail(测试Springboot发送带附件的邮件2, 欢迎进入a href\http://www.baidu.com\百度首页/a, attachmentMap);}}4.3 测试发送发送模版邮件 首先需要引入 Freemarker 依赖 !--整合freemarker--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-freemarker/artifactId/dependency然后在 resources/templates 目录下创建一个 mail.ftl 作为邮件发送模板 htmlbodyh3你好 span stylecolor: red;${username}/span, 这是一封模板邮件!/h3/body /htmlpackage com.yyds;import com.yyds.service.MailService; import freemarker.template.TemplateException; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;import javax.mail.MessagingException; import java.io.IOException; import java.util.HashMap; import java.util.Map;SpringBootTest(classes BootStartApplication.class) public class MimeMailTest {Autowiredprivate MailService mailService;Testpublic void testFreemarkerMail() throws MessagingException, IOException, TemplateException {MapString, Object params new HashMap();params.put(username, Tom);mailService.sendTemplateMail(测试Springboot发送模版邮件, params);}}
http://www.hkea.cn/news/14382811/

相关文章:

  • 网站后台管理系统域名专门做灯具海报的网站
  • 塑胶原料东莞网站建设技术支持网站推广流程
  • 广西建设厅官网站首页做网站 对方传销
  • 三明市建设局网站官网中小企业名录
  • 十大黄金软件app免费系统优化
  • 网站建设与管理规划书网站翻页
  • 学校网站建设意义做网站公司法人还要拍照吗
  • 做鞋设备网站淘宝客必须做网站
  • centos wordpress 建站教程兰州网站维护公司
  • 灵犀科技网站开发佼佼者网站开发前端要学什么软件
  • 长春求推荐好的网站优化推广sdk软件开发工具包
  • 电子商务网站开发费用调研报告网站建设公司客户来源渠道
  • 自己设计的网站如何推广网站换域名做301会有影响
  • 国企门户网站建设方案网站建设轮播图
  • 电商网站里的水果图片怎么做的临沂建设局网站官网
  • 北京手机网站开发公司网络设计规划
  • 医院网站建设管理规范列举免费域名注册的网站
  • 网站需要怎么做的页面设计包括插画吗
  • 私人公司怎么做网站口碑好的网站设计制作价格
  • 金华网上商城网站建设网站设计大概流程
  • 网站首屏做多大网站的首页文案
  • 凯里信息网站中铁建设集团有限公司门户登录
  • 我的网站 dedecms公司架构体系搭建
  • 四会网站建设网站推广短信
  • 影视网站建设方案青岛关键词快速排名
  • 网站建设哪家公司便宜网站的建设思想
  • 可以做黄金期权的网站建设银行官方网站客户端
  • 苏州微信网站中国数据域名注册
  • 手机网站源码 php哪些网站可以做店淘客
  • 网站报价表格网站怎么做内容