Java sends text messages,
General steps for sending text messages in java
I,Registered users in China Net: This program is implemented through the SMS platform provided by the China Network. New users registered on this platform can enjoy 5 free text messages and 3 MMS messages, which is sufficient for trial and experience. China site: http://sms.webchinese.cn/reg.shtml;
II,Modify text message signature: After the registration is successful, you must first modify the text message signature when logging on to the user, because the text message sent by chinanetcenter cannot be successfully sent without a formal signature. For the prompt information, see:
To modify the text message signature, follow these steps: Modify user information ---> modify user information ---> save information, for example:
III,Modify Verification Code gateway and bind SMS Template: If the developed text message is used to send verification codes, order numbers, and so on, you can contact the Customer Service of China Internet (QQ is convenient and fast) to quickly receive the text message ), modify the verification code gateway and bind the text message template. the variables in the text message template are replaced by x. For details, contact the Customer Service staff of China network, so that the text message can be sent to the user's mobile phone in seconds;
Note: After an SMS template is bound, you can only send messages in seconds when the content of the SMS template is the same as that of the bound SMS template. If the content of the SMS is different, the message will be received for a longer time;
IV,Download the commons-httpclient jar package:
V,Write java code to send text messages:
Public static void main (String [] args) throws Exception {HttpClient client = new HttpClient (); post = new PostMethod ("http://gbk.api.smschinese.cn"); post. addRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = gbk "); // set transcoding NameValuePair [] data = {new NameValuePair ("Uid", "username") in the header file "), // The username new NameValuePair ("Key", "User Key") registered on the China Internet construction sms platform, // The User Key new NameValuePair ("smsMob ", "Mobile Phone Number"), // the mobile phone number to be sent to new NameValuePair ("smsText", "content sent according to the SMS template, the variables in the SMS template can be embodied ")}; // post the text message to be sent. setRequestBody (data); client.exe cuteMethod (post); Header [] headers = post. getResponseHeaders (); int statusCode = post. getStatusCode (); System. out. println ("statusCode:" + statusCode); for (Header h: headers) {System. out. println (h. toString ();} String result = new String (post. getResponseBodyAsString (). getBytes ("gbk"); System. out. println (result); // print the returned message status post. releaseConnection ();}
To view the user's key, follow these steps: Modify the text message key ---> Text Message key, for example:
6. You can use the java code written in step 5 to send text messages and send them to your mobile phone in seconds;
Note the following points:
1. Text message signature must be set: because China's Internet construction regulations, text messages without text message signatures cannot be sent successfully;
2. If you want to implement the second-to-second function of SMS, you must modify the verification code gateway and bind the SMS template, in addition, only when the text message content is exactly the same as the bound text message template (the variables in the template can be instantiated each time, and the rest of the content must be the same) Can the text message be sent in seconds, otherwise, the sent text message cannot be sent in seconds, and it is slower to arrive at the user's mobile phone just like the ordinary text message;