Below is code to read.
public string IMAP_COMMAND_PREFIX = "IMAP00"; public int IMAP_COMMAND_COUNT = 0; public string IMPAP_SUCCESS_RESPONSE = " OK"; public string IMAP_NO_RESPONSE = " NO"; public string IMAP_BAD_RESPONSE = " BAD"; public string IMAP_COMMAND_EOL = "\r\n"; public string IMAP_SEARCH_RESPONSE = "* SEARCH "; public string IMAP_UNSEEN_FLAG = "unseen"; public string IMAP_CAPABILITY_COMMAND = "CAPABILITY"; public string IMAP_LOGIN_COMMAND = "LOGIN"; public string IMAP_LOGOUT_COMMAND = "LOGOUT"; public string IMAP_SELECT_COMMAND = "SELECT INBOX"; public string IMAP_SEARCH_COMMAND = "SEARCH"; public string IMAP_UIDFETCH_COMMAND = "UID FETCH"; public string IMAP_BODYSTRUCTURE_COMMAND = "BODYSTRUCTURE"; protected void Page_Load(object sender, EventArgs e) { try { TcpClient tcpclient = new TcpClient(); // create an instance of TcpClient string strCommand = string.Empty; tcpclient.Connect("host name", 143); // HOST NAME IAMP SERVER and port number 143 for POP Stream sslstream = tcpclient.GetStream(); System.IO.StreamWriter sw = new StreamWriter(sslstream); // Asssigned the writer to stream System.IO.StreamReader reader = new StreamReader(sslstream); // Assigned reader to stream strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_CAPABILITY_COMMAND + IMAP_COMMAND_EOL; ReadResonse(reader, strCommand, sw); strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_LOGIN_COMMAND + " " + "EmailAddressID" + " " + "Email Password"; ReadResonse(reader, strCommand, sw); strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_SELECT_COMMAND + IMAP_COMMAND_EOL; ReadResonse(reader, strCommand, sw); strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_SEARCH_COMMAND + " " + IMAP_UNSEEN_FLAG + IMAP_COMMAND_EOL; ReadResonse(reader, strCommand, sw); strCommand = IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + " " + IMAP_SEARCH_COMMAND + " " + IMAP_LOGOUT_COMMAND + IMAP_COMMAND_EOL; ReadResonse(reader, strCommand, sw); } catch (Exception ex) { } } public void ReadResonse(StreamReader reader, string strCommand, StreamWriter swriter) { try { swriter.WriteLine(strCommand); swriter.Flush(); bool IsLastSuccess = false; string strResponse = string.Empty; while ((strResponse = reader.ReadLine()) != null) { if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMPAP_SUCCESS_RESPONSE)) // find the success character in line { break; } else if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMAP_NO_RESPONSE)) // find the failure character in line { break; } else if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line { break; } else if (strResponse.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line { break; } else if (strResponse.StartsWith(IMAP_SEARCH_RESPONSE)) // find the unread mail { string strTempUnread = strResponse.Substring(strResponse.IndexOf(IMAP_SEARCH_RESPONSE) + IMAP_SEARCH_RESPONSE.Length); string[] strArray = strTempUnread.Split(" ".ToCharArray()); string strSearch = string.Empty; while ((strSearch = reader.ReadLine()) != null) { if (strSearch.StartsWith(IMAP_COMMAND_PREFIX + IMAP_COMMAND_COUNT.ToString() + IMPAP_SUCCESS_RESPONSE)) { IsLastSuccess = true; break; } } for (int i = 0; i < strArray.Length; i++) { //read header of email swriter.WriteLine(IMAP_COMMAND_PREFIX + i.ToString() + " " + "FETCH " + strArray[i].ToString() + " BODY[HEADER.fields (from)]\r\n"); // this will retrive your first email swriter.Flush(); string strTemp = string.Empty; while ((strTemp = reader.ReadLine()) != null) { if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMPAP_SUCCESS_RESPONSE)) { break; } else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_NO_RESPONSE)) // find the failure character in line { break; } else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line { break; } else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line { break; } } // read body of email swriter.WriteLine(IMAP_COMMAND_PREFIX + i.ToString() + " " + " FETCH " + strArray[i].ToString() + " BODY[TEXT]\r\n"); // this will retrive your first email swriter.Flush(); while ((strTemp = reader.ReadLine()) != null) { if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMPAP_SUCCESS_RESPONSE)) { break; } else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_NO_RESPONSE)) // find the failure character in line { break; } else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line { break; } else if (strTemp.StartsWith(IMAP_COMMAND_PREFIX + i.ToString() + IMAP_BAD_RESPONSE)) // find the failure character in line { break; } } } } if (IsLastSuccess == true) break; } IMAP_COMMAND_COUNT++; } catch (Exception ex) { log4net.ILog logger = log4net.LogManager.GetLogger("File"); logger.Error("Lead Convertor + ReadResonse :" + ex.Message); } } }Beloow link for IMAP Commands http://bobpeers.com/technical/telnet_imap.php
Happy coding :-)