随着阿里云短信服务的改版,使用阿里云短信服务的过程明显比过去要麻烦的多。相信很多人或多或少大概都被困扰过,所以,选用另外的企业所推出的短信发送或许会是个更好的选择,
首先点击上面的官方链接,注册账号然后登陆
新用户会送你10块钱的金额,差不多200条短信,然后点击配置管理
进入配置管理后,点击新建模版
根据提示填写,模板可以自己定义,当然也可以使用下面的模板,最后点击提交审核等待审核通过即可
接下来就是代码: 首先pom.xml
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
Util:
先获取所需的SID和TOKEN填入工具类对应的地方
将申请模版时填写的短信签名填入 中括号,后面的内容就是你们所选择的模板的内容,这个一定要对应起来,不然会报错
String tamp = "【模版时填写的短信签名】您的验证码为:" + rod + ",请于"+1+"分钟内正确输入,如非本人操作,请忽略此短信。";
package com.athome.jd.util;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONObject;
public class GetMessageCode {
private static final String QUERY_PATH = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
private static final String ACCOUNT_SID = "";
private static final String AUTH_TOKEN = "";
// 根据相应的手机号发送验证码
public static String getCode(String phone) {
String rod = smsCode();
String timestamp = getTimestamp();
String sig = getMD5(ACCOUNT_SID, AUTH_TOKEN, timestamp);
String tamp = "【xxxx】您的验证码为:" + rod + ",请于"+1+"分钟内正确输入,如非本人操作,请忽略此短信。";
OutputStreamWriter out = null;
BufferedReader br = null;
StringBuilder result = new StringBuilder();
try {
URL url = new URL(QUERY_PATH);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);// 设置是否允许数据写入
connection.setDoOutput(true);// 设置是否允许参数数据输出
connection.setConnectTimeout(5000);// 设置链接响应时间
connection.setReadTimeout(10000);// 设置参数读取时间
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
// 提交请求
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
String args = getQueryArgs(ACCOUNT_SID, tamp, phone, timestamp, sig, "JSON");
System.out.println(args);
out.write(args);
out.flush();
// 读取返回参数
br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String temp = "";
while ((temp = br.readLine()) != null) {
result.append(temp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject json = new JSONObject(result.toString());
String respCode = json.getString("respCode");
String defaultRespCode = "00000";
if (defaultRespCode.equals(respCode)) {
return rod;
} else {
return defaultRespCode;
}
}
//测试功能
public static void main(String[] args) {
String result = getCode("");
}
// 定义一个请求参数拼接方法
public static String getQueryArgs(String accountSid, String smsContent, String to, String timestamp, String sig,
String respDataType) {
return "accountSid=" + accountSid + "&smsContent=" + smsContent + "&to=" + to + "×tamp=" + timestamp
+ "&sig=" + sig + "&respDataType=" + respDataType;
}
// 获取时间戳
public static String getTimestamp() {
return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
}
// sing签名
public static String getMD5(String sid, String token, String timestamp) {
StringBuilder result = new StringBuilder();
String source = sid + token + timestamp;
// 获取某个类的实例
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
// 要进行加密的东西
byte[] bytes = digest.digest(source.getBytes());
for (byte b : bytes) {
String hex = Integer.toHexString(b & 0xff);
if (hex.length() == 1) {
result.append("0" + hex);
} else {
result.append(hex);
}
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result.toString();
}
// 创建验证码
public static String smsCode() {
String random = (int) ((Math.random() * 9 + 1) * 100000) + "";
return random;
}
}
这样也就OK了,直接调用方法传入正确的手机号即可