验证码发送手机短信功能
1.需要在短信服务器购买平台(此处以创蓝云通讯为例:253.com)
2.购买拿到用户名以及密码后就可以写代码开发了;
3.贴完整代码如下:(在springMvc下开发的)
创建实例类:
package net.shopxx.entity;
import java.util.Random;
/**
*
* @author tianyh
* @date 2017年4月15日 下午3:41:44
* @Title: SmsSingleRequest
* @ClassName: SmsSingleRequest
* @Description:普通短信发送
*/
public class SmsSendRequest {
/**
* 用户账号,必填
*/
private String account;
/**
* 用户密码,必填
*/
private String password;
/**
* 短信内容。长度不能超过536个字符,必填
*/
private String msg;
/**
* 机号码。多个手机号码使用英文逗号分隔,必填
*/
private String phone;
/**
* 定时发送短信时间。格式为yyyyMMddHHmm,值小于或等于当前时间则立即发送,默认立即发送,选填
*/
private String sendtime;
/**
* 是否需要状态报告(默认false),选填
*/
private String report;
/**
* 下发短信号码扩展码,纯数字,建议1-3位,选填
*/
private String extend;
/**
* 该条短信在您业务系统内的ID,如订单号或者短信发送记录流水号,选填
*/
private String uid;
public SmsSendRequest() {
}
public SmsSendRequest(String account, String password, String msg, String phone) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
}
public SmsSendRequest(String account, String password, String msg, String phone, String sendtime) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
this.sendtime=sendtime;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSendtime() {
return sendtime;
}
public void setSendtime(String sendtime) {
this.sendtime = sendtime;
}
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public String getExtend() {
return extend;
}
public void setExtend(String extend) {
this.extend = extend;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public static String createRandNum() {
Random random = new Random();
StringBuffer sb = new StringBuffer();
for(int i = 0; i <= 5; i++) {
String s = random.nextInt(10) + "";
sb.append(s);
}
return sb.toString();
}
}
创建实例类:
package net.shopxx.entity;
/**
*
* @author tianyh
* @date 2017年4月15日 下午3:41:59
* @Title: SmsSingleResponse
* @ClassName: SmsSingleResponse
* @Description:普通短信发送响应实体类
*/
public class SmsSendResponse {
/**
* 响应时间
*/
private String time;
/**
* 消息id
*/
private String msgId;
/**
* 状态码说明(成功返回空)
*/
private String errorMsg;
/**
* 状态码(详细参考提交响应状态码)
*/
private String code;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "SmsSingleResponse [time=" + time + ", msgId=" + msgId + ", errorMsg=" + errorMsg + ", code=" + code
+ "]";
}
}
创建实例类:
package net.shopxx.entity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
*
* @author tianyh
* @date 2017年4月15日 下午3:27:08
* @Title: ChuangLanSmsUtil
* @ClassName: ChuangLanSmsUtil
* @Description:HTTP 请求
*/
public class ChuangLanSmsUtil {
/**
*
* @author tianyh
* @date 2017年4月15日 下午3:27:04
* @Title sendSmsByPost
* @Description
* @param path
* @param postContent
* @return String
* @throws
*/
public static String sendSmsByPost(String path, String postContent) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
printWriter.write(postContent);
// flush输出流的缓冲
printWriter.flush();
StringBuilder sb = new StringBuilder();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
// 开始获取数据
BufferedReader br = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
在springMVC中写入的控制层方法:
/**
* 验证码发送手机短信
*/
@RequestMapping("/postIdentifyingCode")
public ResponseEntity<?> postIdentifyingCode(String phone,ModelMap model,HttpServletRequest request, HttpServletResponse response){
Setting setting = SystemUtils.getSetting();
String pswd=setting.getPswd();
String account=setting.getAccount();
HttpSession session = request.getSession();
String smsNew=SmsSendRequest.createRandNum();
// 普通短信地址
String smsSingleRequestServerUrl = "http://vsms.253.com/msg/send/json";
// 短信内容
String msg = "您正在使用手机微信端注册商城用户,如果不是本人操作,请勿泄露验证码,你的验证码是"+smsNew+"";
//手机号码
// phone = "13764187236";
SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg, phone);
String requestJson = JSON.toJSONString(smsSingleRequest);
System.out.println("before request string is: " + requestJson);
String responseNew = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
System.out.println("response after request result is :" + response);
SmsSendResponse smsSingleResponse = JSON.parseObject(responseNew, SmsSendResponse.class);
String time=smsSingleResponse.getTime();
String msgId=smsSingleResponse.getMsgId();
String code=smsSingleResponse.getCode();
String errormsg=smsSingleResponse.getErrorMsg();
System.out.println(time+"?"+msgId+"?"+code+"?"+errormsg);
session.setAttribute("timeNew", time);
session.setAttribute("msgId", msgId);
session.setAttribute("phone", phone);
session.setAttribute("smsNew", smsNew);
System.out.println("response toString is :" + smsSingleResponse);
model.addAttribute("smsCode", smsNew);
return Results.OK;
}
使用的发送短信中的jar包:fastjson-1.2.14.jar
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)