In the previous section, we used Java mail to complete the function of sending emails to buyers, and left a function, which is to send text messages to buyers and tell them that the order has been generated. This section mainly introduces how to automatically send text messages to users after payment is completed.
1. Apply for SMS Send Function
It is obvious that we cannot send text messages ourselves, nor do we have the function of making text messages, so we need to rely on third-party operators. However, like China Mobile, China Unicom, and Telecom will not directly provide us with SMS services, but will first provide SMS services to some operators, and then these operators will provide us with them.
There is an operator called "China Net Construction SMS SMS Communication" (official website address). This operator can provide us with SMS services. After entering the official website, you can check the charging standards. Of course, you can get 5 free text messages for the first time, but the free ones may not be too stable, or the sending time is relatively long. It depends on the character, but the paid ones are basically good. His profit model is to get text messages from Mobile, China Unicom or Telecom at a lower price and sell them to users. Of course, there are discount packages. This type of product is generally purchased by large companies or services that need to send text messages to users.
First, we register an account (actually to get the 5 free short messages to test our program). In order to clarify the process, I try to write it as detailed as possible. Please see the following interface:
You can fill in any names and emails, just fill in your own names on your phone, because you will receive the logged-in username and password. After logging in, we can see that there are 5 free text messages that can be sent, as follows:
Friendly tips: Don’t use up 5 free text messages on the first day, and you will send another 5 messages on the second day, and then you won’t send them~
Then we click on the Send SMS in the picture above, and a SMS interface will pop up. We do not want to send SMS, but set some SMS suffixes. That is, after the user receives the SMS, there is a suffix at the end to explain where it comes from, as follows:
After saving, we click on the "SMS API Interface" in the left column, and the API for sending text messages will pop up (the address of the connection is also given here). When we program, we need to refer to the API interface they provide to us, just like the payment function before, using the official interface provided by Yibao. Let's take a brief look:
The above is just some of the content. For details, please click on the link given above. We click on the key in the picture to modify it immediately, mainly to see what the key is, then copy the key and save it. It will be used when writing the program later.
Okay, now the preparations are done and the API is available. Now we can start developing the SMS function.
2. Test whether the third-party server is normal or not
Before developing the SMS function, let’s test whether the server built by China Network is normal. According to the data provided by the SMS API page, we can use UTF-8 encoding to send the interface address:
http://utf8.sms.webchinese.cn/?Uid=User name of this site&Key=Interface security key&smsMob=Mobile phone number&smsText=Verification code: 8888
Let's test whether utf8.sms.webchines.cn is normal. Let’s write a test.jsp page in the project:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <%@ include file="/public/head.jspf" %> </head> <body> <!-- Fill out the parameters yourself--> <a href="http://utf8.sms.webchinese.cn/?Uid=Username of this site&Key=Interface security key&smsMob=Mobile number&smsText=Verification code:8888"> Test server</a> </body></html>
(Friendly reminder: Don’t test multiple times during testing. One time is enough. It mainly depends on whether the server is normal. You have to leave it for later testing several times. You can also write a parameter incorrectly. If a negative number is returned, it means that the server is normal. You can see which negative number represents the error based on the parameters provided by the API page. This is only used for testing. In fact, it cannot be posted like this, because my username and password are also included in the link, which is definitely not possible.)
3. Develop SMS Send Function
###**3.1 Send SMS Process Test** Using the SMS sending function, the following three jar packages are required: (Free download address: http://download.csdn.net/detail/eson_15/9528009) - `commons-codec-1.4.jar` - `commons-httpclient-3.1.jar` - `commons-logging-1.1.1.jar` Next, we will write a normal java class to complete the SMS sending function and test the correctness:
public class SendMessageDemo { public static void main(String[] args) throws Exception { //1. Open the browser HttpClient client = new HttpClient(); //2. How to create a request: get/post PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn/"); //The server address we just applied for //3. Set the requested parameter information post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");//Set transcoding post.setParameter("Uid", "xxxx");//The parameters are the same as above post.setParameter("Key", "xxxx"); post.setParameter("smsMob", "xxxx"); post.setParameter("smsText", "Test!"); //4. Submit the request and return the status code int code = client.executeMethod(post); System.out.println("status code returned by http: " + code);//If 200 is printed, it means it is normal//5. Get the data information returned by the server String result = post.getResponseBodyAsString(); System.out.println("SMS sending result is: " + result);//If you return 1, it means that the sending is normal}} Next, you can run this java program and send it to yourself a text message to test it. My test results are as follows:  ###**3.2 Encapsulate into MessageUtilImpl** Next we will use this function in our own project. We encapsulate the above process into the MessageUtilImpl tool class and extract the interface, as follows:
//The extracted MessageUtil interface public interface MessageUtil { public abstract void sendMessage(String phoneNum, String id);//MessageUtilImpl implementation class @Component("messageUtil")public class MessageUtilImpl implements MessageUtil { @Override public void sendMessage(String phoneNum, String id) { //1. Open the browser HttpClient client = new HttpClient(); //2. How to create a request: get/post PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn/"); //3. Set requested parameter information post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); post.setParameter("Uid", "eson_15"); post.setParameter("Key", "0537729d0b59a7e7358b"); post.setParameter("smsMob", "15216771570"); post.setParameter("smsText", "Test!"); //4. Submit the request and return the status code try { int code = 0; code = client.executeMethod(post); System.out.println("The status code returned by http: " + code); //5. Get the data information returned by the server String result = post.getResponseBodyAsString(); System.out.println("SMS sending result is: " + result); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { post.releaseConnection(); } }}4. Improve payAction
Complete the encapsulation of the tool class. Next, we put the tool class into BaseAction and inject it through the @Resource annotation for use by Action. Below we will improve the backBank() method in the previous payAction, as follows:
@Controller("payAction")@Scope("prototype")public class PayAction extends BaseAction<Object> implements ParameterAware { //Save irrelevant code... public void backBank() { BackData backData = (BackData)model; System.out.println(model); boolean isOK = payService.checkBackData(backData); if(isOK) { //1. Update the order status, the parameters are transmitted in by itself according to the situation in the database, and are used to test forderService.updateStatusById(Integer.parseInt(backData.getR6_Order()), 2); //2. Send email according to the user email address String emailAddress = backData.getR8_MP().split(",")[0]; emailUtil.sendEmail(emailAddress, backData.getR6_Order()); //3. Send mobile phone text message String phoneNum = backData.getR8_MP().split(",")[1]; messageUtil.sendMessage(phoneNum, backData.getR6_Order()); System.out.println("----success!!-----"); } else { System.out.println("----false!!!-----"); } }} The information returned from Yibao is saved in the r6_Order parameter, and the user's email and phone number in the r8_MP parameter. The first is the email and the second is the mobile phone number, separated by commas, so we first need to obtain the user's mobile phone number and then send a text message. OK, the function of sending text messages to the user after payment is completed is completed.
Original link: http://blog.csdn.net/eson_15/article/details/51475431
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.