2025年springboot+网建短信通发送短信

springboot+网建短信通发送短信Springboot 发送短信验证码 前言 短信验证码是通过发送验证码到手机的一种有效的验证码系统 主要用于验证用户手机的合法性及敏感操作的身份验证 第三方短信发送平台有很多种 各个平台有各自的优缺点 在选择的时候可以根据自己的具体实际情况定夺 此篇文章讲述了 Springboot 集成 SMS 发送短信 准备 1 用户注册

大家好,我是讯享网,很高兴认识大家。

Springboot发送短信验证码

前言

短信验证码是通过发送验证码到手机的一种有效的验证码系统。主要用于验证用户手机的合法性及敏感操作的身份验证。

第三方短信发送平台有很多种,各个平台有各自的优缺点,在选择的时候可以根据自己的具体实际情况定夺。此篇文章讲述了Springboot集成SMS发送短信。

准备

  1.用户注册。百度SMS短信通,进入官方页面,注册账号;官网:http://sms.webchinese.cn/

  
讯享网

  2.登陆获取必要信息

  

  3.记录下重要的信息 

   账号 、密码、短信密钥。(编码时必要)   

        绑定手机、邮箱。(账号安全)

编码

1.搭建springboot项目。

2.添加依赖。

<dependencies> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.0.1</version> </dependency> </dependencies> 

讯享网

3.编写工具类。

讯享网package com.wcl.test.utils; import java.util.Random; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.junit.Test; public class SendMessageUtil { private static final String SMS_Url = "http://sms.webchinese.cn/web_api/"; / * @param Uid SMS用户id : lvfang123 * @param Key 接口秘钥:SMS登录可查(非登录密码) * @param sendPhoneNum 短信发送目标号码 * @param desc 短信内容 * @return Integer(1:成功码,其他失败,具体参见注释) */ public static Integer send(String Uid,String Key,String sendPhoneNum,String desc){ HttpClient client = new HttpClient(); PostMethod post = new PostMethod(SMS_Url); post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码 //设置参数 NameValuePair[] data = { new NameValuePair("Uid", Uid), new NameValuePair("Key", Key),//秘钥 new NameValuePair("smsMob", sendPhoneNum), new NameValuePair("smsText", desc) }; post.setRequestBody(data); try { client.executeMethod(post); } catch (Exception e) { e.printStackTrace(); } Header[] headers = post.getResponseHeaders(); int statusCode = post.getStatusCode(); System.out.println("statusCode:" + statusCode); for (Header h : headers) { System.out.println(h.toString()); } String result =""; try { result = new String(post.getResponseBodyAsString().getBytes("gbk")); } catch (Exception e) { e.printStackTrace(); } post.releaseConnection(); return Integer.parseInt(result); } / * -1 没有该用户账户 -2 接口密钥不正确 [查看密钥]不是账户登陆密码 -21 MD5接口密钥加密不正确 -3 短信数量不足 -11 该用户被禁用 -14 短信内容出现非法字符 -4 手机号格式不正确 -41 手机号码为空 -42 短信内容为空 -51 短信签名格式不正确接口签名格式为:【签名内容】 -6 IP限制 大于0 短信发送数量 以上作为补充 */ public static String getMessage(Integer code){ String message; if(code > 0 ) { message = "SMS-f发送成功!短信量还有" + code + "条"; }else if(code == -1){ message = "SMS-没有该用户账户"; }else if(code == -2){ message = "SMS-接口密钥不正确"; }else if(code == -21){ message = "SMS-MD5接口密钥加密不正确"; }else if(code == -3){ message = "SMS-短信数量不足"; }else if(code == -11){ message = "SMS-该用户被禁用"; }else if(code == -14){ message = "SMS-短信内容出现非法字符"; }else if(code == -4){ message = "SMS-手机号格式不正确"; }else if(code == -41){ message = "SMS-手机号码为空"; }else if(code == -42){ message = "SMS-短信内容为空"; }else if(code == -51){ message = "SMS-短信签名格式不正确接口签名格式为:【签名内容】"; }else if(code == -6){ message = "SMS-IP限制"; }else{ message = "其他错误"; } return message; } / * 随机生成6位验证码 * @return */ public static String getRandomCode(Integer code){ Random random = new Random(); StringBuffer result= new StringBuffer(); for (int i=0;i<code;i++){ result.append(random.nextInt(10)); } return result.toString(); } @Test public void testSendMessage(){ //SendMessageUtil.send("SMS账户","接口秘钥","目标号码","发送内容"); Integer resultCode = SendMessageUtil.send("疾风*","d41d8cd98f","159","验证码:"+getRandomCode(6)); System.out.println(SendMessageUtil.getMessage(resultCode));   } }

4.测试。

//SendMessageUtil.send("SMS账户","接口秘钥","目标号码","发送内容"); 使用junit测试,正确填写send方法的参数。无论成功失败都会返回响码,根据响应码判断是否成功,如果失败也可查出失败原因。
小讯
上一篇 2025-03-28 08:23
下一篇 2025-02-24 11:36

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/11803.html