title: I am getting raw text while reading emails using IMAP
body: <p>I have taken some code from MSDN to read emails using IMAP Client. I have changed little bit code so i can only read unseen email.
  I am writing all response in Richtextbox.</p>
  
  <p>The problems is format of Body text of Email is unreadable while all other text is fine.</p>
  
  <pre><code>    void ReadEmail()
      {
          try
          {
  
              // there should be no gap between the imap command and the \r\n       
              // ssl.read() -- while ssl.readbyte!= eof does not work because there is no eof from server 
              // cannot check for \r\n because in case of larger response from server ex:read email message 
              // there are lot of lines so \r \n appears at the end of each line 
              //ssl.timeout sets the underlying tcp connections timeout if the read or write 
              //time out exceeds then the undelying connection is closed 
              tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);
  
              ssl = new System.Net.Security.SslStream(tcpc.GetStream());
              ssl.AuthenticateAsClient("imap.gmail.com");
              receiveResponse("");
  
  
              username = "charlie@gmail.com";
              password = "********";
  
              receiveResponse("$ LOGIN " + username + " " + password + "  \r\n");
  
              receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n");
  
              receiveResponse("$ SELECT INBOX\r\n");
              receiveResponse("$ UID SEARCH UNSEEN\r\n");
             MatchCollection collection= Regex.Matches(Result,@" (\d{1,4})");
             foreach (Match m in collection)
             { 
             UNREAD_UID.Add(int.Parse(m.Groups[1].Value));
             }
              foreach (int x in UNREAD_UID)
              {
  
                       receiveResponse("$ FETCH " +x + " body[header]\r\n");
                      richTextBox1.Text += Environment.NewLine+"-----------------------------------------------------------------------------------------------------------------------"+Environment.NewLine;
                      receiveResponse("$ FETCH " +x + " body[text]\r\n");
                      richTextBox1.Text += Environment.NewLine + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2" + Environment.NewLine;
                      richTextBox1.Update();
              }
  
  
              //receiveResponse("$ STATUS INBOX (MESSAGES)\r\n");
  
  
            //  int number = 1;
  
  
  
              receiveResponse("$ LOGOUT\r\n");
          }
          catch (Exception ex)
          {
              Console.WriteLine("error: " + ex.Message);
          }
          finally
          {
              if (sw != null)
              {
                  sw.Close();
                  sw.Dispose();
              }
              if (ssl != null)
              {
                  ssl.Close();
                  ssl.Dispose();
              }
              if (tcpc != null)
              {
                  tcpc.Close();
              }
          }
       }
  </code></pre>
  
  <hr>
  
  <pre><code>   void receiveResponse(string command)
      {
          try
          {
              if (command != "")
              {
                  if (tcpc.Connected)
                  {
                      dummy = Encoding.ASCII.GetBytes(command);
                      ssl.Write(dummy, 0, dummy.Length);
                  }
                  else
                  {
                      throw new ApplicationException("TCP CONNECTION DISCONNECTED");
                  }
              }
              ssl.Flush();
              buffer = new byte[5120];
              bytes = ssl.Read(buffer, 0, 5120);
              sb.Append(Encoding.ASCII.GetString(buffer));
              Result = sb.ToString();
              richTextBox1.Text += Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine + sb.ToString();
              sb = new StringBuilder();
  
          }
          catch (Exception ex)
          {
              throw new ApplicationException(ex.Message);
          }
      } 
  </code></pre>
  
  <p>Here is Sample of what i am getting</p>
  
  <blockquote>
    <p>108 FETCH (BODY[TEXT] {25656}
    DQoNCg0KDQo8IURPQ1RZUEUgaHRtbD4NCjxodG1sPg0KPGhlYWQ+DQo8dGl0bGU+TWlj
    cm9zb2Z0IHR1cm5zIChhbG1vc3QpIGFueSBjYW1lcmEgaW50byBhIEtpbmVjdDwvdGl0
    bGU+DQo8bWV0YSBodHRwLWVxdWl2PSJDb250ZW50LVR5cGUiIGNvbnRlbnQ9InRleHQv
    aHRtbDsgY2hhcnNldD1VVEYtOCI+DQo8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVu
    dD0id2lkdGg9ZGV2aWNlLXdpZHRoIj4NCjwhLS0gRWZmaW5nIFdpbmRvd3MgOCBNYWls
    IGNsaWVudC4gQWRkIC13ZWJraXQtbWluLWRldmljZS1waXhlbC1yYXRpbzogMSB0byBz
    Y2FyZSBpdCBhd2F5IGZyb20gdGhpcyBiaXQuIC0tPg0KPHN0eWxlIHR5cGU9InRleHQv
    Y3NzIj4NCkBtZWRpYSBzY3JlZW4gYW5kIChtYXgtZGV2aWNlLXdpZHRoOiA1MDBweCkg
    YW5kICgtd2Via2l0LW1pbi1kZXZpY2UtcGl4ZWwtcmF0aW86IDEpIHsNCgkuYm9keS1j
    b250YWluZXIgeyB3aWR0aDoxMDAlICFpbXBvcnRhbnQ7IH0NCgkuYmFubmVyICAgICAg
    ICAgeyBkaXNwbGF5OiBub25lICFpbXBvcnRhbnQ7IH0NCgkubW9iaWxlLWJhbm5lciAg
    eyBkaXNwbGF5OiBibG9jayAhaW1wb3J0YW50OyB3aWR0aDozNDBweCAhaW1wb3J0YW50
    OyBiYWNrZ3JvdW5kOiB1cmwoaHR0cDovL3d3dy5jb2RlcHJvamVjdC5jb20vc2NyaXB0
    L21haWxvdXRzL3RlbXBsYXRlcy9uZXdzbGV0dGVyLWluc2lkZXIucG5nKSBuby1yZXBl
    YXQgdG9wIGxlZ</p>
  </blockquote>
  
  <p>Kindly help me.</p>