添加依赖:
com.sun.mail
javax.mail
public class MessageTest {
//短信服务提供商。这个就百度吧,很多,真的很多。我随便找的一家提供商。
private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
public static void main(String[] args) {
/*
方式二是一键完成短信功能的展示而已
*/
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
client.getParams().setContentCharset("GBK");// 在头文件中设置转码
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");
int mobile_code = (int)((Math.random()*9+1)*100000);
String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");
NameValuePair[] data = {//提交短信
new NameValuePair("account", "C65868831"),// 注册的用户名
new NameValuePair("password", "fc422f9380ae002985db316ecce0ab27"), //查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
//new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
new NameValuePair("mobile", "13590218041"),//要发的手机
new NameValuePair("content", content),//要发的内容
};
method.setRequestBody(data);
try {
client.executeMethod(method);//发送短信
Header[] headers = method.getResponseHeaders();//短信返回信息
int statusCode = method.getStatusCode();//状态码
System.out.println("statusCode:" + statusCode);
for (Header h : headers) {//响应头的打印
System.out.println(h.toString());
}
String result = null;
result = new String(method.getResponseBodyAsString().getBytes(
"GBK"));//打印响应体
System.out.println(result);
method.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
}
}