热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

发送短信验证码和短信通知

引入依赖com.aliyunaliyun-java-sdk-c

引入依赖

<dependency><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-coreartifactId><version>4.5.3version>dependency><dependency><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-dysmsapiartifactId><version>1.1.0version>dependency><dependency><groupId>com.aliyungroupId><artifactId>dysmsapi20170525artifactId><version>2.0.1version>dependency><dependency><groupId>com.aliyungroupId><artifactId>tea-openapiartifactId><version>0.0.10version>dependency><dependency><groupId>com.aliyungroupId><artifactId>tea-consoleartifactId><version>0.0.1version>dependency><dependency><groupId>com.aliyungroupId><artifactId>tea-utilartifactId><version>0.2.10version>dependency><dependency><groupId>com.aliyungroupId><artifactId>teaartifactId><version>[1.0.3, 2.0.0)version>dependency>

SmsUtils
发送短信工具类

package com.hxecm.util;import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.*;
import com.hxecm.dto.AlarmMsgDto;import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;/*** &#64;Auther: Allen* &#64;Date: 2021/2/23 13&#xff1a;42* &#64;Description: 短信发送*/
public class SmsUtils {/*** 阿里云服务参数*/private static final String ACCESS_KEY_ID &#61; "ACCESS_KEY_ID";private static final String ACCESS_KEY_SECRET &#61; "ACCESS_KEY_SECRET";/** 通用签名 */private static final String Sign_Name &#61; "Sign_Name";/** 验证码模板 */private static final String Ver_Template_Code &#61; "Ver_Template_Code";/** 短信通知模板 */private static final String Notice_Template_Code &#61; "Notice_Template_Code";/*** common - 使用AK&SK初始化账号Client** &#64;param accessKeyId* &#64;param accessKeySecret* &#64;return Client* &#64;throws Exception*/public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {Config config &#61; new Config()// 您的AccessKey ID.setAccessKeyId(accessKeyId)// 您的AccessKey Secret.setAccessKeySecret(accessKeySecret);// 访问的域名config.endpoint &#61; "dysmsapi.aliyuncs.com";return new com.aliyun.dysmsapi20170525.Client(config);}// ######################################################// 发送手机验证码验证// ######################################################/*** 随机生成验证码*/private static int newCode;public static int getNewCode() {return newCode;}/*** 每次调用生成一次四位数的随机数验证码*/public static void setNewCode() {newCode &#61; (int) ((Math.random() * 9 &#43; 1) * 1000);}/*** 发送短信验证码** &#64;return* &#64;throws Exception*/public static SendSmsResponse sendSMS(String tel,String smsCode) throws Exception {com.aliyun.dysmsapi20170525.Client client &#61; SmsUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);SendSmsRequest sendSmsRequest &#61; new SendSmsRequest().setPhoneNumbers(tel).setSignName(Sign_Name).setTemplateCode(Ver_Template_Code).setTemplateParam("{\"code\":\"" &#43; smsCode &#43; "\"}");SendSmsResponse resp &#61; client.sendSms(sendSmsRequest);return resp;}/*** 获取随机验证码* &#64;return*/public static String getSmsCode(){//随机验证码setNewCode();String smsCode &#61; Integer.toString(getNewCode());return smsCode;}// ######################################################// 发送手机短信通知// ######################################################public static SendSmsResponse sendSMSNotification(String phone, AlarmMsgDto alarmMsgDto) throws Exception {com.aliyun.dysmsapi20170525.Client client &#61; SmsUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);SendSmsRequest sendSmsRequest &#61; new SendSmsRequest().setPhoneNumbers(phone)//phone可为多个&#xff0c;中间用","连接.setSignName(Sign_Name).setTemplateCode(Notice_Template_Code).setTemplateParam("{\"context\":\"context\"}");SendSmsResponse resp &#61; client.sendSms(sendSmsRequest);return resp;}}

SendSmsResponse ,是否发送成功检验

if (resp.getBody().getCode() !&#61; null && resp.getBody().getCode().equals("OK")) {log.info("SUCCESS");} else {log.info("ERROR");}


推荐阅读
author-avatar
行者师兄2502861743
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有