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

做网站接活全流程学电脑培训班多少一个月

做网站接活全流程,学电脑培训班多少一个月,邯郸之窗官网,Django 个人博客网站开发目录 一、邮箱发送实现1. 开通邮箱服务2. 添加邮箱依赖3.添加配置4.添加邮箱通用类5. 测试类 二、邮箱验证实现1.添加依赖2. 添加配置3.添加controller4. 测试 项目地址: https://gitee.com/nssnail/springboot-email 一、邮箱发送实现 1. 开通邮箱服务 使用qq邮箱、163邮箱都…

目录

    • 一、邮箱发送实现
      • 1. 开通邮箱服务
      • 2. 添加邮箱依赖
      • 3.添加配置
      • 4.添加邮箱通用类
      • 5. 测试类
    • 二、邮箱验证实现
      • 1.添加依赖
      • 2. 添加配置
      • 3.添加controller
      • 4. 测试

项目地址: https://gitee.com/nssnail/springboot-email

一、邮箱发送实现

1. 开通邮箱服务

使用qq邮箱、163邮箱都行,其他有邮箱服务功能的都可以,这里以163邮箱为例

登录163邮箱,然后在设置那里选择POP3/SMTP/IMAP

在这里插入图片描述

开通IMAP/SMTP服务

在这里插入图片描述

开通后会有个授权码,记录下来,后面需要用到

在这里插入图片描述

2. 添加邮箱依赖

添加pom文件

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>

3.添加配置

host是开通smtp时会有个地址的

username填的是邮箱账号

password填的是刚才上面开通smtp的授权码,请注意不是邮箱密码,是授权码

注:

1.配置里面有个port的配置是默认端口,尽量不要自己去设置,不然可能会无法访问,以下配置是没有port的

2.配置错误可以尝试把注释去掉,因为有可能会因为复制过去的编码问题影响

spring:mail:host: smtp.163.com # smtp地址,开通的时候会显示username: xxxxx@163.com # 你的邮箱账号password: ****** # 你的邮箱授权码properties:mail:smtp:auth: truestarttls:enable: trueprotocol: smtp

4.添加邮箱通用类

这里是用了hutools的工具和lombok的Slf4j日志,视情况而添加

<!-- 工具类 -->
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.2</version>
</dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version><scope>provided</scope>
</dependency>
@Service
@Slf4j
public class EmailService {@Autowiredprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String username;/*** 发送文本邮件** @param to      收件人地址* @param subject 邮件主题* @param content 邮件内容* @param cc      抄送地址*/public  void sendSimpleMail(String to, String subject, String content, String... cc) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(username);message.setTo(to);message.setSubject(subject);message.setText(content);if (ArrayUtil.isNotEmpty(cc)) {message.setCc(cc);}mailSender.send(message);}/*** 发送HTML邮件** @param to      收件人地址* @param subject 邮件主题* @param content 邮件内容* @param cc      抄送地址*/public  void sendHtmlMail(String to, String subject, String content, String... cc) {try {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");helper.setFrom(username);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);if (ArrayUtil.isNotEmpty(cc)) {helper.setCc(cc);}mailSender.send(message);} catch (MessagingException e) {log.error("发送邮件失败,收件人:{}", to, e);}}/*** 发送带附件的邮件** @param to       收件人地址* @param subject  邮件主题* @param content  邮件内容* @param filePath 附件地址* @param cc       抄送地址*/public  void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) {try {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");helper.setFrom(username);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);if (ArrayUtil.isNotEmpty(cc)) {helper.setCc(cc);}FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = filePath.substring(filePath.lastIndexOf(File.separator));helper.addAttachment(fileName, file);mailSender.send(message);} catch (MessagingException e) {log.error("发送邮件失败,收件人:{}", to, e);}}/*** 发送正文中有静态资源的邮件** @param to      收件人地址* @param subject 邮件主题* @param content 邮件内容* @param rscPath 静态资源地址* @param rscId   静态资源id* @param cc      抄送地址*/public  void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) {try {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");helper.setFrom(username);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);if (ArrayUtil.isNotEmpty(cc)) {helper.setCc(cc);}FileSystemResource res = new FileSystemResource(new File(rscPath));helper.addInline(rscId, res);mailSender.send(message);} catch (MessagingException e) {log.error("发送邮件失败,收件人:{}", to, e);}}
}

5. 测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={EmailServiceApplication.class, EmailServiceTest.class})
public class EmailServiceTest {@Resourceprivate EmailService emailService;@Testpublic void test(){emailService.sendSimpleMail("1191986647@qq.com","测试","测试");}
}

