java集成阿里云短信服务模版并发送短信

配置在这里插入图片描述

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

全部配置成功后可以获取到对应的模版code

pom.xml

        <dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.1.1</version></dependency><dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-green</artifactId><version>3.6.6</version></dependency>

AliSms

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;/*** 阿里云短信工具类* aliyun-java-sdk-core.jar*/
public class AliSms {private static final String tpl = "SMS_xxxxxxx";// 支付成功通知模版private static final String pay_success = "SMS_xxxxxxx";// 入驻审核通知模版private static final String settle_in = "SMS_xxxxxx";// 下单成功提醒(商户)模版private static final String merchant_remind = "SMS_xxxxx";// 短信签名private static final String sign = "配置的签名名称1";private static final String notice = "配置的签名名称2";// 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)private static final String accessKeyId = "xxxxxxxxxx";private static final String accessKeySecret = "xxxxxxxxxxxxx";public static void main(String[] args) {sendMsg("173xxxxxxxx", pay_success,"{\"no\":\"" + "544354134651215" + "\"}");}/*** 发送验证码* * @param phone*            手机号* @param code*            验证码*/public static CommonResponse sendCode(String phone, String code) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();// request.setProtocol(ProtocolType.HTTPS);request.setMethod(MethodType.POST);request.setDomain("dysmsapi.aliyuncs.com");request.setVersion("2024-04-02");request.setAction("SendSms");request.putQueryParameter("RegionId", "cn-hangzhou");request.putQueryParameter("SignName", sign);request.putQueryParameter("PhoneNumbers", phone);request.putQueryParameter("TemplateCode", tpl);request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());return response;} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return null;}/*** 订单支付发送短信通知* @param phoneNumbers* @param templateParam* @param type 1:支付成功通知模版 2:入驻审核通知 3:下单成功提醒(商户)* @return*/public static CommonResponse sendPaySuccessMsg(String phoneNumbers,String templateParam,String templateParam2,Integer type) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();// request.setProtocol(ProtocolType.HTTPS);request.setMethod(MethodType.POST);request.setDomain("dysmsapi.aliyuncs.com");request.setVersion("2024-04-02");request.setAction("SendSms");request.putQueryParameter("RegionId", "cn-hangzhou");request.putQueryParameter("SignName", notice);//TemplateParam为配置的参数名称if(type==1){request.putQueryParameter("TemplateCode", notice);request.putQueryParameter("TemplateParam", "{\"no\":\"" + templateParam + "\"}");}else if(type==2){request.putQueryParameter("TemplateCode", settle_in);request.putQueryParameter("TemplateParam", "{\"result\":\"" + templateParam + "\",\"date\":\""+templateParam2+"\"}");}else if(type==3){request.putQueryParameter("TemplateCode", merchant_remind);request.putQueryParameter("TemplateParam", "{\"no\":\"" + templateParam + "\"}");}request.putQueryParameter("PhoneNumbers", phoneNumbers);try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());return response;} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return null;}/*** 后台发送短信* * @param phoneNumbers*            手机号* @param templateCode* 			 短信模板           */public static CommonResponse sendMsg(String phoneNumbers,String templateCode,String templateParam) {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();// request.setProtocol(ProtocolType.HTTPS);request.setMethod(MethodType.POST);request.setDomain("dysmsapi.aliyuncs.com");request.setVersion("2024-04-02");request.setAction("SendSms");request.putQueryParameter("RegionId", "cn-hangzhou");request.putQueryParameter("SignName", notice);request.putQueryParameter("PhoneNumbers", phoneNumbers);request.putQueryParameter("TemplateCode", templateCode);request.putQueryParameter("TemplateParam", templateParam);try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());return response;} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}return null;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/597620.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++从入门到精通——类的作用域及类的实例化

类的作用域及类的实例化 前言一、类的作用域二、类的实例化引例类是对对象进行描述的示例 一个类可以实例化出多个对象示例 示例 前言 类的作用域是指类中定义的变量和方法的可见性和可访问性范围。在类的内部&#xff0c;所有成员&#xff08;包括属性和方法&#xff09;都具…

商业和投资

商业和投资 你敢想象吗&#xff1f;资本、资本家、投资人根本就不是靠分红赚钱的。瑞幸咖啡一年半亏了22亿&#xff0c;拼多多4年亏了140亿&#xff0c;京东12年亏了300亿&#xff0c;美团8年亏了1155亿&#xff0c;瑞幸咖啡创始人钱志亚身价却是40亿&#xff0c;拼多多创始人…

金三银四面试题(十五):Java基础问题(6)

这部分面试题多用于面试的热身运动&#xff0c;对很多找实习和准备毕业找工作的小伙伴至关重要。 HashMap与ConcurrentHashMap 都是key-value 形式的存储数据&#xff1b; HashMap 是线程不安全的&#xff0c;ConcurrentHashMap 是JUC 下的线程安全的&#xff1b; HashMap 底层…

计算机网络 实验指导 实验8

三层交换机的访问控制 1.实验拓扑图&#xff1a; 名称接口IP地址网关Switch AF0/1192.168.1.1/24F0/2172.1.1.1/24Switch BF0/1192.168.1.2/24F0/2172.2.2.1/24PC1172.1.1.2/24172.1.1.1PC2172.1.1.3/24172.1.1.1PC3172.2.2.2/24172.2.2.1PC4172.2.2.3/24172.2.2.1 2.实验目的…

设计模式总结-适配器模式

适配器模式 模式动机模式定义模式结构适配器模式实例与解析实例一&#xff1a;仿生机器人实例二&#xff1a;加密适配器 总结 模式动机 在软件开发中采用类似于电源适配器的设计和编码技巧被称为适配器模式。 通常情况下&#xff0c;客户端可以通过目标类的接口访问它所提供的…

CVPR 2024 | 拖拽P图又双叒升级了!DragNoise实现更快更准的拖拽编辑

前言 新加坡管理大学何盛烽团队联合华南师范大学在 CVPR 2024 上发表了工作《Drag Your Noise: Interactive Point-based Editing via Diffusion Semantic Propagation》。这一工作聚焦于利用扩散模型语义传播实现交互式点控制的图像编辑&#xff0c;只需点几个点&#xff0c;即…

【EI会议】2024年机械制造、航天工程技术与材料应用国际学术会议 (MMAETMA 2024)

2024 International Academic Conference on Mechanical Manufacturing, Aerospace Engineering Technology and Materials Application (MMAETMA 2024) 会议地点&#xff1a;长沙&#xff0c;中国 截稿时间&#xff1a;2024.4.30 网址&#xff1a;http://www.mmaetma.com/ …

llama2的python视角

1 调试代码 if __name__ __main__ :config ModelArgs(dim8, n_layers2, n_heads32, n_kv_heads32, vocab_size32000, hidden_dimNone, multiple_of256, norm_eps1e-05, max_seq_len3, dropout0.0)model Transformer(config)input_tokens torch.randint(0, 32000, (1, 3)) …

Windows11安装MySql-8.0.36安装详细教程(保姆级教程)

之前一直用的mysql5.7&#xff0c;最近导入一个项目一直报错&#xff0c;经查阅发现数据库mysql版本太老&#xff0c;今天特地重头下载安装配置一下&#xff0c;做个记录供大家参考。 下载安装包&#xff1a; 下载地址&#xff1a;https://dev.mysql.com/downloads/ 进入后选…

C语言进阶课程学习记录-第24课 - #pragma 使用分析

C语言进阶课程学习记录-第24课 - #pragma 使用分析 #pragma实验-#pragma messagecmd窗口运行 实验-pragma oncebcc编译报错gcc编译成功global.h代码优化 #pragma pack实验BCC编译器输出 小结 本文学习自狄泰软件学院 唐佐林老师的 C语言进阶课程&#xff0c;图片全部来源于课程…

【办公类-48-02】20240407每月电子屏台账汇总成docx-2(腾讯文档xlsx导入docx,每页20条)

作品展示 背景需求&#xff1a; 安全主任再次催交台账 一分园老师发的是链接版——这是我原来制作的在线共享填写“腾讯文档”。但是感觉手机竖版填写起来不方便&#xff0c;&#xff08;表格是横版的&#xff0c;要向右滑动点击格子&#xff0c;填起来容易错行&#xff09;&a…

实现 3D 数字时钟

3D 数字时钟实现 效果展示 CSS / JavaScript 知识点 box-shadow 属性运用&#xff0c;实现 3D 效果的数字时钟transform 属性灵活运用&#xff0c;实现数值时钟上的数字部分JavaScript Date()对象的运用 页面整体布局 <div class"clock"><!-- 时钟仪表盘…