yujian
2019-07-29 a1175313094799efcdbbecf2840a90350d3159a7
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
package com.yeshi.fanli.util;
 
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.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.yeshi.fanli.exception.SMSException;
import com.yeshi.fanli.log.LogHelper;
 
//腾讯短信发送
public class AliyunSMSUtil {
 
    /**
     * 发送单一短信
     * 
     * @param phone
     * @param msg
     */
    public static void sendSingleMsg(String phone, String templateCode, String templateParamJson) throws SMSException {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constant.smsConfig.getAliyunAppId(),
                Constant.smsConfig.getAliyunAppSecret());
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        // 使用post提交
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(phone);
        request.setSignName("小影记科技");
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParamJson);
        SendSmsResponse sendSmsResponse;
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
            if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
                // 请求成功
                LogHelper.smsInfo("阿里云短信发送成功:" + phone);
            } else {
                LogHelper.smsInfo("阿里云短信发送失败:" + phone + "-" + sendSmsResponse.getMessage());
                throw new SMSException(3, sendSmsResponse.getMessage());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
 
    }
 
}