C # Use Web service to send text messages

Source: Internet
Author: User
It is a complicated task for many people to send short messages through programming. Currently, the general solution is to connect computers with mobile phones, you can write text messages related to mobile phone programming languages. Program This method is very difficult for the average person, because this method requires not only many necessary hardware devices, but also the knowledge of mobile phone programming. This article will discuss another simple implementation method for sending short messages through Visual C #. This method is much simpler than we think in the implementation process, you only need to have Visual C # general programming basics and make sure your computer is connected to the Internet. The following describes in detail the implementation process of Visual C # Sending Short Messages.

I. Principle of sending short messages in Visual C:

I think when many readers see the topic of this article, they will surely think that the content of this article must be very profound, and the author must know the gateway address of the text message sent by China Telecom, but this is not the case. This article uses a ready-made resource and a web serviec that can send text messages. This Web Service is a Web service provided by Sina, which can be directly called by users to send short messages. The service address is http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl. This service provides a method for Sending Short Messages "sendxml ". The syntax format of this method is as follows:

String sendxml (carrier, userid, password, mobilenumber, content, msgtype)

The six parameters in the sendxml method are of the string type, and the return value of the sendxml method is also of the string type.

The following describes the six parameters in the sendxml method:

· Carrier: Carrier name. This parameter has no specific requirements when used. You can enter this parameter at will, and the entered string will not be displayed on the recipient's mobile phone.

Userid: the mobile phone number registered on Sina, the address used to register the mobile phone is: http://sms.sina.com.cn, the specific registration method will be described below.

· Password: the password provided by Sina after you successfully register your mobile phone on Sina.

· Mobilenumber: the mobile phone number of the recipient.

· Content: content of the short message to be sent.

· Msgtype: the type of Short Message sent. Because the message sent in this article is not a MMS Message, enter "text ".

To call this web service, you only need to add a web reference directly in the Visual C # development environment and enter this address. Then, you can use the sendxml method in this web service to send short messages. Of course, machines that send short messages must be connected to the Internet.

2. Register a mobile phone on Sina.:

Follow these steps to register a mobile phone on Sina:

1. Open your browser and enter Sina wireless address: http://sms.sina.com.cn in the address bar .. In the upper left corner of the browser, you can see the 01 interface:


Figure 01: xinlang registration mobile phone page

After entering the mobile phone number, click "log in" in Figure 01. If your mobile phone is not in Sina, the message 02 is displayed.


Figure 02: Phone registration page 2

2. enter the corresponding information according to the options shown in Figure 02, and click "log on to mobile Dream". If the registration is successful, alibaba Cloud mobile will send an 8-bit password to your registered mobile phone to get the interface shown in Figure 03.


Figure 03: page 3 of Sina registration mobile phone

This password must be retained. The password must be used to send the text message below.

Iii. Environment for program design, debugging, and running in this article:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. NET 2003 enterprise build version,. NET Framework SDK version 4322.
Iv. Visual C # specific steps for sending short messages:

Visual C # The Key to sending short messages is to reference the Web service provided by Sina. Call the sendxml method of this service. The following describes how to use Visual C # To reference web service to send short messages:

1. Start Visual Studio. NET.

2. Select File, new, and project. The new project dialog box is displayed.

3. Set project type to Visual Basic Project ].

4. Set template to Windows application ].

5. Enter text message in the name text box ].

6. in the location text box, enter E: \.. Net project, and then click OK. the. Net project "directory generates a folder named" SMS "and creates a project file named" SMS.

7. set Visual Studio. switch the current window of net to the form1.cs window, drag the following components to form1 form from the Windows Forms components tab in the toolbox, and perform the corresponding operations:

Four label components.
Four textbox components.

A button component sends short messages. After the button component is dragged into the form1 design form, double-click it, the system will generate the corresponding processing of the click event of this component in the form1.cs file respectively.Code.

8. Switch the current window of Visual Studio. net to the code editing window of form1.vb and replace the code corresponding to the initializecomponent process in form1.cs with the following code. The following code serves to initialize the components added to the form:

