当前位置:网站首页>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);
}
}
边栏推荐
- Square array cyclic right shift
- QT 控件增加双击事件
- Cenos7 firewall command
- Sqlserver paging
- Idea reports an error "insufficient memory"
- 并发三大特性2-有序性
- Mt4/mql4 getting started to mastering EA tutorial lesson 8 - common functions of MQL language (VIII) - common time function
- Find elements that are not common to two arrays
- How to handle root password forgetting in MySQL
- Oracle gets the working day time between two dates
猜你喜欢

Bee framework, an ORM framework that can be learned in ten minutes --bee

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

Mt4/mql4 getting started to mastering EA tutorial lesson 3 - common functions of MQL language (III) - common functions of K-line value taking

It operation and maintenance knowledge map

Chapter VIII web project testing (the end of this chapter)

【Oracle 数据库】奶妈式教程 day12 字符函数

Enumerations, custom types, and swaggerignore in swagger

Concatenate the specified character at the end of a number in a string

歪门邪道之解决首屏图片加载闪烁问题

多点闹钟实例
随机推荐
面试突击59:一个表中可以有多个自增列吗?
Thoughts on the construction of data analysis platform for small and medium-sized enterprises (I)
C # read / write TXT file to listview
找出不是两个数组共有的元素
On Fresnel phenomenon
Oracle execution plan analysis
Record once · fluent file buffer
Bee framework, an ORM framework
Find elements that are not common to two arrays
歪门邪道之解决首屏图片加载闪烁问题
Three characteristics of concurrency 2-orderliness
Website sharing of program ape -- continuous update
Mysql5.7 master-slave mode reference
golang中使用swagger遇到的一些问题
同态加密的基本概念
MySQL index
SVN 提交子文件夹问题
It operation and maintenance knowledge map
C#读写txt文件到listview
C语言实现往MySQL插入和读取图片