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

SpringBoot项目(验证码整合)——springboot整合email springboot整合阿里云短信服务

目录

  • 引出
  • springboot整合email
    • 配置邮箱
    • 导入依赖
    • application.yml配置
    • email业务类
    • 测试类
  • springboot整合阿里云短信服务
    • 申请阿里云短信服务
    • 测试短信服务
    • 获取阿里云的accessKey
    • springboot整合阿里云短信
      • 导包
      • 工具类
  • 总结

引出


1.springboot整合email,qq邮箱,特点免费;
2.springboot整合阿里短信服务,100条免费;
3.后续应用,可以用在登陆业务上,比如邮箱登陆,短信登陆;

springboot整合email

配置邮箱

登录邮箱服务器: 登录QQ邮箱

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

后面要用到这个授权码key

在这里插入图片描述

导入依赖

<!--        qq邮箱--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>

application.yml配置

在这里插入图片描述

在这里插入图片描述

email业务类

接口

package com.tianju.auth.service;public interface IEmailService {/*** 发送右键* @param to 邮件接收方* @param subject 邮件主题* @param content 邮件内容*/void sendEmail(String to,String subject,String content);
}

实现

package com.tianju.auth.service.impl;import com.tianju.auth.service.IEmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.Date;@Service
@Slf4j
public class EmailServiceImpl implements IEmailService {@Value("${spring.mail.username}")private String from;@Resourceprivate JavaMailSender javaMailSender;@Overridepublic void sendEmail(String to, String subject, String content) {SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject(subject);mailMessage.setTo(to);mailMessage.setText(content);mailMessage.setSentDate(new Date());mailMessage.setFrom(from);javaMailSender.send(mailMessage);log.debug("在{}发送一条邮件{}给{}",mailMessage.getSentDate(),mailMessage.getText(),mailMessage.getTo());}
}

测试类

package com.tianju.auth.service.impl;import com.tianju.auth.service.IEmailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.xml.ws.soap.Addressing;import java.util.UUID;import static org.junit.Assert.*;@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class EmailServiceImplTest {@Autowiredprivate IEmailService emailService;@Testpublic void sendEmail() {emailService.sendEmail("xxxx@qq.com", "我是老王,我在测试代码", UUID.randomUUID().toString());}
}

springboot整合阿里云短信服务

申请阿里云短信服务

短信服务 (aliyun.com)

在这里插入图片描述

测试短信服务

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

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

在这里插入图片描述

进行测试

在这里插入图片描述

在这里插入图片描述

调用结果

在这里插入图片描述

成功接收短信

在这里插入图片描述

获取阿里云的accessKey

在这里插入图片描述

在这里插入图片描述

springboot整合阿里云短信

导包

        <!--        阿里云短信验证码相关包--><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.3</version></dependency>

工具类

package com.tianju.auth.util;import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import net.minidev.json.JSONObject;import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class SMSUtil {private static String AccessIdKey = "获取的keyID";private static String AccessKeySecret = "获取的KeySecret";public static void send(String tel,String code) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",AccessIdKey, //AccessIdKeyAccessKeySecret); //AccessKey SecretIAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);//下面这3个不要改动request.setSysDomain("dysmsapi.aliyuncs.com");request.setSysVersion("2017-05-25");request.setSysAction("SendSms");//接收短信的手机号码request.putQueryParameter("PhoneNumbers",tel);//此处写电话号码//短信签名名称request.putQueryParameter("SignName","阿里云短信测试");//短信模板IDrequest.putQueryParameter("TemplateCode","SMS_154950909");//短信模板变量对应的实际值 ${code} 中的值Map<String,String> param = new HashMap<>(2);param.put("code", String.valueOf(code)); //写入的短信内容,验证码request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}}public static void main(String[] args) {int i = new Random().nextInt(80000) + 10000;System.out.println(i); // 35054SMSUtil.send("xxxx", "392712");}}

在这里插入图片描述


总结

1.springboot整合email,qq邮箱,特点免费;
2.springboot整合阿里短信服务,100条免费;
3.后续应用,可以用在登陆业务上,比如邮箱登陆,短信登陆;

http://www.lryc.cn/news/120537.html

相关文章:

  • 缓存穿透,击穿,雪崩之间的区别与联系
  • Vue项目npm run dev 启动报错TypeError: Cannot read property ‘upgrade‘ of undefined
  • dji uav建图导航系列(二)导航
  • 24.Netty源码之合理管理堆内存
  • 如何自学(黑客)网络安全
  • 【vue】vue基础知识
  • 第一百一十一回 如何实现屏幕适配
  • 免费实用的日记应用:Day One for Mac中文版
  • HCIP的BGP基础实验
  • centos7编译安装升级python3.11
  • win10安装mysql和c++读取调用举例
  • 计算机竞赛 opencv python 深度学习垃圾图像分类系统
  • 通讯协议037——全网独有的OPC HDA知识一之聚合(六)实际时间最小值
  • 【Freertos基础入门】freertos任务的优先级
  • 【报错】ModuleNotFoundError: No module named ‘websocket‘
  • [Leetcode] [Tutorial] 多维动态规划
  • C语言 二级指针和多级指针
  • 新机器到了要做的事情
  • 个人开发中常见单词拼错错误纠正
  • vb+sql汽车配件管理系统设计与实现
  • Spring Boot+Mybatis实现增删改查接口开发+测试(超详细建议收藏)
  • winform 使用CommonOpenFileDialog选择文件夹或文件
  • EXPLAIN使用分析
  • 布局性能优化:安卓开发者不可错过的性能优化技巧
  • Python 中的机器学习简介:多项式回归
  • docker 容器中执行命令出现错误: 13: Permission denied
  • JavaWeb学习|JavaBean;MVC三层架构;Filter;Listener
  • arx 外部参照文件(XREF)的添加、删除、卸载和重载_objectarx
  • 【博客699】docker daemon预置iptables剖析
  • Golang 中的交叉编译详解