现在的位置: 首页 > 综合 > 正文

java编写发手机短信的程序

2018年04月28日 ⁄ 综合 ⁄ 共 3132字 ⁄ 字号 评论关闭

下面是公司项目现成的代码:

需要的jar包:

commons-httpclient

commons-lang

commons-logging

commons-codec



import java.util.regex.Pattern;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.NameValuePair;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.lang.StringUtils;

public class SMSTest {

private static
final String KEY_CELL =
"Cell";

private static
final String KEY_CONTENT =
"Content";

private static
final String KEY_MOBILE =
"Mobile";

private static
final String KEY_PWD =
"Pwd";

private static
final String KEY_USERNAME =
"CorpID";

private static
final String KEY_SEND_TIME =
"SendTime";

private static
final Pattern RESPONSE_FAILED_PATTERN = Pattern.compile("^-\\d{1,}$");

public void send(String mobile, String content)
throws Exception {

NameValuePair[] parametersBody = contructParams4Send(content, mobile);

sendRequestAndGetResponse(parametersBody,
"sendUrl");   //sendUrl短信中心地址

}

private NameValuePair[] contructParams4Send(String text, String mobiles){

NameValuePair paramUsername =
new
NameValuePair(KEY_USERNAME, "*******");

NameValuePair paramPassword =
new
NameValuePair(KEY_PWD, "**********");

NameValuePair mobile = new NameValuePair(KEY_MOBILE, mobiles);

NameValuePair content =
new
NameValuePair(KEY_CONTENT, text);

NameValuePair cell = new NameValuePair(KEY_CELL,
"");

NameValuePair sendTime =
new
NameValuePair(KEY_SEND_TIME,
""
);

NameValuePair[] parametersBody =
new
NameValuePair[] { paramUsername, paramPassword, mobile, content, cell,sendTime };

return parametersBody;

}

private String sendRequestAndGetResponse(NameValuePair[] parametersBody, String url)
throws Exception {

HttpClient httpClient =
new
HttpClient();

httpClient .getParams().setParameter("http.protocol.content-charset",
"gb2312");

PostMethod post = new PostMethod(url);

try {

post.setRequestBody(parametersBody);

httpClient.executeMethod(post);

String response = post.getResponseBodyAsString();

if (!isFailed(response)) {

System.out.println("send message success,response code is<" + response +
">");

return response;

}else{

System.out.println("send message failed,response code is<" + response +
">," + SMSResponseCode.getMsg(response));

throw
new Exception("发送失败!");

}

} catch (Exception e) {

System.out.println("send message failed." + e.getMessage());

throw
new Exception(e);

} finally {

if (post !=
null) {

post.releaseConnection();

}

}

}

private boolean isFailed(String response) {

return
RESPONSE_FAILED_PATTERN
.matcher(StringUtils.trim(response)).matches();

}

public static
void main(String[] args) {

try {

new SMSTest().send("************",
"哇哈哈哈哈");

} catch (Exception e) {

e.printStackTrace();

}

}

}

public enum SMSResponseCode {

SUCCESS("1",
"发送成功"), NOT_REGIST("-1",
"账号未注册"), OTHER_ERROR("-2",
"其他错误"), 

INVALID_USERNAME_PWD("-3",
"帐号或密码错误"), MAX_MOBILES("-4",
"一次提交信息不能超过600个手机号码"), 

BALANCE_NOT_ENOUGH("-5",
"余额不足,请先充值"), INVALID_SEND_TIME("-6",
"定时发送时间不是有效的时间格式"), 

INVALID_CONTENT_LENGTH("-8",
"发送内容需在3到250字之间") ,
EMPYT_MOBILE("-9",
"发送号码为空");

private String
responseCode;

private String
msg
;

public String getResponseCode() {

return
responseCode;

}

public String getMsg() {

return
msg;

}

private SMSResponseCode(String responseCode, String msg) {

this.responseCode = responseCode;

this.msg = msg;

}

public static String getMsg(String responseCode) {

for (SMSResponseCode code : SMSResponseCode.values()) {

if (code.getResponseCode().equals(responseCode)) {

return code.getMsg();

}

}

return
"未知错误!";

}

}

抱歉!评论已关闭.