من خلال الشيفرة التالية نتعلم كيفية تسجيل الدخول والخروج من الايميل
عن طريق بروتوكول مكتب البريد
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | /// <summary> /// RFC1939 - Post Office Protocol - Version 3 (POP3) /// <seealso cref="http://www.faqs.org/rfcs/rfc1939" /> /// </summary> class POP3 { Stream stream = null; TcpClient tcpclient = new TcpClient(); bool connected = false; bool isLogedon = false; public bool Connected { get { return connected; } } public bool IsLogedon { get { return isLogedon; } } public POP3(string host, int port, bool ssl) { try { // open port 110 ( the pop3 port ) On the server // catch any error resuting from a bad server name tcpclient.Connect(host, port); // default port 110 if(tcpclient.Connected) { if(true == ssl) { // This is Secure Stream // opened the connection between client and POP Server SslStream sslstream = new SslStream(tcpclient.GetStream()); // authenticate as client sslstream.AuthenticateAsClient(host); //bool flag = sslstream.IsAuthenticated; // check flag stream = sslstream; } else { // opened the connection between client and POP Server stream = tcpclient.GetStream(); } // check that the connection is okay if(getData().StartsWith("+OK")) connected = true; } } catch { } } string getData() { if(!tcpclient.Connected) return string.Empty; byte[] data = new byte[tcpclient.ReceiveBufferSize]; // get the response stream.Read(data, 0, data.Length); // return the response return System.Text.Encoding.Default.GetString(data); } public string SendCmd(string cmd) { if(!connected) return string.Empty; byte[] byteCMD = null; // byte encode the command byteCMD = System.Text.Encoding.ASCII.GetBytes(cmd + "\r\n"); // send the data stream.Write(byteCMD, 0, byteCMD.Length); return getData(); } public bool Logon(string user, string pass) { string ret = string.Empty; ; // make sure you have a connection if(connected) { // refer POP rfc command, there very few around 6-9 command //send the username ret = SendCmd("USER " + user); //if that was successfull, send the password if(ret.StartsWith("+OK")) { ret = SendCmd("PASS " + pass); //if that was successfull set the return flas to true if(ret.StartsWith("+OK")) { return (isLogedon = true); } } } return false; } public string Logoff() { // close the connection isLogedon = false; return (connected ? SendCmd("QUIT") : string.Empty); } } |
اختبار الشيفرة POP3
تحميل الشيفرة POP3 (114)