Java常用工具类-发短信(集成云通信的企业信使短信平台)

集成云通信的企业信使短信平台,实现发短信功能,具体代码如下:

package com.zrsc.sendsms;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * 短信发送工具类
 * 
 */
public class MessageSenderUtil {
    public static final String MSG_MOBILE_ADMIN="18310646106";
    private static String NOTESTARTSIGN="【***】";//短信前的签名
    private static String SENDURL="http://115.28.50.135:8888/sms.aspx";
    private static String USERID="***";//发短信的账号ID
    private static String USERNAME="***";//发短信的账号
    private static String PASSWORD="***";//发短信密码对应的MD5值
    private static String VERIFICATION_CODE=NOTESTARTSIGN+"您的验证码是:1234";//短信验证码例子
    /**
     * 
     * 功能描述:发送短信
     *
     */
    @SuppressWarnings("deprecation")
    public static boolean sendMsg(String mobile,String content){
        HttpPost httpRequest = null;
        try {
            HttpClient client = new org.apache.http.impl.client.DefaultHttpClient(
                    new PoolingClientConnectionManager());
            client.getParams().setIntParameter(
                    CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
            client.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
                    5000);
            httpRequest = new HttpPost(SENDURL);
            //httpRequest.setURI(new URI(baseUrl));
            List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();  
            params.add(new BasicNameValuePair("action", "send"));  
            params.add(new BasicNameValuePair("userid", USERID)); 
            params.add(new BasicNameValuePair("account", USERNAME));
            params.add(new BasicNameValuePair("password", PASSWORD));
            params.add(new BasicNameValuePair("mobile", mobile));
            params.add(new BasicNameValuePair("content", content));
            UrlEncodedFormEntity entity =new UrlEncodedFormEntity(params, "UTF-8");  
            httpRequest.setEntity(entity);
            HttpResponse httpResponse = client.execute(httpRequest);
            if (null != httpResponse) {
                String result = EntityUtils.toString(httpResponse.getEntity());
                System.out.println(result);
                if("ok".equals(getSendMsgResult(result))){//0:代表成功
                    return true;
                }
            }
        } catch (Exception e) {
             throw new RuntimeException(e);
        } finally {
            if (httpRequest != null) {
                httpRequest.reset();
            }
        }
        return false;
    }

    private static String getSendMsgResult(String resultStr) throws DocumentException{
        Document document = DocumentHelper.parseText(resultStr);
        Element root = document.getRootElement();  
        Element message = root.element("message"); 
        return message.getText();
    }
    public static void main(String[] args) throws Exception {
        System.out.println(sendMsg(MSG_MOBILE_ADMIN,VERIFICATION_CODE));
    }
}
 

发送短信成功后,会返回如下信息:

<?xml version="1.0" encoding="utf-8" ?><returnsms>
 <returnstatus>Success</returnstatus>
 <message>ok</message>
 <remainpoint>0</remainpoint>
 <taskID>0</taskID>
 <successCounts>1</successCounts></returnsms>

下载源码

源码包含如下

1.集成华软通信短信网关,已实现发验证码短信,对应Java文件为SMS106.java,没有依赖的jar。
2.集成河南华夏通信短信网关,已实现发验证码短信,对应Java文件为ShenZhouMsg.javar,没有依赖的jar。
3.集成云通信的企业信使短信平台,已实现发验证码短信,对应Java文件为MessageSenderUtil.javar,依赖的jar有httpclient-4.3.3.jar,httpcore-4.3.2.jar,commons-logging-1.1.1.jar,dom4j-1.6.1.jar。
4.集成阿里云短信服务,已实现发验证码短信,对应Java文件为AliSMS.java,依赖的jar有aliyun-java-sdk-core-4.1.0.jar,activation-1.1.1.jar,gson-2.8.5.jar,jaxb-api-2.1.jar,jaxb-core-2.3.0.1.jar,jaxb-impl-2.3.2.jar,httpclient-4.3.3.jar,httpcore-4.3.2.jar。
阿里云短信服务有对接文档,即《阿里短信集成详解.docx》。

综上所述,费用最便宜是集成阿里云短信服务,但技术对接最麻烦。如果想找个最简单对接又便宜的,建议使用河南华夏通信短信网关。

猜你喜欢

转载自blog.csdn.net/jlq_diligence/article/details/88662110