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
To prevent deadlocks on the interface when connecting to the server, I opened another thread.
Private void button#click (object sender, System. EventArgs e) { Thread myThread = new Thread (new ThreadStart (TreadGet )); MyThread. Start (); }
Private void TreadGet () { ConnectToServer (); LogonServer (); GetEmailList (); }
// Connect to the server Private void ConnectToServer () { This. myClient = new TcpClient (); Try { This. myClient. Connect (this. servernametext box. Text, 110 ); This. messageListBox. Items. Add ("connected to the server "); // Initialize the network stream. The data is read through this object. Ns = this. myClient. GetStream (); Sr = new StreamReader (ns ); Sw = new StreamWriter (ns ); } Catch (Exception ex) { This. messageListBox. Items. Add (ex. Message ); } } // Verify the user name and password Private void LogonServer () { Result = sr. ReadLine (); This. messageListBox. Items. Add (result ); Sw. WriteLine ("USER" + this. nameTextBox. Text ); Sw. Flush ();
Result = sr. ReadLine (); If (result. Substring (0, 3) = "-ER ") { This. messageListBox. Items. Add ("this user name is not available "); Return; }
Sw. WriteLine ("PASS" + this. passTextBox. Text ); Sw. Flush ();
Try { Result = sr. ReadLine (); } Catch (IOException ioex) { This. messageListBox. Items. Add (ioex. Message ); Return; }
If (result. Substring (0, 4) = "-ERR ") { This. messageListBox. Items. Add ("cannot log on, and the user name and password may be incorrect! "); Return; }
This. messageListBox. Items. Add ("Logon successful "); }
// Obtain the email 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 () + "email "); Else This. messageListBox. Items. Add ("No email in mailbox ");
For (int I = 1; I <= totalnum; I ++) { Sw. WriteLine ("top" + I. ToString () + "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 (); } |
References
The Pop3 protocol is used for online mail receiving. Understanding Pop3 helps us to better understand the mail system. You can also telnet to the mail server to receive emails using the Pop3 protocol.
Generally, you can use these commands after telnet Pop3 Server 110. These commands are case-insensitive and do not include the password itself. Be sure not to display the password back. After the verification is passed, the echo is allowed.
User username recognized by the user
Pass password: if the execution is successful, the status is converted.
Apop name, digest recognizes a secure transmission password method. If the password is successfully transferred, the status is converted. For details, see RFC 1321.
Stat processes server send-back mailbox statistics, such as number of mails and total number of bytes
The uidl n processing server returns the unique identifier used for the specified email. If no unique identifier is specified, all emails are returned.
List n processes the size of the specified email returned by the server, etc.
Retr n processes all text returned by the server
Dele n processes the server tag deletion, Which is deleted only when the quit command is executed.
Rset process undo all dele commands
Top n, m processing returns the content of the first m lines of the n mail, and m must be a natural number
Noop processes the server and returns a positive response.
The quit client wants to end the session. If the server is in the "processing" status, it is now in the "Update" status to delete the emails marked as deleted. If the server is in the "approved" status, the server does not enter the "Update" status when the session ends.
A complete example of telnet reception is as follows:
Telnet pop3Server 110
User username
Pass ****
Stat
List
Retr 1
Retr 2
...
Dele 1
Dele 2
...
Quit