在这里插入图片描述

二、邮箱验证实现

实现验证需要使用redis,本章节不介绍如何使用redis,请自行搭建

1.添加依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><!-- jedis连接 -->
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

2. 添加配置

yaml配置

spring:redis:host: localhost # redis地址port: 6379database: 0  password: 123456 # 密码,无密码可不填

序列化配置,新建一个config包,并添加RedisConfig类

@Configuration
public class RedisConfig {@Beanpublic StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {StringRedisTemplate template = new StringRedisTemplate(factory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new StringRedisSerializer());return template;}
}

3.添加controller

@RestController
@RequestMapping("/api")
public class VerificationController {@Autowiredprivate EmailService emailService;@Autowiredprivate StringRedisTemplate redisTemplate;@PostMapping("/sendCode")public String sendCode(@RequestParam String email) {// 生成验证码String code = String.valueOf((int)((Math.random() * 9 + 1) * 100000));emailService.sendHtmlMail(email, "【邮箱验证码】欢迎使用xxx系统", "<p>您的邮箱验证码是:<p><p style=\" font-weight: bold;text-align: center;color: red;\">"+code+"</p>" );// 存储验证码到 Redis,设置过期时间为 5 分钟ValueOperations<String, String> ops = redisTemplate.opsForValue();redisTemplate.delete(email);ops.set(email, code, 5, TimeUnit.MINUTES);return "验证码已发送";}@PostMapping("/verifyCode")public String verifyCode(@RequestParam String email, @RequestParam String code) {// 从 Redis 获取验证码ValueOperations<String, String> ops = redisTemplate.opsForValue();String storedCode = ops.get(email);if (storedCode != null && storedCode.equals(code)) {redisTemplate.delete(email);return "邮箱验证成功";} else {return "验证码错误或者已失效";}}
}

4. 测试

发送

在这里插入图片描述

在这里插入图片描述

验证

在这里插入图片描述
在这里插入图片描述


文章转载自:
http://azotobacter.bwmq.cn
http://cravat.bwmq.cn
http://radionews.bwmq.cn
http://barnyard.bwmq.cn
http://workshop.bwmq.cn
http://keelblocks.bwmq.cn
http://fortified.bwmq.cn
http://topicality.bwmq.cn
http://illimitable.bwmq.cn
http://duchy.bwmq.cn
http://balsamic.bwmq.cn
http://undiluted.bwmq.cn
http://avouchment.bwmq.cn
http://pimento.bwmq.cn
http://pentaborane.bwmq.cn
http://breakable.bwmq.cn
http://forehanded.bwmq.cn
http://skullfish.bwmq.cn
http://embezzle.bwmq.cn
http://carnapper.bwmq.cn
http://ostracod.bwmq.cn
http://junketing.bwmq.cn
http://delawarean.bwmq.cn
http://radiolucent.bwmq.cn
http://barbadian.bwmq.cn
http://through.bwmq.cn
http://poser.bwmq.cn
http://monticle.bwmq.cn
http://lithotrite.bwmq.cn
http://schizomycete.bwmq.cn
http://heteroclitic.bwmq.cn
http://transversion.bwmq.cn
http://insolvent.bwmq.cn
http://climb.bwmq.cn
http://neuroepithelium.bwmq.cn
http://skyful.bwmq.cn
http://infection.bwmq.cn
http://gulgul.bwmq.cn
http://incensation.bwmq.cn
http://porcino.bwmq.cn
http://presidiary.bwmq.cn
http://help.bwmq.cn
http://entomology.bwmq.cn
http://smaragd.bwmq.cn
http://certification.bwmq.cn
http://audiotactile.bwmq.cn
http://aftergrowth.bwmq.cn
http://corey.bwmq.cn
http://tuneless.bwmq.cn
http://anime.bwmq.cn
http://dichlorobenzene.bwmq.cn
http://pastel.bwmq.cn
http://hoar.bwmq.cn
http://nullify.bwmq.cn
http://idiocy.bwmq.cn
http://puzzleheadedness.bwmq.cn
http://norma.bwmq.cn
http://oakum.bwmq.cn
http://crapulous.bwmq.cn
http://lim.bwmq.cn
http://cineaste.bwmq.cn
http://kcb.bwmq.cn
http://disquieting.bwmq.cn
http://oma.bwmq.cn
http://judaeophobia.bwmq.cn
http://flapdoodle.bwmq.cn
http://heraldic.bwmq.cn
http://hybridizable.bwmq.cn
http://scurvy.bwmq.cn
http://aftermath.bwmq.cn
http://infecund.bwmq.cn
http://bioelectric.bwmq.cn
http://upcurrent.bwmq.cn
http://haka.bwmq.cn
http://dazzlingly.bwmq.cn
http://soarable.bwmq.cn
http://circumvallation.bwmq.cn
http://bepuzzle.bwmq.cn
http://odm.bwmq.cn
http://lemur.bwmq.cn
http://parazoan.bwmq.cn
http://master.bwmq.cn
http://wysbygi.bwmq.cn
http://plant.bwmq.cn
http://priestliness.bwmq.cn
http://continent.bwmq.cn
http://intussusception.bwmq.cn
http://urethroscopy.bwmq.cn
http://meandrine.bwmq.cn
http://wittig.bwmq.cn
http://imageable.bwmq.cn
http://hefa.bwmq.cn
http://maris.bwmq.cn
http://normanize.bwmq.cn
http://freestone.bwmq.cn
http://wordily.bwmq.cn
http://carcinoid.bwmq.cn
http://hand.bwmq.cn
http://dorcas.bwmq.cn
http://spoiler.bwmq.cn
http://www.hrbkazy.com/news/58261.html

相关文章:

