POP3 programming under .NET

xiaoxiao2021-03-06  74

Someone asked someone on the forum asked how to use .NET to make POP3 programming, in fact, the use of POP3 is very simple, so .NET does not give corresponding classes like SMTP.

Tony said nonsense, the programmer needs to make the code, :)!

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);}} // Verify user name, password private void logonser () {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 ("Without this user name"; return;}

SW.WRITELINE ("Pass" this.PassTextBox.text); sw.flush ();

Try {result = sr.readline ();} catch (ooException ooex) {this.MessageListBox.Items.add (ioEx.Message); return;} if (Result.Substring (0, 4) == "- ERR") {This.MessageListBox.Items.Add ("Unable to log in, it may 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.toTostring () "Mail"); else this.MessageListBox.Items.add ("There is no email in the mailbox");

For (int i = 1; i <= totalnum; i ) {sw.writeline ("top" i.toString () "0"); sw.flush (); result = sreadline ();

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;

}

THISTBOX1 .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 When the Status Conversion APOP Name, Digest recognizes a safe transfer password, and the execution is successful, see RFC 1321. STAT Processing Request Server Returns Mailbox Statistics, such as the number of emails, the total number of bytes UIDL N processing server returns the unique identifier for the specified message, if not specified, return all. List N Processing Server Returns the Size of the Specified Email Equation with the Retr n Processing Server Returns the DELE N process server tag delete, the quit command is really deleted when the RSET process is revoked all dele commands Top N, M returns N number mail The front M line content, m must be a natural number NOOP handle 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: Telnet Pop3Server 110 user username pass **** stat List retr 1 retr 2 ... Dele 1 dele 2 ... Quit

If you need the source program, leave the email address in the comment

转载请注明原文地址:https://www.9cbs.com/read-120114.html

New Post(0)