JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可使用sina提供的webservice进行发送,可是须要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的经常使用,前提是须要购买硬件设备(3)使用中国网建提供的SMS短信平台(申请帐号地址:http://sms.webchinese.cn/default.shtml) html
本程序主要是运用了中国网建提供的SMS短信平台,这个短信平台基于java提供个专门的接口java
public class SMSverification {
/**
* 返回一个map集合,保存验证码code和发送短信的状态码result
*
* @param phone
* @return map
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static HashMap<String, String> getMsgStatus(String phone)
throws UnsupportedEncodingException, IOException {
HashMap<String, String> map = new HashMap<String, String>();
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
post.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
String code = varificationCode();
NameValuePair[] data = {
new NameValuePair("uid", "yantuyouni"),// 注册的用户名
new NameValuePair("key", "yantuyouni"),// 注册成功后,登陆网站使用的密钥,这个密钥要登陆到国建网而后有一个API接口,点进去就有一个key,能够改,那个才是密钥
new NameValuePair("smsMob", phone),// 手机号码
new NameValuePair("smsText", phone + "用户您好,欢迎加入沿途有你。验证码:"
+ code + "。") };//设置短信内容
post.setRequestBody(data);
client.executeMethod(post);
Header[] headers = post.getResponseHeaders();
int statusCode = post.getStatusCode();
System.out.println("statusCode===========" + statusCode);
for (Header header : headers) {
System.out.println(header.toString());
}
String result = new String(post.getResponseBodyAsString().getBytes(
"gbk"));
System.out.println("返回的状态消息========" + result);
map.put("code", code);
map.put("result", result);
return map;web
}app
/**
* 生成四位随机数
*
* @return
*/
public static String varificationCode() {
String code = "";
int i = (int) ((Math.random() * 9 + 1) * 1000);// 生成一个四位的随机数
code = String.valueOf(i);// Integer.toString(i);
return code;
}
}dom
短信发送后返回值 说 明
-1 没有该用户帐户
-2 密钥不正确(不是用户密码)
-3 短信数量不足
-11 该用户被禁用
-14 短信内容出现非法字符
-41 手机号码为空
-42 短信内容为空
大于0 短信发送数量 post