Visual C # Writer Programs for implementing POP3 1. Open vs.net 2003.
2. Create a new WinForm Application.
3. Add a namespace
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Text; using System. IO;
4. Main code
In order to prevent the interface from being dead when the server is connected, I have another thread.
Private void button1_click (object sender, system.eventargs e) {thread mythread = new thread (New Threadstart (Treadget)); Mythread.Start ();
PRIVATE VOID TREADGET () {ConnectToServer (); LogonServer (); getemaillist ();
// Connect the server private void connectionToServer () {this.myclient = new tcpclient (); try {this.myclient.connect (this.servernametextBox.text, 110); this.MessageListBox.Items.Add ("Connection Server Success") ; // Initialize the network stream, data is read out through this object (this.myclient.getStream (); sr = new streamreader (ns); sw = new streamwriter (ns);} catch (exception ex) {this. MessageListBox.Items.add (ex.Message); User " this.nametextBox.text); sw.flush ();
Result = sr.readline (); if (Result.Substring (0, 3) == "- ER") {this.MessageListBox.Items.add ("Without this user name"; return;}
SW.WRITELINE ("Pass" this.PassTextBox.text); sw.flush ();
Try {result = sr.readline ();} catch (ioException ooex) {this.MessageListBox.Items.Add (ioEx.Message); Return;}
IF (Result.Substring (0, 4) == "- Err") {this.MessageListBox.Items.Add ("Unable to log in, possibly make username password error!"); return;}
This.MessageListBox.Items.Add ("Login Success");}
// Get mailing list private void getemaillist () {string from = null; string subject = null; sw.writeline ("stat"); sw.flush ();
Result = sr.readline (); // MessageBox.show (Result); string [] NumMessage = Result.Split ('); int totalnum = convert.Toint32 (NumMessage [1]); if (Totalnum> 0) this .MessageListBox.Items.add ("You have" Totalnum.toString () "Mail"); else this.MessageListBox.items.add ("There is no email in the mailbox"); for (int i = 1; i <= TotalNum; i ) {sw.writeLine ("TOP" i.toT7tring () "0"); SW.FLUSH (); result = sr.readline ();
While (true) {result = sr.readline (); if (Result == ") Break; if (Result.Length> 4) {if (Result.Substring (0, 5) ==" from: ") From = result; if (result.substring (0,8) == "Subject:") Subject = Result;
}} This.listbox1 .Items.add (i.tostring () " from " " Subject);}}
Private void button2_click (object sender, system.eventargs e) {this.listbox1.items.clear ();
Reference
Web Mail is charged with the POP3 protocol, and understanding the POP3 protocol helps us to deepen the understanding of the mail system. And using the POP3 protocol, you can also directly Telnet to the mail server.
Generally, Telnet POP3 Server 110 can be used with these commands, case in cases, excluding the password itself, pay attention to not let the password return, etc. After verification, it allows back to look back.
User username user approved
PASS Password Approved Performance Status Conversion
APOP NAME, DiGest recognizes a safe transmission password, and executes the successfully caused state conversion, see RFC 1321.
STAT processing request server returns the mailbox statistics, such as the number of emails, total number of mail characters
UIDL N Processing Server Returns the unique identifier for this specified message, if not specified, return all.
List N Processing Server Returns the size of the specified message, etc.
Retrn Processing Server Returns All Texts
Dele N Processing Server tag delete, the quit command is executed when it is executed
RSET Process Undo All Dele Commands
Top N, M processing returns the first M line content of N-mail, m must be a natural number
NOOP Handling Server Returns a positive response
Quit Client wants to end the session. If the server is in the "process" status, now enter the "Update" status, delete those messages marked into deletion. If the Server is in the "recognized" state, the Server does not enter the "Update" status when the session is ended.
A complete acceptor example using Telnet is as follows: