(This is Aliyun inside the ha, not Taobao inside the two or is not the same; Aliyun background message service inside)
Text messaging is useful in many projects, to sum up, the next direct copy code.
about Accesskeyid and key can view the API document with a jump link to view.
Package com.alidayu.test;
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.UUID;
/** * Created on 17/6/7. * SMS API Product Demo program, the project contains a Smsdemo class, directly through the * implementation of the main function can be experienced SMS Product API function (only need to replace AK into the open cloud Communication-SMS product function AK can) * Project relies on 2 jar packages ( Deposited in the Libs directory of the project) * 1:aliyun-java-sdk-core.jar * 2:aliyun-java-sdk-dysmsapi.jar * * Note: Demo Project Code adoptedUTF-8/public class Smsdemo {//Product Name: Cloud Communication SMS API products, developers do not need to replace static final String Product = "Dysmsapi";
Product domain name, developers do not need to replace static final String domain = "dysmsapi.aliyuncs.com";
TODO here needs to be replaced by the developer's own AK (looking for the Aliyun Access console) static final String Accesskeyid = "Youraccesskeyid";
Static final String Accesskeysecret = "Youraccesskeysecret"; public static Sendsmsresponse Sendsms () throws Clientexception {//Can self-adjust timeout system.setproperty ("Sun.net.
Client.defaultconnecttimeout "," 10000 ");
System.setproperty ("Sun.net.client.defaultReadTimeout", "10000"); Initialize acsclient, temporarily do not support region iclientprofile profile = Defaultprofile.getprofile ("Cn-hangzhou", Accesskeyid, Accesske
Ysecret);
Defaultprofile.addendpoint ("Cn-hangzhou", "Cn-hangzhou", product, domain);
Iacsclient acsclient = new Defaultacsclient (profile);
Assembly Request Object-specific Description see console-Document part content Sendsmsrequest request = new Sendsmsrequest (); Required: Outgoing cell phone number requEst.setphonenumbers ("15011111111");
Required: SMS signature-can be found in the SMS console request.setsignname ("Di ou software Studio");
Required: SMS templates-can be found in the SMS console Request.settemplatecode ("sms_11111111");
Optional: Variables in the template replace the JSON string, such as the template content is "Dear ${name}, your captcha is ${code}", the value here is Request.settemplateparam ("{\" code\ ": \" 889977\ "}");
Optional: Outid to extend the field to the business side, and eventually bring the value back to the caller Request.setoutid ("Youroutid") in the SMS receipt message;
Hint this may throw an exception, pay attention to catch sendsmsresponse Sendsmsresponse = Acsclient.getacsresponse (request);
return sendsmsresponse; public static Querysenddetailsresponse Querysenddetails (String bizid) throws Clientexception {//self-adjusting timeout
Between System.setproperty ("Sun.net.client.defaultConnectTimeout", "10000");
System.setproperty ("Sun.net.client.defaultReadTimeout", "10000"); Initialize acsclient, temporarily do not support region iclientprofile profile = Defaultprofile.getprofile ("Cn-hangzhou", Accesskeyid, Accesske
Ysecret); Defaultprofile.addendpoint ("cn-hangzhou "," Cn-hangzhou ", product, domain);
Iacsclient acsclient = new Defaultacsclient (profile);
Assembly Request Object Querysenddetailsrequest = new Querysenddetailsrequest ();
Required-Number Request.setphonenumber ("15000000000");
Optional-serial number Request.setbizid (Bizid);
Required-Send date support within 30 days of record query, format yyyyMMdd simpledateformat ft = new SimpleDateFormat ("YyyyMMdd");
Request.setsenddate (New Date ()) (Ft.format);
Required-page size request.setpagesize (10L);
Required-The current page number is counted starting from 1 request.setcurrentpage (1L);
Hint this may throw an exception, pay attention to catch querysenddetailsresponse Querysenddetailsresponse = Acsclient.getacsresponse (request);
return querysenddetailsresponse; public static void Main (string[] args) throws Clientexception, interruptedexception {//texting SENDSM
Sresponse response = sendsms ();
SYSTEM.OUT.PRINTLN ("The data returned by the SMS Interface----------------"); System.out.println ("code=" + Response.getcode ());
System.out.println ("message=" + response.getmessage ());
System.out.println ("requestid=" + Response.getrequestid ());
System.out.println ("bizid=" + Response.getbizid ());
Thread.Sleep (3000L); Identify fine if (Response.getcode ()!= null && response.getcode (). Equals ("OK")) {Querysenddetailsresp
Onse querysenddetailsresponse = Querysenddetails (Response.getbizid ());
SYSTEM.OUT.PRINTLN ("SMS Detail query interface returns data----------------");
System.out.println ("code=" + Querysenddetailsresponse.getcode ());
System.out.println ("message=" + querysenddetailsresponse.getmessage ());
int i = 0;
For (Querysenddetailsresponse.smssenddetaildto smsSendDetailDTO:querySendDetailsResponse.getSmsSendDetailDTOs ())
{System.out.println ("smssenddetaildto[" +i+ "]:");
System.out.println ("content=" + smssenddetaildto.getcontent ()); SysteM.out.println ("errcode=" + Smssenddetaildto.geterrcode ());
System.out.println ("outid=" + Smssenddetaildto.getoutid ());
System.out.println ("phonenum=" + smssenddetaildto.getphonenum ());
System.out.println ("receivedate=" + smssenddetaildto.getreceivedate ());
System.out.println ("senddate=" + smssenddetaildto.getsenddate ());
System.out.println ("sendstatus=" + smssenddetaildto.getsendstatus ());
System.out.println ("template=" + Smssenddetaildto.gettemplatecode ());
} System.out.println ("totalcount=" + Querysenddetailsresponse.gettotalcount ());
System.out.println ("requestid=" + Querysenddetailsresponse.getrequestid ());
}
}
}