cloudroam
2025-03-28 795fb91c35818cd806b3cf19794736efccd59760
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
package com.mzl.flower.utils;
 
import com.alibaba.fastjson.JSON;
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 lombok.extern.slf4j.Slf4j;
 
@Slf4j
public class SmsUtil {
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";
 
 
    static final String connectTime = "30000";
    static final String readTime = "30000";
    static final String keyId = "LTAI5tFGHa9bwhuEDKH6YPnc";
 
    static final String keySecret = "BrY0BM4pvDXhVKOMLsXzgzlhVe1keQ";
    static final String signName = "云南花满芫花卉";
 
    public static SendSmsResponse sendSms(final String tel, final String templateCode, Object paramMap) throws ClientException {
        return sendSms(tel, templateCode, JSON.toJSONString(paramMap));
    }
 
    public static SendSmsResponse sendSms(final String tel, final String templateCode, final String templateParam) throws ClientException {
        return sendSms(tel, templateCode, templateParam, null);
    }
 
    public static SendSmsResponse sendSms(final String tel, final String templateCode, final String templateParam, final String outId) throws ClientException {
        log.info("Send SMS [mobile no:" + tel + " templateCode:" + templateCode + " templateParam:" + templateParam + "]");
        System.setProperty("sun.net.client.defaultConnectTimeout", connectTime);
        System.setProperty("sun.net.client.defaultReadTimeout", readTime);
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", keyId, keySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(tel);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParam);
        request.setOutId(outId);
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        log.info("Send SMS [mobile no:" + tel + " templateCode:" + templateCode + " templateParam:" + templateParam + "] result: " + sendSmsResponse.getCode() + " " + sendSmsResponse.getMessage());
        return sendSmsResponse;
    }
 
}