Home  >  Article  >  Java  >  How to send text messages to mobile phones in Java? How to send mobile text messages

How to send text messages to mobile phones in Java? How to send mobile text messages

青灯夜游
青灯夜游forward
2018-10-22 16:30:4910863browse

The content of this article is to introduce how to send text messages to mobile phones in Java? How to send text messages via mobile phone. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

In daily project development, you will encounter functional requirements for SMS verification codes. Next, we will mainly introduce this function. . .

1. The basic idea of ​​realizing mobile phone SMS verification

1. The user fills in the mobile phone number and clicks the Get Verification Code button (the page can do some processing to check whether the mobile phone number is valid) , initiate a request; after success, the page starts counting down for 60s

2. After receiving the request in the background, verify whether the mobile phone number is valid and registered. If it is invalid or registered, it will return to prompt the user that the mobile phone number is invalid or registered. ; If it is valid and not registered, the verification code will be generated in the background, and relevant data (including mobile phone number and verification code) will be sent by calling the third-party SMS interface. If the verification code is sent successfully, the verification code will be stored in the session or resis, and verification will be set. The code is valid for a period of time, and a prompt will be returned if it fails.

3. After the mobile phone receives the verification code, fill it in on the page within the valid time, and click the function buttons such as registration and login to send the request

4. The background receives the request and gets the page transmission Compare the incoming verification code with the verification code in the session or resis. If they are the same, they pass the verification and you can perform corresponding business processing (registration or login, etc.). Finally, invalidate the verification code in the session or resis. Generally, just leave it blank. Otherwise, it will prompt that the verification code is wrong or expired.

2. Technology for sending text messages

There are currently three main ways:

(1) Use the webservice interface to send mobile text messages. This can be used The webservice provided by sina is used to send, but registration is required;

(2) Use the SMS mao method to send text messages. This method should be more commonly used, provided that hardware equipment needs to be purchased.

(3) Use the SMS text message platform provided by China Net Construction (China Net Construction application account address: http://sms.webchinese.cn/reg.shtml)

In fact, no matter which method, basically It's all the same, just download the demo and configure your own information.

This mainly introduces the SMS text messaging platform provided by China Net Construction to send text messages:

After registering an account, the platform provides 5 free test text messages, and you can download the corresponding jar, demo, and modifications Configuration, development access

commons-logging-1.1.1.jar
commons-httpclient-3.1.jar
commons-codec-1.4.jar

Demo example:

HttpClientUtil tool class: encapsulates the method of sending post/get requests sent by SMS

test class:

import java.util.HashMap;
import java.util.Map;

public class test {    
	//用户名
	private static String Uid = "测试www";    
	//接口安全秘钥
	private static String Key = "d41d8cd98f00b204e990";    
	//手机号码,多个号码如13800000000,13800000001,13800000002
	private static String smsMob = "13800000000";    
	//短信内容
	private static String smsText = "验证码:123456";    

	public static void main(String[] args) {    
		HttpClientUtil client = HttpClientUtil.getInstance();    
        //UTF发送
		int result = client.sendMsgUtf8(Uid, Key, smsText, smsMob);
		if (result > 0) {
			System.out.println("UTF8成功发送条数==" + result);
		} else { 
			System.out.println(client.getErrorMsg(result));
		}
	}
}

Summary: That’s it. The entire content of the article is hoped to be helpful to everyone's study. For more related tutorials, please visit Java video tutorial, java development graphic tutorial, bootstrap video tutorial!

The above is the detailed content of How to send text messages to mobile phones in Java? How to send mobile text messages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete