-2

I am trying to extract header and body information from email, the following code retrieves the header and body in their raw form. I have an email object that contains the fields from, subject, date, and body. I would like to extract these values from the email and assign them to the email object. How do I get around it? I have tried several ways like getting the header info and using a streamReader.ReadLine() to get a line but I got illegal path exceptions. I know I can use a library but I need to achieve it this way.

What I mean is this, IMAP command returns header information. And I want to extract subject value, date value, sender e-amil, etc. and assign them to my email objects corresponding values like

emailObject.subject = "subjectValue"


public class Imap
{

    static void Main(string[] args)
    {
        try
        {
            path = Environment.CurrentDirectory + "\\emailresponse.txt";

            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);

            sw = new System.IO.StreamWriter(System.IO.File.Create(path));

            tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993);

            ssl = new System.Net.Security.SslStream(tcpc.GetStream());
            ssl.AuthenticateAsClient("imap.gmail.com");
            receiveResponse("");

            Console.WriteLine("username : ");
            username = Console.ReadLine();

            Console.WriteLine("password : ");
            password = Console.ReadLine();
            receiveResponse("$ LOGIN " + username + " " + password + "  \r\n");
            Console.Clear();

            receiveResponse("$ LIST " + "\"\"" + " \"*\"" + "\r\n");

            receiveResponse("$ SELECT INBOX\r\n");

            receiveResponse("$ STATUS INBOX (MESSAGES)\r\n");


            Console.WriteLine("enter the email number to fetch :");
            int number = int.Parse(Console.ReadLine());

            Console.WriteLine("*************Header************");
            Console.WriteLine("");
           // receiveResponse("$ FETCH " + number + " body[header]\r\n");
           // BODY.PEEK[HEADER.FIELDS (SUBJECT)]

         //   StringBuilder sb = receiveResponse("$ FETCH " + number + " BODY.PEEK[HEADER.FIELDS (From Subject Date)]\r\n");
          StringBuilder sb= receiveResponse("$ FETCH " + number + " body.peek[header]\r\n");

             Console.WriteLine(sb);

            Console.WriteLine("");
            Console.WriteLine("Body");
            sb = new StringBuilder();
            sb=receiveResponse("$ FETCH " + number + " body[text]\r\n");

            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] serverbuff = new Byte[1024];
            int count = 0;
            string retval = enc.GetString(serverbuff, 0, count);

            Console.WriteLine(sb.ToString());

            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();
            }
        }


        Console.ReadKey();
    }
    static StringBuilder receiveResponse(string command)
    {
        sb = new StringBuilder();
        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[2048];
            bytes = ssl.Read(buffer, 0, 2048);
            sb.Append(Encoding.ASCII.GetString(buffer));


          //  Console.WriteLine(sb.ToString());
            sw.WriteLine(sb.ToString());
           // sb = new StringBuilder();

            return sb;

        }
        catch (Exception ex)
        {
            throw new ApplicationException(ex.Message);
        }
    }
Jesse
  • 8,605
  • 7
  • 47
  • 57
Neo
  • 717
  • 2
  • 11
  • 26
  • Why not use a library designed specifically for IMAP? See [http://stackoverflow.com/questions/670183/accessing-imap-in-c-sharp](http://stackoverflow.com/q/670183/960195). – Adam Mihalcin Apr 20 '13 at 01:25
  • thanks, that was helpful, need to do it without the library though – Neo Apr 20 '13 at 01:42
  • Could you please post details of this illegal path exception you mention? – Gigi Apr 21 '13 at 13:02

1 Answers1

2

You said you do not want to use an IMAP library. This means that you will have to implement your own. You should start by reading RFC 3501 because there is no chance you could get the protocol right without reading the docs carefuly. In particular, you're issuing a STATUS command on the currently selected mailbox, which is explicitly forbidden by the protocol specification. The rest of the code supports the assumption that you have not read the RFC yet.

Community
  • 1
  • 1
Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29