当前位置:网站首页>Alibaba cloud ~ simply send SMS
Alibaba cloud ~ simply send SMS
2022-06-22 08:17:00 【Hanxiaozhi】
Simple three steps : Easily send text messages
One : Alibaba cloud
1. Create an alicloud account
2. Create a signature , Create a template
3. Create a secret key ( Use your own secret key id, And the password )
Two : Code
1. Introduce two dependencies
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dysmsapi -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
3、 ... and : Tool class ( Sign the inside , Template No , And change the secret key to your own , perform main Method test , That's all right. )
package com.itheima.utils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.dysmsapi.transform.v20170525.SendSmsResponseUnmarshaller;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
/**
- Created on 17/6/7.
- SMS API Of the product DEMO Program , The project includes a SmsDemo class , Directly through
- perform main Function to experience SMS products API function ( Only need to AK Replace with cloud communication - The functions of SMS products AK that will do )
- The project depends on 2 individual jar package ( Stored in the libs Under the table of contents )
- 1:aliyun-java-sdk-core.jar
- 2:aliyun-java-sdk-dysmsapi.jar
- remarks :Demo The project code adopts UTF-8
- Please do not refer to this for international SMS sending DEMO
*/
public class SmsDemo {
// The product name : Cloud messaging API product , Developers don't need to replace
static final String product = "Dysmsapi";
// Product domain name , Developers don't need to replace
static final String domain = "dysmsapi.aliyuncs.com";
// TODO It needs to be replaced by the developer's own AK( Search on Alibaba cloud's access console )
static final String accessKeyId = " Secret key id";
static final String accessKeySecret = " password ";
public static SendSmsResponse sendSms(String phone,String code) throws ClientException {
// The timeout can be adjusted by self
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
// initialization acsClient, Temporary does not support region turn
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
// Assemble request object - Please refer to the console for details - Part of the document
SendSmsRequest request = new SendSmsRequest();
// Required : Mobile number to be sent
request.setPhoneNumbers(phone);
// Required : SMS signature - Can be found in SMS console
request.setSignName(" Taiping Insurance ");
// Required : SMS template - Can be found in SMS console
request.setTemplateCode("SMS_184206106");
// Optional : Variable substitution in templates JSON strand , If the template content is " dear ${name}, Your verification code is ${code}" when , The value here is
request.setTemplateParam("{\"code\":\""+code+"\"}");
// optional - Uplink SMS extension code ( Please ignore this field if there is no special requirement )
//request.setSmsUpExtendCode("90997");
// Optional :outId To extend the fields provided to the business side , Finally, this value will be brought back to the caller in the SMS receipt message
request.setOutId("yourOutId");
//hint An exception may be thrown here , Be careful catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
// The test method
public static void main(String[] args) throws ClientException, InterruptedException {
// Verification Code
String ZiMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
String result = "";
Random random = new Random();
for (int i = 0; i < 6; i++) {
int index = random.nextInt(ZiMu.length());
char c = ZiMu.charAt(index);
result += c;
}
// texting
SendSmsResponse response = sendSms(" cell-phone number ", result);
Thread.sleep(3000L);
}
}
边栏推荐
- Failed to access the ES installed on Tencent ECs, but the solution to accessing the ES successfully on the server
- 成为一名卓越云架构师要具备的五项技能
- C language implements inserting and reading pictures into MySQL
- Use of keepalived high availability cluster
- 0基础自学stm32(野火)——什么是寄存器?
- MySQL queries data within one hour
- Sqlserver paging
- Using KDJ metrics on MT4
- SQL triggers
- 邮件巨头曝严重漏洞,用户数据被窃取
猜你喜欢

Type of sub database and sub table

Learn data warehouse together - Zero

开展有效的创客教育课程与活动

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - common functions of MQL language

年度十强!赛宁网安再次入围《中国数字安全百强报告》

PostgreSQL source code (56) extensible type analysis expandedobject/expandedrecord

Enumerations, custom types, and swaggerignore in swagger

【Oracle 数据库】奶妈式教程 day11 数值函数

【Oracle 数据库】奶妈式教程 day14 转换函数

Three concurrent features 1- visibility
随机推荐
Design skills of common table structure in database design
每周推荐短视频:什么是“计算世界”?
JSON使用示例
Collections and arrays
Oracle execution plan analysis
Failed to access the ES installed on Tencent ECs, but the solution to accessing the ES successfully on the server
swagger中的枚举、自定义类型和swaggerignore
计算水费问题
Website sharing of program ape -- continuous update
MySQL backup - mysqldump
PostgreSQL common commands and SQL -- continuous update
0基础自学stm32(野火)——什么是寄存器?
QT error prompt 1:invalid use of incomplete type '***‘
Define the data source of hikaricp connection pool for bee
Any to Any 实时变声的实现与落地丨RTC Dev Meetup
Why redis is so fast:
Mysql5.7 master-slave mode reference
MySQL index
Qt 错误提示1: invalid use of incomplete type ‘***‘
Object to string pit