JAVA代码之短信发送功能(luosimao)

JAVA写短信发送功能

前景概要本人使用的是luosimao的方式,短信肯定是要收费的。需要买,但是测试的话是有免费的,一般是10条短信,10条语音,也可以发邮件(不建议用luosimao来写邮件,因为smtp是免费的,luosimao可能是收费的,我也没用过,下一篇会写发邮件的),总体来说螺丝帽写短信的代码还是比较简单的,速度也很快。

1.准备工作

            (1)  登录luosimao官网 (https://luosimao.com/ ) 首先注册一个账号,登录之后会有以下页面:

        

       (2)点击短信(上图指示短信入口处)

            将入下图所示的key记下,后边发送短信调用接口都是依靠这个Key去辨识,key相当于他们的用户,里边有短信余额,所以尽量保密,以免泄露,当然这里测试所以没什么关系,哈哈

           

        (3)另外,这里的签名管理和短信模板都可以进行设置,建议是设置,因为如果设置了短信模板,这样发送的时候,就很少进行他们官网的审核(个人感觉,发送速度提高了不少),签名必须符合要求,具体要求请看上边第一张图中的开发文档处,里边有要求的。

         

            

         上图短信模板虽然设置了,但是在代码中并不是只写###那部分,还是需要加上前后那两个模板,只是为了方便发送才设置短信模板,个人感觉。

         如果给公司写,需要公司充值。这个就不用解释了。电信移动也不会让咱有这个漏洞滴


2.代码编写   

    如果想自己写请看官网上的开发文档里边,有各种语言的,我也是从那里弄下来改了改,便于使用。

    首先导入两个jar包,地址在官网--->开发文档--->左边代码示例中的JAVA---->页面最下边即是

    (下载网盘地址:)点击打开链接  

     luosimao提供三个接口,单条发送批量发送余额查询三个。

    我直接将这个类的代码附在下边,然后解释调用的过程

      
   
package org.hdht.util;
/**
 * 发送短信接口
 */
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
/**
 *
 * @author lingshufeng
 */
public class SendMessage {
	private static final String LUOSIMAO_USER = "api";                 
	private static final String urlSendSingle = "http://sms-api.luosimao.com/v1/send.json"; //发送单条短信接口
	private static final String urlSendBatch = "http://sms-api.luosimao.com/v1/send_batch.json"; //发送批量短信接口
	private static final String urlSerchString = "http://sms-api.luosimao.com/v1/status.json"; //账户查询接口
	private static final String MESSAGE_MODEL_STRING = "【航天宏图】"; //短信签名    应与luosimao账户中设置的签名一致,如果不一致则不可用
	public static Map
   
   
    
     responseMap =new HashMap
    
    
     
     ();
	private String keyString; //验证密码
	public SendMessage(String keyString){
		this.keyString = keyString;
	}
	/**
	 * 执行方法入口---发送短信
	 * @return 返回发送状态信息
	 */
    public String MessageExecuteMethod(Map
     
     
      
       dataMap){
    	String resultString = null;
        if(dataMap ==null){
        	return null; //短信内容为空 或者 错误的手机号
        }
        String mobile = dataMap.get("mobile"); //手机号
        String messageString = dataMap.get("message")+MESSAGE_MODEL_STRING; //发送短信内容
        MultivaluedMapImpl formData = new MultivaluedMapImpl();
        formData.add("message", messageString); //必须要在luosimao官网上添加短信模板,并且这里的信息要符合短信模板的要求,这样发送的时候就不会经过他们进行审核,发送速度快
        if(!StringUtils.isEmpty(mobile)){ //手机号码不为空
        	if(mobile.split(",").length<2){ //单条信息
        		formData.add("mobile", mobile);  		
        		resultString = SendMessageMethod(formData,urlSendSingle);
        	}else{
        		formData.add("mobile_list", mobile);
        		resultString = SendMessageMethod(formData,urlSendBatch); 	
        	}
        }else{
        	formData.add("moblie", "");
        	resultString = SendMessageMethod(formData,urlSendSingle);
        }
        return resultString;
    }
    /**
     * 执行方法入口----账户查询
     * @return map 如果交互成功会有两个key  "code"表示返回状态代码,"msg"表示返回信息
	 *      
     */
    public Map
      
      
       
        UserInfoSearchMethod(){
    	Map
       
       
         resultMap = SearchUserDetails(urlSerchString); return resultMap; } /** * 发送短信逻辑 */ public String SendMessageMethod(MultivaluedMapImpl formData,String urlString){ String responseString = null; Client client = Client.create(); client.addFilter(new HTTPBasicAuthFilter(LUOSIMAO_USER,"key-"+this.keyString)); WebResource webResource = client.resource(urlString); ClientResponse response = webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).post(ClientResponse.class, formData); String textEntity = response.getEntity(String.class); try { JSONObject jsonObj = new JSONObject( textEntity ); String error_code = jsonObj.getInt("error")+""; responseString= responseMap.get(error_code); } catch (JSONException ex) { Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex); } return responseString; } /** * 账户信息(查询余额) */ private Map 
        
          SearchUserDetails(String urlString){ Map 
         
           resultMap = new HashMap 
          
            (); Client client = Client.create(); client.addFilter(new HTTPBasicAuthFilter(LUOSIMAO_USER,"key-"+this.keyString)); WebResource webResource = client.resource(urlString); ClientResponse response = webResource.get( ClientResponse.class ); String textEntity = response.getEntity(String.class); try { JSONObject jsonObj = new JSONObject( textEntity ); String error_code = jsonObj.getInt("error")+""; String deposit = jsonObj.getInt("deposit")+""; resultMap.put("code", responseMap.get(error_code)); resultMap.put("counts", deposit); } catch (JSONException ex) { Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex); } return resultMap; } public static void main(String[] args) { SendMessage sendMessage = new SendMessage("513c5a2716d76d53a0d72d0051131ad6");//对应luosimao注册账号的key Map 
           
             map = new HashMap 
            
              (); map.put("mobile", "15201020689,13562670526,18366505330"); map.put("message", "山西卫星遥感平台提醒,发个短信真困难【航天宏图】"); //符合该注册账号中的短信模板会提高发送速度 String telResponse = sendMessage.MessageExecuteMethod(map); System.out.println(telResponse); Map 
             
               userResponse = sendMessage.UserInfoSearchMethod(); System.out.println("User_code"+userResponse.get("code")); System.out.println("Remain_counts"+userResponse.get("counts")); } static{ if(responseMap.isEmpty()){ responseMap.put("0","发送成功"); responseMap.put("-10","验证信息失败"); responseMap.put("-11","用户接口被禁用"); responseMap.put("-20","短信余额不足"); responseMap.put("-30 ","短信内容为空"); responseMap.put("-31","短信内容存在敏感词"); responseMap.put("-32","短信内容缺少签名信息"); responseMap.put("-33","短信过长,超过300字(含签名)"); responseMap.put("-34","签名不可用"); responseMap.put("-40","错误的手机号"); responseMap.put("-41","号码在黑名单中"); responseMap.put("-42","验证码类短信发送频率过快"); responseMap.put("-43","号码数量太多"); responseMap.put("-50","请求发送IP不在白名单内"); responseMap.put("-60","定时时间为过去"); } } } 
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

     将该类附到你们项目下,导入上边两个jar包,并且找到在上边准备好的key,欧了开始调用。
   
 (1)SendMessage sendMessage = new SendMessage("key") ;
      //以上代码,对应luosimao注册账号的key串,使用时将该段代码中的蓝色字体替换成你的key

      Map<String, String> map = new HashMap<String, String>();
      //以上的map用来存收件人号码和短信内容,具体格式如下:
  map.put("mobile", "号码1,号码2,号码3"); //这里传一个号码也是可以的,上边方法内部已经作出判断,根据个数调用不同接口
  map.put("message", "山西卫星遥感平台提醒,这是CSDN啊【航天宏图】"); //符合注册账号中的短信模板会提高发送速度

  String telResponse =  sendMessage.MessageExecuteMethod(map); //调用发短信方法
  System.out.println(telResponse);//这是返回信息,具体要看官网开发文档,修改地方为上边代码中的responseMap

Map<String, String> userResponse =  sendMessage.UserInfoSearchMethod(); //调用查询余额方法。
  System.out.println("User_code"+userResponse.get("code"));
  System.out.println("Remain_counts"+userResponse.get("counts")); //返回的短信剩余量



  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茁壮成长的凌大大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值