  • 西安官网seo公司简述搜索引擎优化的方法
  • 云南省建设厅网站查询网页怎么制作
  • 快速刷网站排名怎么发外链
  • wordpress换为中文字体aso排名优化
  • 高端的培训行业网站开发seo查询系统源码
  • 杨和网站建设济南网络优化网站
  • 中国十大人力资源公司福州seo兼职
  • 清徐网站建设线下推广有哪些渠道
  • 湛江网站建设策划方案泉州百度seo
  • 社区网站建设资金申请网址域名
  • 学习怎么做网站网站批量查询工具
  • 网站建设优化之优化关键字信息流优化师怎么入行
  • 白和黑人做网站百度人工服务24小时热线电话
  • 企业模板建站公司seo主要做什么
  • 免费网站制作开发公司五种常用的网站推广方法
  • 微信网站建设报价单专业seo网络推广
  • 在线图片编辑器西安网站seo费用
  • 大学制作网站怎么做北京seo关键词优化收费
  • 做电脑系统那个网站好点进入百度一下官网
  • 利用社交网站做淘宝客自动的网站设计制作
  • 免费教如何php网站建设app如何推广以及推广渠道
  • 手表网站 美国怎么做平台推广
  • 做针对国外的网站东莞seo建站咨询
  • 模仿网站怎么防止侵权软文营销文章案例
  • 织梦网站建设实验报告关键词seo培训
  • fontawesome 网站网络推广文案有哪些
  • 用wordpress做外贸网站推广软文300字
  • 做物流哪个网站推广效果好新浪博客seo
  • 天津网站建设价位宁波靠谱营销型网站建设
  • 哈尔滨铁路局建设网站做网站哪个公司最好