短信宝接入发送短信实测 Java

news/2024/10/30 20:38:08/

1丶注册短信宝

2 代码分为授权跟参数实体

授权实体

@Data
public class SmsBaoAuthorization {//http://api.smsbao.com/sms    private String url;//在短信宝注册的用户名private String username;//在短信宝注册的密码    private String password;
}

参数实体

@Data
public class SmsBaoMsgData {private String phone;//签名放入内容中 也可单独作为参数拼接 例如:【万千购】您的验证码是1234private String content;}
调用接口
@Slf4j
@Component
public class SmsBaoSendMsgHandle {public String sendMsgOne(SmsBaoAuthorization smsBaoAuthorization, SmsBaoMsgData smsBaoMsgData) {String state = SMSBaoUtil.send(smsBaoAuthorization, smsBaoMsgData);return state;}public void sendMsgBatch(SmsBaoAuthorization smsBaoAuthorization, List<SmsBaoMsgData> smsBaoMsgDatas) {log.info("发短信");for (SmsBaoMsgData smsBaoMsgData : smsBaoMsgDatas) {String state = SMSBaoUtil.send(smsBaoAuthorization, smsBaoMsgData);}}}

短信宝发送工具类

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class SMSBaoUtil {public static String url;public static String username;public static String password;public static void setUrl(String url) {SMSBaoUtil.url = url;}public static void setUsername(String username) {SMSBaoUtil.username = username;}public static void setPassword(String password) {SMSBaoUtil.password = password;}public static String send(SmsBaoAuthorization smsBaoAuthorization, SmsBaoMsgData smsBaoMsgData) {StringBuffer httpArg = new StringBuffer();httpArg.append("u=").append(smsBaoAuthorization.getUsername()).append("&");httpArg.append("p=").append(md5(smsBaoAuthorization.getPassword())).append("&");httpArg.append("m=").append(smsBaoMsgData.getPhone()).append("&");httpArg.append("c=").append(encodeUrlString(smsBaoMsgData.getContent(), "UTF-8"));return request(smsBaoAuthorization.getUrl(), httpArg.toString());}public static String send(String phone, String content) {StringBuffer httpArg = new StringBuffer();httpArg.append("u=").append(username).append("&");httpArg.append("p=").append(md5(password)).append("&");httpArg.append("m=").append(phone).append("&");httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));String result = request(url, httpArg.toString());return result;}public static String send(String url, String username, String password, String phone, String content) {StringBuffer httpArg = new StringBuffer();httpArg.append("u=").append(username).append("&");httpArg.append("p=").append(md5(password)).append("&");httpArg.append("m=").append(phone).append("&");httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));String result = request(url, httpArg.toString());return result;}public static String md5(String plainText) {StringBuffer buf = null;try {MessageDigest md = MessageDigest.getInstance("MD5");md.update(plainText.getBytes());byte b[] = md.digest();int i;buf = new StringBuffer("");for (int offset = 0; offset < b.length; offset++) {i = b[offset];if (i < 0)i += 256;if (i < 16)buf.append("0");buf.append(Integer.toHexString(i));}} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return buf.toString();}public static String encodeUrlString(String str, String charset) {String strret = null;if (str == null)return str;try {strret = java.net.URLEncoder.encode(str, charset);} catch (Exception e) {e.printStackTrace();return null;}return strret;}public static String request(String httpUrl, String httpArg) {BufferedReader reader = null;String result = null;StringBuffer sbf = new StringBuffer();httpUrl = httpUrl + "?" + httpArg;try {URL url = new URL(httpUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setRequestMethod("GET");connection.connect();InputStream is = connection.getInputStream();reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));String strRead = reader.readLine();if (strRead != null) {sbf.append(strRead);while ((strRead = reader.readLine()) != null) {sbf.append("\n");sbf.append(strRead);}}reader.close();result = sbf.toString();} catch (Exception e) {e.printStackTrace();}return result;}
}

在使用类里面调用

1注入调用类

@Autowired private SmsBaoSendMsgHandle smsBaoSendMsgHandle;

2装配授权参数

SmsBaoAuthorization smsBaoAuthorization = new SmsBaoAuthorization();
3装配消息参数

SmsBaoMsgData smsBaoMsgData = new SmsBaoMsgData();

4 发送

sendResult=smsBaoSendMsgHandle.sendMsgOne(smsBaoAuthorization,smsBaoMsgData);

效果示例

 


http://www.ppmy.cn/news/312089.html

相关文章

飞信好友发短信

网上看到有网页版的飞信&#xff0c;http://fetionlib.appspot.com/可以添加好友&#xff0c;群发和定时发送短信给飞信好友&#xff0c;还开放了API接口供程序调用&#xff0c;可以用它来监控机器是否正常服务定期给管理员发短信&#xff0c;或者小规模的网站给会员发短信之类…

电信smgp3.0短信发送问题

smgp3.0短信 问题&#xff0c;返回错误码46 怎么解决 从网上下载的smgp的jar &#xff0c;最终返回的status错误码是46 非法的发送方号码 &#xff0c;求教怎么解决 SMGP3 smgp new SMGP3(); int socketID smgp.SMGP_Connect("135.0.69.90", 8891, "MSSFXPT&…

Python自动发短信

摘要&#xff1a;最近开发微信自动回复小程序&#xff0c;需要用到一个好友发消息自动短信通知我的功能&#xff0c;因此写了这个程序。主要使用Twilio网站提供的短信功能。 实现起来也是非常方便的。 作者博客地址&#xff1a;http://www.yooongchun.cn/ 首先到官网申请账号&a…

用计算机发短信,怎样发短信的步骤_教你如何使用电脑发短信!

",1)"> 教你如何使用电脑发短信! 今天我教大家一招怎样通过电脑跟别人收发信息。 首先我要说明几点 1我今天告诉大家的方法可以同时在电脑上收发信息也就是跟聊QQ一样的和别人发短信不需要触摸手机了~!超级方便 2发短信产生的费用跟你使用手机产生的费用一样也就是…

互发短信之SmsManager

短信管理器 &#xff1a; SmsManager 1. 在 Android 2.0 以前 应该使用 android.telephony.gsm.SmsManager 之后应该用 android.telephony.SmsManager; 2. 获取系统默认的短信管理器 SmsManager smsManager SmsManager.getDefault(); 3. 按照每条短信最大字数来拆分短信 …

tp6个人实现短信发送注册

public function loginDo() {$data input();$codeCache::get($data[tel]);//用表单输入的验证码做对比if ($code ! $data[code]) {return json([code > 500, msg > 验证码错误, data > ]);}$data[name]input(tel);$data[pwd]123456;\app\admin\model\User::create($d…

使用飞信定时给好友发短信

飞信的定时短信功能应该在2008年底都推出了吧&#xff0c;园子一直也没有用过。今天试了下发现非常好用&#xff0c;本文简单分享下飞信定时短信的使用方法。 定时发送短信的好处 在朋友生日的时候&#xff0c;有时候为了把发送祝福的时间卡在晚上零点零分而费了不小功夫。现…

7000条短信接口!福利赠送!

感谢支持浪杉博客&#xff01;记录时间2019.1.23 以下是7000条短信接口 http://id.ourgame.com/mobilepassport!getMobileYzm.do?passport http://id.ourgame.com/mobilepassport!getMobileYzm.do?passport http://hd.jiedaibao.com/promotion/send-verify-code?icodeGQ9KB…