java编写发手机短信的程序

本文介绍了一种使用Java实现的短信发送方法,通过调用特定的API接口完成短信发送任务。该实现利用了Apache Commons库中的多个组件,如HTTP客户端、语言工具等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


需要的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 "未知错误!";

}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值