基于阿里云 aliyun-java-sdk-sms-3.0.0-rc1.jar
package com.vinci.units;
import org.json.JSONException;
import org.json.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
/**
* 阿里云发短信工具
*
* @author ccq
*
*/
public class MassagesUtili {
public static final String TEMPLATECODE = "xxxxxxx";// 管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)
private static final String SIGNNAME = "xxxxxxx";// 管理控制台中配置的短信签名(状态必须是验证通过)
private static final String ACCESSKEYID = "xxxxxxx";// 是 阿里云颁发给用户的访问服务所用的密钥ID 密钥ID
private static final String ACCESSKEYSECRET = "xxxxxxx";// 是签名结果串,关于签名的计算方法,请参见
/**
* /** 发送短信
*
* @param code
* 6位随机数
* @param product
* 产品名
* @param RecNum
* 目标手机号,多个手机号可以逗号分隔
*
* @return 1发送成功 ,不是1发送失败 返回失败信息
* @return templateCode 短信模板
*/
@SuppressWarnings("unused")
public static String sendMessage(String code, String product, String RecNum, String templateCode) {
String reString = "1";
try {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, ACCESSKEYSECRET);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
SingleSendSmsRequest request = new SingleSendSmsRequest();
request.setSignName(SIGNNAME);// 控制台创建的签名名称
request.setTemplateCode(templateCode);// 控制台创建的模板CODE
JSONObject js = new JSONObject();
js.put("code", code);
js.put("product", product);
request.setParamString(js.toString());// 短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。"
// request.setParamString("{}");
request.setRecNum(RecNum);// 接收号码
SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
} catch (Exception e) {
e.printStackTrace();
reString = e.getMessage();
}
return reString;
}
/**
* /** 发送短信
*
* @param content
* 短信所需的参数 json格式 {"product":"测试","code":"365042"}
* @param TemplateCode
* 管理控制台中配置的审核通过的短信模板的模板CODE(状态必须是验证通过)
* @param SignName
* 管理控制台中配置的短信签名(状态必须是验证通过)
* @param AccessKeyId
* 阿里云颁发给用户的访问服务所用的密钥ID
* @param AccessKeySecret
* 签名结果串
* @param RecNum
* 目标手机号,多个手机号可以逗号分隔
*
* @return 1发送成功 ,不是1发送失败 返回失败信息
*/
@SuppressWarnings("unused")
public static String sendMessage(String content, String TemplateCode, String SignName, String AccessKeyId,
String AccessKeySecret, String RecNum) {
String reString = "1";
try {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms", "sms.aliyuncs.com");
IAcsClient client = new DefaultAcsClient(profile);
SingleSendSmsRequest request = new SingleSendSmsRequest();
request.setSignName(SignName);// 控制台创建的签名名称
request.setTemplateCode(TemplateCode);// 控制台创建的模板CODE
request.setParamString(content);// 短信模板中的变量;数字需要转换为字符串;个人用户每个变量长度必须小于15个字符。"
// request.setParamString("{}");
request.setRecNum(RecNum);// 接收号码
SingleSendSmsResponse httpResponse = client.getAcsResponse(request);
} catch (Exception e) {
e.printStackTrace();
reString = e.getMessage();
}
return reString;
}
@SuppressWarnings("unused")
public static void main(String[] args) throws JSONException {
String code = "123123";
String product = "xxxx";
// //System.out.println(sendMessage(code, product, "13533xxxxxx"));
}
}