Tencent cloud Java integrated SMS verification code

First, you have to register on the cloud and Tencent launched SMS service.
Step 1: Create SMS app


Step Two: Create a text signature


Step Three: Create a text template in a second step after the success of


Here on the setup is complete, the next Java code, by Tencent interface to the java sdk
Details for: official documents.
First, before implementation needs to import a jar package: qcloudsms-1.0.6.jar.
maven words, dependent as follows:

<dependency>
<the groupId> com.github.qcloudsms </ the groupId>
<the artifactId> qcloudsms </ the artifactId>
<Version> 1.0.6 </ Version>
</ dependency>
. 1
2
. 3
. 4
. 5
turned finish jar package can start writing a.
Send a verification code tools:

import java.io.IOException;

import org.json.JSONException;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;

/ **
* Tencent cloud SMS codes tools
* @author QQ
* /
public class SendMessageUtil {

// SMS application AppID SDK
int AppID = 1400009099; // at the beginning of 1400

// AppKey SMS app SDK
String AppKey = "9ff91d87c2cd7cd0ea762f141975d1df37481d48700d70ac37470aefc60f9bad";

// SMS template ID, you need to apply in a text message applications
int templateId = 7839; // NOTE: Here's just one example of template ID`7839` real need to apply the template ID in the message console

// Signature
String smsSign = "Tencent cloud"; // NOTE: Here's signature "Tencent cloud" is just one example, the real need to apply for signature in the message console, in addition to the signature parameters used are `` signature content, rather than ` signature ID`

// code
String str = "000000";

/ **
* send SMS verification code
* @param phoneNum which needs to be sent to a mobile phone number
* @return verification code, if it is 000000, then failed to send
* /
public String sendMessage (phoneNum String) {
the try {
// then the 6-digit codes assigned
String strTemp = (int) (( Math.random () * 9 + 1) * 100000) + "";

number // array elements and specific the number of variables must match the template
// instance you need to fill in the template code and valid time, {1}, {2}
// here that the parameters you should fill two
String [] params = {strTemp, "5 "};
SmsSingleSender ssender = new new SmsSingleSender (AppID, AppKey);

// signature parameter is not provided or is empty, to send text messages use the default signing
SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNum, templateId, params, smsSign, "", "");

System.out.println("result = " + result);

// sent successfully to the assigned codes
IF (result.result == 0) {
STR = strTemp;
}
} the catch (HTTPException E1) {
// the HTTP error response code
e1.printStackTrace ();
} the catch (JSONException E2) {
// json parse error
e2.printStackTrace ();
} the catch (IOException E3) {
// network IO error
e3.printStackTrace ();
}
return STR;
}

}

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 is
62 is
63 is
64
65
66
67
68
69
calls tools:

/ **
* test text verification code
* /
public void text () {
SendMessageUtil sendMessageUtil new new SendMessageUtil = ();
sendMessageUtil.sendMessage ( "18,712,346,789");
}
. 1
2
. 3
. 4
. 5
. 6
. 7
following a successful transmission, result is output:

result = {"result":0,"errmsg":"OK","ext":"","sid":"8:vBelUMJHIg3jVC6ZW2U20190710","fee":1}
1
效果为:
————————————————

Guess you like

Origin www.cnblogs.com/hyhy904/p/11414311.html