Interface documentation
step
- Configure SMS signature
- Apply for SMS template
- Download relevant sdk packages
1, Configure signature
-Log in to alicloud, click products and services, and find SMS services in cloud communications.
2, Configure SMS template
3, Download relevant sdk files
sdk download address entry:
https://help.aliyun.com/document_detail/55359.html?spm=5176.11065259.1996646101.searchclickresult.d0a31a71oq0HYi
Directory list of sdk
api_demo (SMS service API interface calls demo project) - provides a demo of relevant SMS, which can be used directly by modifying relevant properties.
api_sdk (SDK on which the short message service API interface depends) - provides relevant jar packages and maven coordinates.
msg_demo (DEMO of SMS receipt message)
msg_sdk (SDK for SMS receipt message)
4, Custom SMS method
1. Introduce relevant jar packages or maven
<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><code class="language-xml"><span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"><</span>dependency</span><span style="color:#999999">></span></span> <span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"><</span>groupId</span><span style="color:#999999">></span></span>com.aliyun<span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"></</span>groupId</span><span style="color:#999999">></span></span> <span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"><</span>artifactId</span><span style="color:#999999">></span></span>aliyun-java-sdk-core<span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"></</span>artifactId</span><span style="color:#999999">></span></span> <span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"><</span>version</span><span style="color:#999999">></span></span>4.0.6<span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"></</span>version</span><span style="color:#999999">></span></span> <span style="color:#969896 "> <! -- Note: if an error is prompted, upgrade the basic package version first. If it cannot be solved, contact technical support -- ></span> <span style="color:#cc6666"><span style="color:#cc6666"><span style="color:#999999"></</span>dependency</span><span style="color:#999999">></span></span> </code></span></span></span>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2. Concrete implementation
<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><code class="language-java"><span style="color:#c678dd">public</span> <span style="color:#c678dd">class</span> SendSms <span style="color:#999999">{</span> <span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">sendAliyun</span><span style="color:#999999">(</span>String merchantName<span style="color:#999999">,</span>String phone<span style="color:#999999">,</span>String money<span style="color:#999999">)</span> <span style="color:#999999">{</span> <span style="color:#c678dd">try</span> <span style="color:#999999">{</span> <span style="color:#5c6370 "> / / set timeout - self adjustable</span> System<span style="color:#999999">.</span><span style="color:#61aeee">setProperty</span><span style="color:#999999">(</span><span style="color:#669900">"sun.net.client.defaultConnectTimeout"</span><span style="color:#999999">,</span> <span style="color:#669900">"10000"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> System<span style="color:#999999">.</span><span style="color:#61aeee">setProperty</span><span style="color:#999999">(</span><span style="color:#669900">"sun.net.client.defaultReadTimeout"</span><span style="color:#999999">,</span> <span style="color:#669900">"10000"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / several parameters required to initialize ascClient</span> <span style="color:#c678dd">final</span> String product <span style="color:#669900">=</span> <span style="color:#669900">"Dysmsapi"</span><span style="color:#999999">;</span><span style="color:#5c6370 "> / / SMS API product name (SMS product name is fixed and does not need to be modified)</span> <span style="color:#c678dd">final</span> String domain <span style="color:#669900">=</span> <span style="color:#669900">"dysmsapi.aliyuncs.com"</span><span style="color:#999999">;</span><span style="color:#5c6370 "> / / SMS API product domain name (the interface address is fixed and does not need to be modified)</span> <span style="color:#5c6370 "> / / replace with your AK</span> <span style="color:#c678dd">final</span> String accessKeyId <span style="color:#669900">=</span> <span style="color:#669900">"LtAIY25ciEK660jh"</span><span style="color:#999999">;</span><span style="color:#5c6370 "> / / for your accessKeyId, refer to step 2 of this document</span> <span style="color:#c678dd">final</span> String accessKeySecret <span style="color:#669900">=</span> <span style="color:#669900">"NI3WlwvUU5vPD4BUszHaMNsWBGhF8S"</span><span style="color:#999999">;</span><span style="color:#5c6370 "> / / for your accessKeySecret, refer to step 2 of this document</span> <span style="color:#5c6370 "> / / initialize ascClient. Multiple region s are not supported temporarily (please do not modify)</span> IClientProfile profile <span style="color:#669900">=</span> DefaultProfile<span style="color:#999999">.</span><span style="color:#61aeee">getProfile</span><span style="color:#999999">(</span><span style="color:#669900">"cn-hangzhou"</span><span style="color:#999999">,</span> accessKeyId<span style="color:#999999">,</span> accessKeySecret<span style="color:#999999">)</span><span style="color:#999999">;</span> DefaultProfile<span style="color:#999999">.</span><span style="color:#61aeee">addEndpoint</span><span style="color:#999999">(</span><span style="color:#669900">"cn-hangzhou"</span><span style="color:#999999">,</span> <span style="color:#669900">"cn-hangzhou"</span><span style="color:#999999">,</span> product<span style="color:#999999">,</span> domain<span style="color:#999999">)</span><span style="color:#999999">;</span> IAcsClient acsClient <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> DefaultAcsClient<span style="color:#999999">(</span>profile<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / assembly request object</span> SendSmsRequest request <span style="color:#669900">=</span> <span style="color:#c678dd">new</span> SendSmsRequest<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / submit using post</span> request<span style="color:#999999">.</span><span style="color:#61aeee">setMethod</span><span style="color:#999999">(</span>MethodType<span style="color:#999999">.</span>POST<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / required: mobile phone number to be sent. It supports batch calls separated by commas. The maximum batch number is 1000 mobile phone numbers. The timeliness of batch calls is slightly delayed compared with a single call. It is recommended to use a single call for verification code type SMS</span> request<span style="color:#999999">.</span><span style="color:#61aeee">setPhoneNumbers</span><span style="color:#999999">(</span>phone<span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / required: SMS signature - can be found in SMS console</span> request<span style="color:#999999">.</span><span style="color:#61aeee">setSignName</span><span style="color:#999999">(</span><span style="color:#669900">"xxx"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / required: SMS template - can be found in SMS console</span> request<span style="color:#999999">.</span><span style="color:#61aeee">setTemplateCode</span><span style="color:#999999">(</span><span style="color:#669900">"SMS_178823227"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#5c6370 "> / / optional: the variables in the template replace the JSON string. For example, when the template content is" Dear ${name}, and your verification code is ${code} ", the value here is</span> <span style="color:#5c6370 "> / / friendly note: if line breaks are required in JSON, please refer to the requirements of the standard JSON protocol for line breaks. For example, if the SMS content contains \ r\n, it needs to be expressed as \ \ r\n in JSON, otherwise JSON will fail to parse on the server</span> SimpleDateFormat format<span style="color:#669900">=</span><span style="color:#c678dd">new</span> SimpleDateFormat<span style="color:#999999">(</span><span style="color:#669900">"yyyy year MM month dd day HH:mm"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> String time<span style="color:#669900">=</span>format<span style="color:#999999">.</span><span style="color:#61aeee">format</span><span style="color:#999999">(</span><span style="color:#c678dd">new</span> Date<span style="color:#999999">(</span><span style="color:#999999">)</span><span style="color:#999999">)</span><span style="color:#999999">;</span> request<span style="color:#999999">.</span><span style="color:#61aeee">setTemplateParam</span><span style="color:#999999">(</span><span style="color:#669900">"{\"name\":\""</span><span style="color:#669900">+</span>merchantName<span style="color:#669900">+</span><span style="color:#669900">"\", \"time\":\""</span><span style="color:#669900">+</span>time<span style="color:#669900">+</span><span style="color:#669900">"\",\"money\":\""</span><span style="color:#669900">+</span>money<span style="color:#669900">+</span><span style="color:#669900">"\"}"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> </code></span></span></span>
< span class = "token comment" > / / the request fails, and a ClientException will be thrown here. < / span > sendsmsresponse sendsmsresponse < span class = "token operator" > = < / span > acsclient < span class = "token pulse" >. < / span > < span class = "token function" > getacsresponse < / span > < span class = "token pulse" > (< / span > request < span class = "token pulse" >) </span><span class="token punctuation">;</ span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sendSmsResponse<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> null <span class="token operator" >&&</span> sendSmsResponse<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"OK" < / span > < span class = "token negotiation" >) < / span > < span class = "token negotiation" >) < / span > < span class = "token negotiation" > {<! --- > < / span > < span class = "token comment" > / / the request is successful < / span > system < span class = "token negotiation" >. < / span > out < span class = "token negotiation" >. < / span > < span class = "token function" > println < / span > < span class= "Token impulse" > (< / span > < span class = "token string" > > SMS sent successfully!!!, balance is: "< / span > < span class =" token operator "> + < / span > money < span class =" token operator "> + < / span > < span class =" token string ">", bizid: "< / span > < span class =" token operator "> + < / span > sendsmsresponse < span class =" token impulse ">. < / span > < span class =" token function ">getBizId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">",Code:"</span><span class="token operator">+</span>sendSmsResponse<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation"> (</span><span class="token punctuation">)</span><span class="token operator">+</span> <span class="token string">",Message:"</span><span class="token operator">+</span>sendSmsResponse<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">) </span><span class="token operator">+</span><span class="token string">",RequestId:"</span><span class="token operator">+</span>sendSmsResponse<span class="token punctuation">.</span><span class="token function">getRequestId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">) </span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation"> {<!-- --></span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
3. Testing
<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><code class="language-java"><span style="color:#c678dd">public</span> <span style="color:#c678dd">static</span> <span style="color:#c678dd">void</span> <span style="color:#61aeee">main</span><span style="color:#999999">(</span>String<span style="color:#999999">[</span><span style="color:#999999">]</span> args<span style="color:#999999">)</span> <span style="color:#999999">{</span> SendSms<span style="color:#999999">.</span><span style="color:#61aeee">send</span><span style="color:#999999">(</span><span style="color:#669900">"xxx"</span><span style="color:#999999">,</span><span style="color:#669900">"15279206000"</span><span style="color:#999999">,</span> <span style="color:#669900">"99.99"</span><span style="color:#999999">)</span><span style="color:#999999">;</span> <span style="color:#999999">}</span> </code></span></span></span>
- 1
- 2
- 3
4. Effect
<span style="color:rgba(0, 0, 0, 0.75)"><span style="background-color:#ffffff"><span style="color:#000000"><code class="language-java">[xxx]Warm tip: Dear xxx Customer, as of<span style="color:#98c379">2020</span>year<span style="color:#98c379">12</span>month<span style="color:#98c379">04</span>day <span style="color:#98c379">14</span><span style="color:#669900">:</span><span style="color:#98c379">37</span>,Your charging account balance is<span style="color:#98c379 "> 99.99 < / span > yuan. Please pay the fee in time to avoid affecting your normal use when the balance is insufficient. </code></span></span></span>
- 1
Writing is not easy. Now that you're here, you might as well pay attention and praise!!!
Choosing the right platform is the most important thing for ECs to carry business!Judging from the current pattern of the domestic cloud computing market, the top three domestic cloud computing are Alibaba cloud, Tencent cloud and Huawei cloud. Alibaba cloud and Tencent cloud, as enterprises backed by the Internet platform, prefer B-end users; Huawei and, as traditional communication giants, prefer the G-end.
Of course, how to select a server model? Here is a document summary in detail. The document address is:
If it is a high concurrency and high IO business scenario, it is necessary to determine the server specification to optimize the performance of business applications. Refer to the official document:
Alibaba cloud server specifications: ECS instance family - ECS specification
Tencent cloud server specification: CVM instance family - ECS specification