java 調用騰訊雲短信api


 

依賴:

     <!--騰訊短信依賴-->
        <dependency>
            <groupId>com.github.qcloudsms</groupId>
            <artifactId>qcloudsms</artifactId>
            <version>1.0.6</version>
        </dependency>

代碼:

public class SMSUtil {


    private static final int appid =xxxxx;
    private static final String appkey = "xxxxxx";


    public static String send(String phoneNumbers) {
        int yzm = random();
        String num = Integer.toString(yzm);
        SmsSingleSenderResult result = null;
        try {
            String smsSign = "" + yzm + "為您的登錄驗證碼,請於2分鍾內填寫。如非本人操作,請忽略本短信。"; // NOTE: 這里的簽名"騰訊雲"只是一個示例,真實的簽名需要在短信控制台中申請,另外簽名參數使用的是`簽名內容`,而不是`簽名ID
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            result = ssender.send(0, "86", phoneNumbers, smsSign, "", "");
            if (result.result == 0) {
                //發送成功返回驗證碼
                return num;
            }
        } catch (HTTPException e) {
            //System.out.println("HTTP響應碼錯誤");
            return result.toString();
        } catch (JSONException e) {
            //System.out.println("json解析錯誤");
            return result.toString();
        } catch (IOException e) {
            //System.out.println(" 網絡IO錯誤");
            return result.toString();
        } catch (com.github.qcloudsms.httpclient.HTTPException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /*
        生成6位驗證碼
     */
    private static int random() {
        Random r = new Random();
        StringBuffer sb = new StringBuffer();
        int[] c = new int[6];
        for (int i = 0; i < 6; i++) {
            c[i] = r.nextInt(9) + 1;
            sb.append(c[i]);
        }
        return Integer.parseInt(sb.toString());

    }

    public static void main(String[] args) {
        String send = SMSUtil.send("這里給手機號碼");
        System.out.println(send);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM