Get the body of the message

Nov 10, 2009 at 9:58 PM

Can anyone post some example code for opening the inbox for a user, interating through the emails and getting the email bodies?  I have eveything working great except for being able to read the body of the email.  An example would be awesome. 

I have found that i can pull the whole email html via Message.BodyParts(Message.HTML).Data , but i am just looking to get the actual text that the user typed..

I am working in vb.net, but C# is fine too.  Thanks in advance..

Nov 13, 2009 at 5:29 PM

 

          ImapConnect connection = new ImapConnect("imap.gmail.com", 993, true);
          ImapCommand command = new ImapCommand(connection);
          ImapAuthenticate authenticate = new ImapAuthenticate(connection, "user", "password");
          connection.Open();
          authenticate.Login();
          ImapMailbox mailbox = command.Select("INBOX");

          mailbox = command.Fetch(mailbox, mailbox.Exist - 100, mailbox.Exist);
          foreach (ImapMailboxMessage m in mailbox.Messages)
          {
            String subject = m.Subject;

            ImapMailboxMessage msg = command.FetchBodyStructure(m);
            if (m.HasText)
            {
              msg = command.FetchBodyPart(m, m.Text);
              String textBody = msg.BodyParts[m.Text].Data;
            }
            if (m.HasHTML)
            {
              msg = command.FetchBodyPart(m, m.HTML);
              String htmlText = msg.BodyParts[m.HTML].Data;
            }
          }
          authenticate.Logout();
          connection.Close();

 

This would read the body/bodies of the last 100 messages. Not sure if I understand you correctly because your way is the correct way to retrieve the (html) body. This is what the mail program of the user created out of the text the user entered.

Nov 13, 2009 at 6:59 PM

What I was looking for was how to pull the actually message out of the email.  Without all the html formating and encoding.  I am new to IMAP so i could be way off base on this, but is this the MIME encoding i see people talk about.  Either way, i am looking to just get the body of the email as you would see it in an email program.  Just text, no formatiing...

Thanks in advance...

Nov 13, 2009 at 10:49 PM

Well thats the thing: if you send a message from your outlook/thunderbird/... your client will format your text and create the html page... Some clients will also create a text-only version (which would be the text/plain -> String textBody = msg.BodyParts[m.Text].Data;).

if you want to see the same thing you would see in your email program, you have to render the html. If you use .net, the easiest thing would be to use the WebBrowser control (see -> http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx).

 

Nov 17, 2009 at 6:43 PM
DieBagger wrote:

Well thats the thing: if you send a message from your outlook/thunderbird/... your client will format your text and create the html page... Some clients will also create a text-only version (which would be the text/plain -> String textBody = msg.BodyParts[m.Text].Data;).

if you want to see the same thing you would see in your email program, you have to render the html. If you use .net, the easiest thing would be to use the WebBrowser control (see -> http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx).

 

Thanks for the follow up.

I am dealing with Outlook/Exchange 2003 emails.  I am checking both the HTML & TEXT body parts, but only the HTML body part contains any data.  I am attempting to use this control to programatically iterate through the emails within an inbox and take action dependent on variables within the email body. 

If i am understanding you correctly i will need to parse the message out of the HTML in order to really get at it.  Is there a standard for doing this or does it really come down to basic string parsing?

Nov 17, 2009 at 8:29 PM

yes that's about right... Do a search for ".net html to text", maybe you'll find an existing solution...

 

http://www.example-code.com/csharp/htmlToText_simple.asp seems like what you need, but it's not freeware...

Nov 17, 2009 at 10:00 PM
Edited Nov 17, 2009 at 10:00 PM

Thanks for point me in the right direction..  I figured it out..  incase anyone else needs the code to parse out the plain text you can use this.

Dim doc As mshtml.IHTMLDocument2 = New mshtml.HTMLDocument
doc.write(Body)
Dim PlainText as string = doc.Body.OuterText