Understand the POP3 protocol, use simple code to monitor POP3 mailbox, or use Telnet directly

zhaozj2021-02-16  102

POP3 protocol points, more detailed information can be viewed RFC1939

(1) The POP3 command consists of a command and some parameters. All commands are composed of a CRLF pair (2) command and parameters consisting of a printable ASCII character, and they consist of a status code and a command that may follow additional information between the Space Space (3) POP3. All responds are also two status codes, "determination" (" OK") and "fail" ("-err"). (5) When all the information is transmitted, the last line is transmitted, including an end character (decimal code 46, that is, ") and a CRLF pair. (6) There are three states, accredited status, processing status, and update status in the POP3 protocol. When the client is connected to the server, once the client provides its own identity and successfully confirmed, that is, the client issues a Quit command after the corresponding operation is transferred, then the client issues a Quit command, then enter the update state, after the update is finally heavy State the state. As shown below

Waiting to connect to identify Quit Commands - | Approve | ----- | Process | ------ | Update | | | _________________________________ | Return

A minimal, successful client-server session process

POP3 Client Pop3 Server Socket () Listen () Connect () -------------------------------> accept () <- ---------------------------- OK Send a confirmation message (enter "confirm" status) Send a user command ------ ------------------------> <------------------------ ----- OK Send a confirmation message Send pass command ----------------------------> <----- ------------------------ OK Send a confirmation message (enter the "operation" status) Send a quit command ----------- ------------------------------------------------------------------- OK Send a confirmation message (enter "update" status) If the customer sends quit in the "confirmation" state, the session does not enter the "update" status can be tested in the command line (c: / telnet pop3) .163.com 110 // Connect to Netease's Mail Server OK Coremail .... // Return OK User 'YourUserName' // Client Send User Name, You need a pair of single quotes pass 'YourPassword' // client on YourUserName, you may need a pair of single quotes OK mm nn // on YourPassword OK MM NN // Number of emails and size stat // Get the status of the mailbox OK XXX XXX / / Return the number of emails and the command list and meaning you can send, from the CHINAUNIX command parameter state description ---------------------------------------------------------- -------------------------------------------------- ---------------------------------------------- User UserName If this command is successful, the pass command is successful.

Will lead to state conversion pass password approved apop name, Digest approved Digest is a summary of MD5 message, Windows system is not supported --------------------------- ------------------------------------------------- ---------------------------------------------- Stat None Processing Request Server Sends a statistics on the mailbox, such as the total number of mailboxes and the total byte number UIDL [MSG #] to process the unique identifier of the message, each identifier of the POP3 session will be the unique list [msg #] process Returns the number of emails and the size of each message Returns all text dele [MSG #] processing server that is identified by the parameter identifier will be deleted by the parameter identifier as deleted, and the Quit Command Executes the RSET NONE processing server will be heavy Set all the messages marked as deleted, used to undo the dele command TOP [MSG #] Processing server will return the front N row content before the mail identified, n must be the positive integer NOOP NONE processing server returns a positive response ----- -------------------------------------------------- -------------------------------------------------- --------------------- Quit None Update Foxmail and Outlook Use POP3 from LinuxForum. NET assume that there are three emails on the server waiting client reception. The difference between Foxmail and OE is to delete an additional marking in Foxmail, while Outlook, etc., then fully marked to delete the final execution Quit command.

Foxmail Outlook ------------------------------------------ RETR 1 Retr 1 dele 1 Retr 2 Retr 2 Retr 3 Dele 2 Dele 1 Retr 3 Dele 2 Dele 3 Dele 3 Quit Quit --------------------------- -------------- Simple C # code can implement the monitor of POP3 mailbox Namespace Sky.mailmonitor {public class pop3 {private const Int bufsize = 1024; // default 1kbytes buffers // server confirmation The status code of the message must be uppercase private const string okflag = " ok"; private const string errflag = "-err"; private int _port = 110; public int port {get {return_port;} set {_port = value ;}} Private int mailcount = 0; public int mailcount {g et {return mailCount;} set {mailCount = value;}} private int mailSpace = 0; public int MailSpace {get {return mailSpace;} set {mailSpace = value;}} private Socket socket = null; public int port {get { Return port;}} // step 1: Connect a POP3 server, I hope to get the OK confirmation message public void connection (String hostname) {// build a socket socket =

new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPHostEntry hostInfo = Dns.GetHostByName (hostname); IPEndPoint endPoint = new IPEndPoint (hostInfo.AddressList [0], Port); socket.Connect (endPoint); string TMP = recvline (); // blocking, wait for ' OK' Console.Writeline (TMP);} // Step 2: Send command word User and Pass public void login (String Username, String Password) {Sendcommand ("User " Username); string tmp = recvline (); console.writeline (" Server Say: {0} ", TMP); Sendcommand (" pass " password); string tmp1 = recvline (); console.writeline (" Server SAY: {0} ", TMP1);} // Step 3: Send a command word stat, request the server to send back statistics on the mailbox, such as the total number of emails and total bytes / / c: stat // s: OK Nn mm // nn is the number of emails, mm is the size public void stat () {sendcommand ("stat"); string tmp = recvline (); tmp = tmp.substring (TMP .Indexof ("") 1); // get the number of emails after ok and a space, the number string tmp1 = tmp.substring (0, tmp.indexof (")). Trim (); mailcount = INT32.PARS (Tmp.Substring (0, Tmp.indexOf ("))). Trim ()); console.writeline (" Server Say: {0} ", TMP); // mailspace = int32.parse (TMP1. Substring (0, tmp1.indexof (")). Trim ());} // list [msg], the MSG parameter is optional, returned to the number of emails and the size of each message, if there is no MSG parameter, the server will Returns all email information // c: list // s: OK 187 9703827 / / 1 31502 //.. Public void list () {sendcommand ("

List "); string tmp = recvline (); console.writeline (" Server Say: {0} ", TMP);} public void dele (string mailnumber) {sendcommand (" dele " mailnumber; string tmp = recvline Console.writeline ("Server Say: {0}", TMP);} public void quit () {sendcommand ("quit"); string tmp = recvline (); console.writeline ("Server Say: {0} ", TMP);} // // Close the open connection and send the quit command word, the quit command does not parameter public void close () {if (socket == null) return; try {quit (); // first invoke quit Method} finally {socket.close ();}}}} private void sendcommand (String command) {command = ""; // must add CRLF to Byte after the word word [] buffer = Encoding.ascii.getbytes (Command.Tochararray ()); // Send buffer int Byte Ssent = socket.send (Buffer, Buffer.Length, 0); if (Bytessent! = Buffer.Length) Throw new exception ("Failed to send request to server");} // Todo: Server Answer Private by Multi-Line string RecvMultiLine () {} // server response private string RecvLine to form line CRLF () {// add a null terminator byte [] buffer = new byte [bufsize 1]; StringBuilder message = new StringBuilder (bufsize ); Int Bytesread; // read buffer for (;;) {bytesRead =

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

New Post(0)