admin
2025-02-11 8cc47cfe4c6d6b48e62cf00f6cbd0951ec57c264
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
package com.everyday.word.utils;
 
import com.everyday.word.exception.SMSException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.json.JSONException;
 
import java.io.IOException;
 
/**
 * @author hxh
 * @description 腾讯云短信发送工具
 * @date 15:22 2025/2/11
 * @return
 **/
public class TencentSMSUtil {
 
    /**
     * @return void
     * @author hxh
     * @description 发送单条短信
     * @date 15:23 2025/2/11
     * @param: phone
     * @param: msg
     * @param: smsAppId
     * @param: smsAppKey
     **/
    public static void sendSingleMsg(String phone, String msg, String smsAppId, String smsAppKey) throws SMSException {
        SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(smsAppId),
                smsAppKey);
        SmsSingleSenderResult result = null;
        try {
            result = ssender.send(0, "86", phone, msg, "", "");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (HTTPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        if (result == null) {
            throw new SMSException(2, "短信发送失败");
        }
        if (result.result == 1025) {
            throw new SMSException(result.result, "今日验证码发送超限,请明日再试");
        } else if (result.result != 0) {
            // 发送失败
            throw new SMSException(result.result, "短信发送失败");
        }
 
    }
 
}