Tutorial on how to open and read an email using C# and POP3

For a recent project I found a great tutorial on how to read and manipulate emails using POP3 on Developer Fusion.

Two things you should note when trying this code.

1) POP3 protocol won’t actually delete the email until you call the QUIT command. If your application fails before you call the Disconnect() method, no emails will be deleted.

2) I was unable to view the content of my emails without making a modification to the Response() method. Apparently the buffersize used in the tutorial was not sufficient for the emails I was viewing. The new method I used is posted on the tutorial’s feedback forum by “ddoctor.” It is as follows:

private string Response()
{
   System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

   long buffersize = 1024;
   byte[] serverbuff = new byte[buffersize];
   NetworkStream stream = GetStream();
   int count = 0;
   while (true)
   {
      byte[] buff = new byte[2];
      int bytes = stream.Read(buff, 0, 1 );
      if (bytes == 1)
      {
         // if serverbuffer is full, double its capacity
         if (count == buffersize)
         {
            byte[] temp = new byte[buffersize * 2];

            int i = 0;
            while (i < buffersize)
            {
               temp = serverbuff;
               i++;
            }
            buffersize *= 2;
            serverbuff = temp;
         }

         serverbuff[count] = buff[0];
         count++;

         if (buff[0] == '\n')
         {
            break;
         }
      }
      else
      {
         break;
      };
   };
}
This entry was posted in Web & Software Development and tagged , . Bookmark the permalink.

2 Responses to Tutorial on how to open and read an email using C# and POP3

  1. Rob says:

    I have no problems with Retrieving the message number and UID for it using my own code for POP, but how to I read the data from the UID?

  2. Joyce Holt says:

    I’m new at this.My emails come but Ican’t send out.How do Ifix my email using Smtp and POP3?

Leave a Reply

Your email address will not be published. Required fields are marked *