アリメールサービス

3681 ワード

初歩的なアリメールに必要なものは公式サイトで説明されています
アクセスキーを有効にして、初めて100本を無料で送ってテストすることができます.
https://help.aliyun.com/document_detail/59210.html?spm=5176.sms-account.108.5.381e1cbefHnojl
 
/**
     *      
     * @param phone
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSend/{phone}",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSend(@PathVariable String phone, HttpServletRequest req) throws Exception{
        ApiResult result = ApiResult.makeSuccessResult();
        //                 
        String code = ShareCodeUtil.toSerialCode(Integer.parseInt(phone.substring(phone.length()-4,phone.length())));
        //      
        AliMessageSend.sendSms(phone,code);
        //         
        req.getSession().setAttribute(phone,code);
        return result;
    }
package com.fx.fxxt.utils.aliMessageSend;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
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 com.fx.fxxt.utils.ShareCodeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: Yi xiaobai
 * @Date: 2018/10/15 0015    10:03
 */
public class AliMessageSend {
    //     :     API  ,       
    private static final String product = "Dysmsapi";
    //     ,       
    private static final String domain = "dysmsapi.aliyuncs.com";

    //              AK(           )
    //private static String mobile = "176****1111";
    private static String accessKeyId = "****";
    private static String accessKeySecret = "****";
    private static String signName = "    ";
    private static String templeteCode = "SMS_********";

    //       
    public static void main(String[] args) {
        /*try {
            //sendSms();
        } catch (ClientException e) {
            System.out.println(e);
        }*/
    }

    //       
    public static SendSmsResponse sendSms(String phone,String code) throws ClientException {
        //          
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //    acsClient,    region 
        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //       -        -      
        SendSmsRequest request = new SendSmsRequest();

        //   :      
        request.setPhoneNumbers(phone);
        //   :    -          
        request.setSignName(signName);
        //   :    -          
        request.setTemplateCode(templeteCode);

        //   :        JSON ,      "     ,      ${code}" ,     


        String jsonParam = "{\"code\":\""+ code +"\"}";

        request.setTemplateParam(jsonParam);

        // hint          ,  catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }
}

コントラスト検証コード
//                              equals 
String code = (String) request.getSession().getAttribute(dto.getPhone());