Ali cloud messaging service

1. Add dependence

   

 <-! Add Ali cloud texting dependent ->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>

utils 2. sent

   

package com.tensquare.sms.util;

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.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * SMS Tools
 * @Author admin
 */
@Component
public  class SmsUtil {

    // Product Name: Cloud Communications SMS API product, developers do not need to replace 
    static  Final String Product = "Dysmsapi" ;
     // product domain, developers do not need to replace 
    static  Final String Domain = "dysmsapi.aliyuncs.com" ;

    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.sms.templateCode}")
    private String templateCode;
    @Value("${aliyun.sms.signName}")
    private String signName;


    /**
     * send messages
     *
     * @Param Mobile phone number
     * @Param param parameters
     * @Return 
     * @throws ClientException
      * / 
    public SendSmsResponse sendSms (Mobile String, String param) throws ClientException {
         // can be self-adjusted timeout 
        System.setProperty ( "sun.net.client.defaultConnectTimeout", "10000" );
        System.setProperty ( "sun.net.client.defaultReadTimeout", "10000" );
         // initialize acsClient, does not support region of 
        IClientProfile Profile = DefaultProfile.getProfile ( "CN-Hangzhou" , accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        AcsClient IAcsClient = new new DefaultAcsClient (Profile);
         // Assembly requested object - particularly described Console - part document 
        SendSmsRequest = Request new new SendSmsRequest ();
         // Required: phone number to be transmitted 
        request.setPhoneNumbers (Mobile);
         // required: SMS signature - can be found in the SMS console 
        request.setSignName (signName);
         // required: SMS templates - can be found in the SMS console 
        request.setTemplateCode (templateCode);
         // optional: template variable substitution JSON string, such as templates for the content "Dear $ {name}, your verification code is $ {code}", the value herein 
        request.setTemplateParam ( "{\" code \ ": \" "+ param + "\"} " );
        // Optional - up message spreading code (no special needs of users ignore this field)
         //request.setSmsUpExtendCode ( "90997");
         // Optional: outId is provided to the business side extension field, culminating in an SMS message receipt this value back to the caller 
        request.setOutId ( "yourOutId" );
         // hint this Department might throw an exception, note the catch 
        sendSmsResponse sendSmsResponse = acsClient.getAcsResponse (Request);
         return sendSmsResponse;
    }

    public QuerySendDetailsResponse querySendDetails (Mobile String, String bizId) throws ClientException {
         // can be self-adjusted timeout 
        System.setProperty ( "sun.net.client.defaultConnectTimeout", "10000" );
        System.setProperty ( "sun.net.client.defaultReadTimeout", "10000" );
         // initialize acsClient, does not support region of 
        IClientProfile Profile = DefaultProfile.getProfile ( "CN-Hangzhou" , accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        AcsClient IAcsClient = new new DefaultAcsClient (Profile);
         // assembled requested object 
        QuerySendDetailsRequest = Request new new QuerySendDetailsRequest ();
         // Required - number 
        request.setPhoneNumber (Mobile);
         // optional - Serial 
        request.setBizId (bizId);
         / / required - sent within 30 days of the date of record to support queries, format yyyyMMdd 
        SimpleDateFormat ft = new new SimpleDateFormat ( "yyyyMMdd" );
        request.setSendDate (ft.format ( new new a Date ()));
         // Required - page size 
        request.setPageSize (10L );
         // Required - 1 counted from the current page 
        request.setCurrentPage (1L );
         // hint here might throw an exception, note the catch 
        querySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse (Request);
         return querySendDetailsResponse;
    }
}

Guess you like

Origin www.cnblogs.com/sjzxxy/p/12041621.html