Aliyun SMS demo__ Aliyun SMS

Source: Internet
Author: User
Tags getmessage aliyun
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 ( 
 stored in the Libs directory of the project) * 1:aliyun-java-sdk-core.jar * 2:aliyun-java-sdk-dysmsapi.jar * * Note: Demo project code using UTF-8 * International SMS Send no reference to this demo */public class Smsdemo {//Product Name: Cloud Communication SMS API products, developers do not need to replace the 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 ("15000000000");
        Required: SMS Signature-request.setsignname ("Cloud communications") can be found in the SMS console;
        Required: SMS templates-can be found in the SMS console Request.settemplatecode ("sms_1000000"); Optional: Variables in the template replace the JSON string, such as the template content is "Dear ${name}, your authentication code is ${code}", the value here is Request.settemplateparam ("{\ name\": \ "tom\", \ "Code\"

        : \ "123\"} ");

        Optional-Uplink SMS Extension code (no special needs of users please ignore this field)//request.setsmsupextendcode ("90997");

        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, Accesskeysecret);
        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 ()); }

    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.