package com.utils;
import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import java.io.IOException;
import java.util.Properties;
/**
* @Author: luoxiao
* @Date: 2019/12/13 17:22
*/
public class SMSUtil {
//文件路径
public static String filePath= "/sendPhone.properties";
private static Properties pro=PropertiesUtils.getProperties(filePath);
// SDK AppID 以1400开头
public static int appid =Integer.parseInt(pro.getProperty("appid"));
// 短信应用 SDK AppKey
public static String appkey = pro.getProperty("appkey");
// 短信模板 ID,需要在短信应用中申请
public static int templateId = Integer.parseInt(pro.getProperty("templateId"));
// 签名
public static String smsSign =pro.getProperty("smsSign");
//根据模板发送单条短信
public static void sendMessage(String phone,String[] params){
try {
smsSign=new String(smsSign.getBytes("ISO-8859-1"), "GBK");
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
// 签名参数未提供或者为空时,会使用默认签名发送短信
SmsSingleSenderResult result = ssender.sendWithParam("86",
phone, templateId, params, smsSign, "", "");
System.out.println(result);
} catch (HTTPException e) {
// HTTP 响应码错误
e.printStackTrace();
} catch (JSONException e) {
// JSON 解析错误
e.printStackTrace();
} catch (IOException e) {
// 网络 IO 错误
e.printStackTrace();
}
}
//按模板发送多条短信
public static void sendMsgByModel(String phoneNumbers[]) {
try {
smsSign=new String(smsSign.getBytes("ISO-8859-1"), "GBK");
// 需要发送短信的手机号码
//String phone="17338131017";
String[] params = {"8月12", "重大事件", "智网内网被DDos攻击", ""};
SmsMultiSender ssender = new SmsMultiSender(appid, appkey);
SmsMultiSenderResult result = ssender.sendWithParam("86", phoneNumbers,
templateId, params, smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
System.out.println(result);
} catch (HTTPException e) {
// HTTP 响应码错误
e.printStackTrace();
} catch (JSONException e) {
// JSON 解析错误
e.printStackTrace();
} catch (IOException e) {
// 网络 IO 错误
e.printStackTrace();
}
}
public static void sendMsg(String phone) {
String[] phoneNumbers = {phone};
try {
smsSign=new String(smsSign.getBytes("ISO-8859-1"), "GBK");
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
SmsSingleSenderResult result = ssender.send(0, "86", phoneNumbers[0],
"【智网安云】您的验证码是: 5678", "", "");
System.out.println(result);
} catch (HTTPException e) {
// HTTP 响应码错误
e.printStackTrace();
} catch (JSONException e) {
// JSON 解析错误
e.printStackTrace();
} catch (IOException e) {
// 网络 IO 错误
e.printStackTrace();
}
}
private static String SIGNATURE = "智网安云";
private static final int TYPE_NORMAL = 0;
private static final String NATION_CODE = "86";
private static final Log log = LogFactory.getLog(SMSUtil.class);
public static SmsSingleSenderResult sendSMS(String phoneNumber, String verificationCode) {
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
// 该msg需要与腾讯云上的短信模版一致,不然会显示无法匹配。
String msg = "【" + SIGNATURE + "】您的验证码为:" + verificationCode + ",该验证码2分钟内有效,请勿泄漏于他人!";
SmsSingleSenderResult result = null;
try {
result = ssender.send(TYPE_NORMAL, NATION_CODE, phoneNumber, msg, "", "");
} catch (HTTPException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}