首先要有个阿里云账号,可到阿里云登录页注册并登录。
登录后访问短信服务快速学习和测试,其中有逐步介绍如何发送短信:
新增资质
新增资质相当于进行实名认证,资质是申请签名的实名化信息。
申请签名
签名是短信中能代表发送者属性的字段。一般就是公司名字。发送短信时,签名会附加到短信开头。
申请模板
短信是基于模板的,模板中带有变量占位符,变量格式如${code}
,仅支持 1 个变量。
例如:您的验证码为${code}
,该验证码 5 分钟内有效,请勿泄露于他人。
系统设置
配置接收发送短信的结果等,不需要的话就不用配置。
发送短信
先绑定测试手机号码,然后选择自己新建的签名/模板,点击调用API发送短信
按钮后会跳转到示例代码。这里就直接选择测试签名/模板了。
下面为给出的示例代码:
| |
| package com.demo.sms; |
| |
| import com.aliyun.tea.*; |
| |
| public class Sample { |
| |
| |
| |
| |
| |
| |
| |
| |
| public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception { |
| |
| |
| com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() |
| |
| .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")) |
| |
| .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); |
| |
| config.endpoint = "dysmsapi.aliyuncs.com"; |
| return new com.aliyun.dysmsapi20170525.Client(config); |
| } |
| |
| public static void main(String[] args_) throws Exception { |
| java.util.List<String> args = java.util.Arrays.asList(args_); |
| com.aliyun.dysmsapi20170525.Client client = Sample.createClient(); |
| com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest() |
| .setSignName("阿里云短信测试") |
| .setTemplateCode("SMS_154950909") |
| .setPhoneNumbers("183xxxxxxxx") |
| .setTemplateParam("{\"code\":\"1234\"}"); |
| com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); |
| try { |
| |
| client.sendSmsWithOptions(sendSmsRequest, runtime); |
| } catch (TeaException error) { |
| |
| |
| System.out.println(error.getMessage()); |
| |
| System.out.println(error.getData().get("Recommend")); |
| com.aliyun.teautil.Common.assertAsString(error.message); |
| } catch (Exception _error) { |
| TeaException error = new TeaException(_error.getMessage(), _error); |
| |
| |
| System.out.println(error.getMessage()); |
| |
| System.out.println(error.getData().get("Recommend")); |
| com.aliyun.teautil.Common.assertAsString(error.message); |
| } |
| } |
| } |
代码依赖于阿里云短信 SDK:
| <dependency> |
| <groupId>com.aliyun</groupId> |
| <artifactId>dysmsapi20170525</artifactId> |
| <version>3.0.0</version> |
| </dependency> |
核心步骤为:
- 创建配置
- 设置签名、模板、手机号和模板中要用到的参数
- 发送短信
| |
| public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception { |
| com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() |
| .setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")) |
| .setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); |
| config.endpoint = "dysmsapi.aliyuncs.com"; |
| return new com.aliyun.dysmsapi20170525.Client(config); |
| } |
| |
| com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest() |
| .setSignName("阿里云短信测试") |
| .setTemplateCode("SMS_154950909") |
| .setPhoneNumbers("183xxxxxxxx") |
| .setTemplateParam("{\"code\":\"1234\"}"); |
| |
| com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); |
| client.sendSmsWithOptions(sendSmsRequest, runtime); |
注意第一步从环境中获取ALIBABA_CLOUD_ACCESS_KEY_ID
、ALIBABA_CLOUD_ACCESS_KEY_SECRET
变量来构建Config
。这两个值(AccessKey ID 和 AccessKey Secret)是访问阿里云 API 的密钥:
另:参考https://api.aliyun.com/product/Dysmsapi
根据需要调整Config.endpoint
。
参考:使用阿里云的短信服务发送短信
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!