POP3 Authorization

Hi,

      I have been working on this code which will access a pop3 server and thus retrieve the emails. I have been working around a tutorial.  I can connect to the server,

it returns +OK

But when I supply the username and password for the account it does not allow me access. I am supplying the correct Information as I can access this email box with Outlook Express with the same username and password.

I type in the pop3 server name and use PORT 110 (POP3) <--------Everything is successful apart from the way I send the username and password.

Anyways here is my code, thank you in advance.

    protected void Button1_Click(object sender, EventArgs e)
    {
        OutPut.InnerHtml = "";
        try
        {
            TcpClient AndyTcpCLient = new TcpClient();
            AndyTcpCLient.Connect(txtServer.Text, Convert.ToInt32(DropDownList1.SelectedValue));
            NetworkStream netStream = AndyTcpCLient.GetStream();
            StreamReader strReader = new StreamReader(netStream);
            if (AndyTcpCLient.Connected)
            {
                OutPut.InnerHtml += "You have Connected <br />";
                OutPut.InnerHtml += strReader.ReadLine() + "<br />";
                byte[] WriteBuffer = new byte[256];

                ASCIIEncoding enc = new ASCIIEncoding();
                WriteBuffer = enc.GetBytes("USER xxxxxxxx \r\n");
                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                //OutPut.InnerHtml += strReader.ReadLine()+"<br />";
                WriteBuffer = null;
                WriteBuffer = enc.GetBytes("PASS xxxxxxxx \r\n");
                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                OutPut.InnerHtml += strReader.ReadLine() + "<br />";
                //WriteBuffer = enc.GetBytes("LIST\r\n");
                //                netStream.Write(WriteBuffer, 0, WriteBuffer.Length);
                OutPut.InnerHtml += strReader.ReadLine() + "<br />";

            }

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }

    }
LVL 20
REA_ANDREWAsked:
Who is Participating?
 
elticConnect With a Mentor Commented:
I think you have to wait for the server to response. Here's a very dirty little hack,
but if I where you, I would use that one at codeproject:  --> http://www.codeproject.com/csharp/POP3Library.asp

using System.Net;
using System.Net.Sockets;

static void Main(string[] args)
        {
            TcpClient pop = new TcpClient();
            pop.Connect("pop.web.de", 110);
            try
            {
                NetworkStream ns = pop.GetStream();
                string infoText = getAnswer(ns);
                Console.WriteLine(infoText);

                sendCommand(ns, "USER xxxxxx\n");
                string answer = getAnswer(ns);
                Console.WriteLine(answer);
               
                /// If answer is OK send pass
                Regex regexCommandOK = new Regex(@"^\+OK");
                if (regexCommandOK.IsMatch(answer))
                {
                    Console.WriteLine("Username OK");
                    sendCommand(ns, "PASS xxxxx\n");
                    answer = getAnswer(ns);
                    Console.WriteLine(answer);

                    /// If password is OK, get list of emails
                    if (regexCommandOK.IsMatch(answer))
                    {
                        Console.WriteLine("Password correct");
                        sendCommand(ns, "LIST\n");
                        string emailList = getAnswer(ns);
                        Console.WriteLine(emailList);
                    }
                    else
                    {
                        Console.WriteLine("Wrong password");
                    }
                }
                else
                {
                    Console.WriteLine("Wrong username");
                }
            }
            finally
            {
                pop.Close();
            }
            Console.ReadLine();
        }

        private static string getAnswer(NetworkStream ns)
        {
            bool commandEndFound = false;
            string answer = String.Empty;
            int answerByte = ns.ReadByte();
            while ((!commandEndFound) && (answerByte >= 0))
            {
                char answerChar = (char)answerByte;
                answer += answerChar;
                if (answerChar == '\n')
                {
                    commandEndFound = true;
                }
                else
                {
                    answerByte = ns.ReadByte();
                }
            }
            return answer;
        }

        private static void sendCommand(NetworkStream ns, string command)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(command);
            ns.Write(buffer, 0, buffer.Length);
        }
0
 
REA_ANDREWAuthor Commented:
thanks I will try this.

This code looks challenging too, so thanks for that As i am learning C# I want to be in at the Deepend :-)  .

Will get back to you with the results. Thanks again

Andrew
0
All Courses

From novice to tech pro — start learning today.