springboot绑定邮箱激活发邮件
用户注册功能快做完了,还差激活这个步骤。
以前听同学说有免费测试用的短信API,本身想搞个高大上的短信验证码的,前后端界面接口都搞了,网上逛了下聚合数据,百度云阿里云...各种云,看了看价格。劝退了,咱们还是踏实点,用邮箱绑定激活吧(qiong)。
(存入数据库,标记未激活,记下时间,验证码)
注册发邮件(账号不可用)---->用户点击邮件链接---->跳转到激活页面---->后台标记激活(账号可用)----->正常登陆
(验证时间(30分钟过期),邮箱、验证码核对)
为了防止骚扰,准备记录邮箱发送次数,以免产生过多垃圾邮件。
邮箱收发协议主要有POP3、IMAP、SMTP等
导入封装好javaMail的maven坐标:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.1.7.RELEASE</version>
</dependency>
设置邮箱,获取打开smtp协议获取授权密码,比如QQ邮箱:
配置yml:
spring:mail:# 邮件服务器地址 我的是网易126邮箱host: smtp.126.com# 登录用户名username: icu996@126.com# 密码是邮箱提供商的授权码password: icu996# 编码格式default-encoding: UTF-8
Service方法:使用 JavaMailSender 发送邮件。
package com.lyj.mall.service.impl;import com.lyj.mall.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;@Service
public class MailserviceImpl implements MailService {//获取yml配置文件的值@Value("${spring.mail.username}")private String from;@Autowiredprivate JavaMailSender mailSender;/*** 简单文本邮件* @param to 接收者邮件* @param subject 邮件主题* @param content 邮件内容*/@Overridepublic void sendSimpleMail(String to, String subject, String content) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(content);try {mailSender.send(message);} catch (Exception e) {e.printStackTrace();}}/*** HTML 文本邮件* @param to 接收者邮件* @param subject 邮件主题* @param content HTML内容* @throws MessagingException*/public void sendHtmlMail(String to, String subject, String content) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);helper.setFrom(from);mailSender.send(message);}/*** 附件邮件* @param to 接收者邮件* @param subject 邮件主题* @param content HTML内容* @param filePath 附件路径* @throws MessagingException*/public void sendAttachmentsMail(String to, String subject, String content,String filePath) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);helper.setFrom(from);FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = file.getFilename();helper.addAttachment(fileName, file);mailSender.send(message);}/*** 图片邮件* @param to 接收者邮件* @param subject 邮件主题* @param content HTML内容* @param rscPath 图片路径* @param rscId 图片ID* @throws MessagingException*/public void sendInlinkResourceMail(String to, String subject, String content,String rscPath, String rscId) {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = null;try {helper = new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);helper.setFrom(from);FileSystemResource res = new FileSystemResource(new File(rscPath));helper.addInline(rscId, res);mailSender.send(message);} catch (MessagingException e) {e.printStackTrace();}}}
页面获取路径参数,ajax提交验证,返回成功!
为了安全起见,邮箱和验证码应该加密混淆下。这里就不具体讲了。
springboot绑定邮箱激活发邮件相关推荐
- phpmail通过qq发邮箱失败_PHP中利用PHPMailer配合QQ邮箱实现发邮件
phpmailer实现给网站用户发送邮件,WordPress好像禁用了mail()函数,也不能直接使用自带的发送邮件,以防止暴露IP PHPMailer的介绍: 可运行在任何平台之上 支持SMTP验证 ...
- 邮箱如何发邮件,邮箱如何清理发信,VIP邮箱靓号是什么?
邮箱如何发邮件,邮箱如何清理发信量,vip邮箱发信量有上限吗?关于上述问题,是我们在发邮件时常遇到的,今天小编给大家整理了相关资料,一起说说吧! 邮箱如何发邮件? 这个问题看你要发多少数量,登陆邮箱点 ...
- springboot mail插件/JavaMailSenderImpl 发邮件。163网易企业邮箱调用的坑。
前言 spring boot 2.0.0.RELEASE spring 5.0.4.RELEASE 网易企业邮箱 2019年版 jdk 1.8 发邮件的说明 按照加密方式方式,可以分为: 明文 TLS ...
- php 操作 腾讯企业邮箱设置发邮件,纯代码实现WordPress使用腾讯企业邮箱发送邮件...
为什么选择腾讯企业邮箱,其中有个非常优秀的功能,就是可以绑定在腾讯云购买的域名为邮箱的域名,这样子看起来邮箱会更加的正统一点.另一个就是可以直接绑定到QQ邮箱,相对来说平时读邮件的时候回显得更加方便. ...
- 使用matlab编程给qq或163邮箱等发邮件
平时在科研中经常会被导师要求使用matlab跑一些大程序,许多时候一个结果跑下来得花去好几个小时的时间,然后改改参数再继续跑,这样真的很让人不淡定啊,如果使用matlab编程让matlab跑出的让自己 ...
- python是什么邮箱_python发邮件详解,smtplib和email模块详解
在介绍具体的实现python发邮件的具体操作之前,我觉得有必要介绍下SMTP,更有助于理解python发邮件的实现原理.SMTP协议属于TCP/IP协议簇,即简单邮件传输协议,它是一组用于由源地址到目 ...
- 邮箱怎么发邮件,邮件发信量多少,职场新人怎么发汇报邮件呢?
在职场,工作汇报几乎是我们每天都要做的,而电子邮箱无疑是很好的汇报工具,写一封调理清晰的汇报邮件,也是职场的必备技能.下面,TOM VIP邮箱给大家做下分享吧~ 首先,在逻辑上需掌握几点精华 学会自上 ...
- 非QQ邮箱 无法发邮件给 QQ号@qq.com的邮箱 问题 解决方案
工作中遇到了一个问题: 使用 非QQ邮箱 进行发件时,发送给 QQ号@qq.com1 的邮件总是提示发件失败. 正当我百思不得其解的时候,同事给了我个解决方案: 把收件邮箱 @后面小写的"q ...
- uni-app学习笔记之163邮箱自动发邮件
前期准备 注册163邮箱 在邮箱设置中开启POP3/SMTP服务 新增授权密码(记得保存) 新建云函数目录mail163 安装 nodemailer 模块 在mail163目录右键->" ...
- Laravel5使用qq邮箱smtp发邮件配置及测试
第一步:.env中的MAIL配置: MAIL_DRIVER=smtp MAIL_HOST=smtp.qq-com MAIL_PORT=587 MAIL_USERNAME=xxxxxxxxx@qq-co ...
最新文章
- [转自脚本之家] Javascript cookie 详解
- python可以提高程序执行速度N倍你知道吗?
- big sur无法验证macos_网易邮箱出现重大失误,忘记续期HTTPS证书导致大量用户无法使用...
- 进击的UI---------------------UIStepper(加减)
- PHP动态多文件上传的具体代码
- 跟我学 Java 8 新特性之 Stream 流(四)并行流
- Javascript--File对象
- 计算机信息的编码教案,信息的编码教案信息的编码教案.doc
- java编程思想txt下载_《JAVA编程思想》之第5章-初始化和清理
- 发抖音上热门最佳时间 视频MD5修改器苹果手机
- 恒凯USB数据采集卡在国产麒麟操作系统下的应用(四)QT环境设置及数据采集实验
- 网络音视频下载小套路-dy、xmly
- 3d建模软件安装教程,游戏建模必备软件推荐(收藏)
- 机器学习笔试面试题目 二
- java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/View$OnUnhandledKeyEventListener
- C#的新特性(C#核心技术浅解)
- VMware卸载不干净导致无法重安
- 如何彻底卸载Oracle数据库
- ElasticSearch快速入门实战
- 对于‘用C#编写一个员工工资计算’问题的代码编写风格和结构设计考虑的比较【发现自己还是太弱,大家可以在评论区中提出我代码中的不足】