Using C # to implement POP3 mail receiving program

Source: Internet
Author: User
Tags empty final implement interface mail connect first row client
Microsoft's Vs.net development tool has been launched for some time, and the new language, C #, is becoming familiar and accepted. As a new language, C # is believed to have the incomparable superiority of the traditional language, especially in the network application, the developer feels the powerful function of C #. So this article through the use of C # to implement a POP3 protocol based on the Mail receiver program to show you the function of C # Network programming is powerful, but also to introduce the POP3 protocol based on the principle of e-mail reception. Let me first introduce you to the basics of email reception:The first is the connection between the client and the server. However, before the client connects to the server, note that the port is set to the default number 110th for the POP3 protocol. After the client connection server succeeds, the server returns the following information: +ok ... The character +ok is the return information of the POP3 protocol. Its response information is not as rich and varied as the SMTP protocol, with only two: +ok or-err. Where +ok indicates a successful connection, while-err indicates that the connection failed. Next, the client enters user < username > The command tells the server your username. Note that some servers distinguish between uppercase and lowercase letters. After the server returns +OK, the client enters the pass < password > server returns +OK, and returns some statistics on the mailboxes, such as: +ok 1 message (s) [1304 byte (s)] The different server returns the same information format, So we can use the Stat command to view the situation of the mailbox. The stat command responds with two digits, representing the number of messages and the size of the message. If there is a letter in the mailbox, you can use the RETR command to get the body of the message. The format of the RETR command is: RETR < message number > If the first row of the returned result is +ok information, success is indicated. The second line is the body of the message. The last line, like the SMTP protocol, is a separate English period that represents the end of a message. Save the message to use the DELE command to delete the mail in the mailbox, otherwise the original message will continue to remain on the server, once the mail is a lot, your mailbox will explode. The format of the DELE command is: DELE < message number > If you delete the error, you can use the RSet command to recover all messages that have been deleted. The condition is that you have not quit, once withdrew, that all bye bye. When all is done, enter the QUIT command to exit the POP3 server. ImplementAbove, I briefly introduced to you the basic process and principles of POP3 mail reception, the following is a use of the principle of the implementation of the Simple Mail receiving program. With an understanding of the fundamentals, programming work becomes quite easy. In this program, I mainly used two classes: TcpClient class and NetworkStream class. The TcpClient class is a very important class that uses C # for network programming, which provides a simple way to connect, send and receive data through the network, thus realizing the simplification of the network programming greatly. The NetworkStream class implements the standard. NET Framework flow mechanism for sending and receiving data through network sockets, which supports synchronous and asynchronous access to network data streams and is an important part of network communication. Here I first give the program the final effect of the operation, as shown here:

the specific process steps are as follows: First step:Open Vs.net, create a new project, select "Visual C # project" In the project type, select "Windows Application" in the template, project name may be "Mailreceiver", and finally click "OK" button. Step Two:Layout the main interface. First add the following controls to the form: Six label controls, four TextBox controls, one RichTextBox control, one CheckBox control, one ListBox control, and three button controls. The properties of each control are set as shown in the following table: Form1 (main form) Text Property POP3 Mail Receive program MaximizeBox Property False Label1 Text Property POP3 server address: TextAlign Property MiddleRight Label2 Text Property User name: TextAlign Property MiddleRight Label3 Text Property Password: TextAlign Property MiddleRight Label4 Text Property Information: TextAlign Property Middleleft Label5 Text Property message number: TextAlign Property Middleleft Label6 Text Property Status: TextAlign Property Middleleft Popserver, Username, Password, Mailnum (TextBox control) Text Property (all empty) PasswordChar Properties of Password * Message (RichTextBox control) Text Property (empty) backupchbox (CheckBox control) Text Property keep backups on the mail server Status (ListBox control) ItemHeight Property of Connect (Button control) Text Property Connection FlatStyle Property Flat Disconnect (Button control) Text Property Disconnect FlatStyle Property Flat Enabled Property False Retrieve (Button control) Text Property Receive mail FlatStyle Property Flat Enabled Property FalseOther properties can be default values. After you have set the above control properties, make a reasonable layout of the main form, you can get the final interface diagram as follows: Step Three:Code writing. First, because some of the important classes in the program that are used in network programming, such as: TcpClient, NetworkStream, the following namespaces (Namespace) need to be added at the beginning of the program: using System.Net; using System.Net.Sockets; using System.IO;Next, add some of the following public data members to our class: Public tcpclient Server; Public NetworkStream NETSTRM; Public StreamReader RDSTRM; publicstring Data; publicbyte[] szdata; publicstring CRLF = "\ r \ n";Finally, the three-button message response function, which is the main part of the program, is the three function. The message response function of the Connect button completes the connection to the POP3 server and logs in according to the username and password. If all goes well, then send the stat command to the server to obtain information about the message in the mailbox: the number and size of the messages, and finally complete the connection. Its function is implemented as follows: List1 the message response function of the Disconnect button terminates the connection to the mail server by sending the QUIT command to the server, and the function is simple, as follows: List2 Finally, The message response function of the Retrieve button obtains the corresponding message according to the input of the user in the message number text box, the command sent to the server is RETR, and the message is judged according to the information returned. If the first character of the returned message is "+", it means +ok, or success, or "-", which means-err or failure. In the case of success, if the user chooses "keep backup on the mail server", then the backup is not removed from the server after receiving the message, otherwise the DELE command is used for the corresponding deletion. The function implementation is as follows: List3 The last step is to save your labor, then compile, build and run, the final diagram I have given at the beginning of the article, it is OK. So far, we have completed all the work of the POP3 Mail receiving program. From this, it is not difficult to find that using C # to complete some network practical programming is very easy. As long as we have mastered the principles, the specific programming work becomes very easy and methodical. Even if you didn't know anything about the implementation of POP3 Mail, I think after reading this article, you will have at least a general idea of it. However, it should be pointed out that this is only a very simple example, if you want to apply in practice, it needs to be greatly improved, interested readers may wish to try. Source code file is Mailreceiver.rar

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.