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

... 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;
      };
   };
}

2 Comments

Leave a Reply

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