Private void initializecomponent ()
{
This. textbox1 = new system. Windows. Forms. Textbox ();
This. textbox2 = new system. Windows. Forms. Textbox ();
This. textbox3 = new system. Windows. Forms. Textbox ();
This. button1 = new system. Windows. Forms. Button ();
This. label1 = new system. Windows. Forms. Label ();
This. label2 = new system. Windows. Forms. Label ();
This. label3 = new system. Windows. Forms. Label ();
This. label4 = new system. Windows. Forms. Label ();
This. textbox4 = new system. Windows. Forms. Textbox ();
This. suspendlayout ();
This. textbox1.location = new system. Drawing. Point (144, 16 );
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (184, 21 );
This. textbox1.tabindex = 0;
This. textbox1.text = "";
This. textbox2.location = new system. Drawing. Point (144, 69 );
This. textbox2.name = "textbox2 ";
This. textbox2.passwordchar = ''''''''*'''''''';
This. textbox2.size = new system. Drawing. Size (184, 21 );
This. textbox2.tabindex = 1;
This. textbox2.text = "";
This. textbox3.location = new system. Drawing. Point (144,122 );
This. textbox3.name = "textbox3 ";
This. textbox3.size = new system. Drawing. Size (184, 21 );
This. textbox3.tabindex = 2;
This. textbox3.text = "";
This. button1.location = new system. Drawing. Point (152,256 );
This. button1.name = "button1 ";
This. button1.size = new system. Drawing. Size (80, 32 );
This. button1.tabindex = 4;
This. button1.text = "send ";
This. button1.click + = new system. eventhandler (this. button#click );
This. label1.location = new system. Drawing. Point (56, 24 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (88, 16 );
This. label1.tabindex = 5;
This. label1.text = "registered mobile phone number :";
This. label2.location = new system. Drawing. Point (88, 77 );
This. label2.name = "label2 ";
This. label2.size = new system. Drawing. Size (72, 16 );
This. label2.tabindex = 6;
This. label2.text = "Password :";
This. label3.location = new system. Drawing. Point (56,128 );
This. label3.name = "label3 ";
This. label3.size = new system. Drawing. Size (96, 16 );
This. label3.tabindex = 7;
This. label3.text = "target mobile phone number :";
This. label4.location = new system. Drawing. Point (96,176 );
This. label4.name = "label4 ";
This. label4.size = new system. Drawing. Size (72, 16 );
This. label4.tabindex = 8;
This. label4.text = "content :";
This. textbox4.location = new system. Drawing. Point (144,175 );
This. textbox4.multiline = true;
This. textbox4.name = "textbox4 ";
This. textbox4.size = new system. Drawing. Size (184, 48 );
This. textbox4.tabindex = 3;
This. textbox4.text = "";
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (410,303 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textbox4 );
This. Controls. Add (this. textbox3 );
This. Controls. Add (this. textbox2 );
This. Controls. Add (this. textbox1 );
This. Controls. Add (this. label4 );
This. Controls. Add (this. label3 );
This. Controls. Add (this. label2 );
This. Controls. Add (this. label1 );
This. formborderstyle = system. Windows. Forms. formborderstyle. fixedsingle;
This. maximizebox = false;
This. Name = "form1 ";
This. Text = "Visual C # SMS sending ";
This. resumelayout (false );
}

Now, the interface design and functional implementation preparations for the [SMS] project are complete, as shown in Figure 04:


Figure 04: design page of the [SMS] project

9. Select Solution Explorer for SMS and right-click reference. The add web reference dialog box is displayed. In this dialog box, enter "http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl" in the URL text box and click the go to button to get the page shown in Figure 05:


Figure 05: Web reference page added in the text message Project

Click Add reference in Figure 04 to add the Web service provided by Sina.

10. set visual stuido. switch the current window of net to the code editing window of form1.cs, and replace the code corresponding to the click event of button1 in from1.cs with the following code, the following code calls the sendxml method provided in the introduced Web service to send short messages to a specified mobile phone:

Private void button#click (Object sender, system. eventargs E)
{
SMS .cn.com. Sina. smsinter. smsws = new SMS .cn.com. Sina. smsinter. smsws ();
String result = ws. sendxml ("Sina", textbox1.text, textbox2.text, textbox3.text, textbox4.text, "new ");
MessageBox. Show (result );
}

11. Now, after the above steps are correctly executed, all the work of the text message project has been completed. Click F5 to run the program. after entering the register mobile phone number, password, target mobile phone number, and content, click send, the program sends a short message to the specified mobile phone number.

V. Summary:

This Visual C # Short Message sending method described in this article. The key to solving this problem is to reference the web service and call the method in this web service. In the full text, is this method very simple. Finally, we would like to remind you that the use of such short messages is not a free lunch. For the billing standard, refer to the relevant instructions on the Sina Wireless Network Station. Each message is charged for a dime, fees are charged from the phone number registered on Sina. When using the method described in this article to send short messages, there is usually a delay after sending is completed. This is because the backend uses the message queue mechanism, but this delay generally takes only a few seconds.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.