Click here to Skip to main content
15,354,956 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)

I want to save the body content of a email in outlook to a local file. I am able to save the entire message .msg but i want to save only the html content of the body. for example: In the outlook email body i have a table i want to save that table to a file. the script which i am working on:

public void GetAttachments() 
   Microsoft.Office.Interop.Outlook.Application myolApp = default(Microsoft.Office.Interop.Outlook.Application);
   Microsoft.Office.Interop.Outlook.NameSpace ns = default(NameSpace); 
   MAPIFolder Inbox = default(MAPIFolder); 
   object Item = null; 
   Attachment Atmt = default(Attachment); 
   string FileName = null; string subject = null;
   string AttachmentName = null; string Body = null; string SenderName = null; string SenderEmailAddress = null; string CreationTime = null; int i = 0; int j = 0; 
        myolApp = (Microsoft.Office.Interop.Outlook.Application)Interaction.CreateObject("Outlook.Application","");
        ns = myolApp.GetNamespace("MAPI");
        ns.Logon("", "", false, true);
        Inbox = ns.Folders["Mailbox - Name"].Folders["Inbox"];
        i = 0;
        j = 1;
        //Scan for attachments
        foreach (object Item_loopVariable in Inbox.Items) 
           Item = Item_loopVariable;
           if ((Item as MailItem) != null ? ((MailItem)Item).UnRead : false) 
             Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body;
                  j = j + 1;

        //Clear Memory
        Atmt = null;
        Item = null;
        ns = null;
   catch (System.Exception ex)
        MessageBox.Show("An unexpected error has occurred." + "\r\n" + "Please note and report the following information."         + "\r\n" + "Script Name: GetAttachments" + "\r\n" + "Error Description: " + ex.Message + "\r\n" + "Error StackTrace: " + ex.StackTrace, "Error!");
        Atmt = null;
        Item = null;
        ns = null;

I need changes in these piece of code:

Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body; 
((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body; 
((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\path\"+"filename", Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML );

Any help.
Updated 4-Apr-11 8:34am
Orcun Iyigun 4-Apr-11 14:34pm
Edited for readability.
[no name] 4-Apr-11 15:05pm
Thank you for editing.
Member 10454303 21-Jan-14 1:46am
Hello guys,

I have a requirement of storing the emails that I receive to my emailID(not OUTLOOK) into the SQL database. I am unaware how to start and hav no Idea how to get it done. Can anyone please help me.

You may want to look into

[Microsoft Exchange Web Services]

It's much more robust than the outlook interop.

Good Luck
[no name] 4-Apr-11 16:07pm
My requirement is to use MAPI with outlook interop.Thanks.
dbrenth 4-Apr-11 16:35pm
In that case I withdraw my solution. I think you will have to parse the Body string and save it out to a file with a good old fashioned Stream function.
Perhaps I misunderstood your question, but when you open the mailitem, why don't you just get the data from the HTMLBody property and write that to a file for example using TextWriter class. If you use Outlook to save the message, it'll save also parts of the header etc.
[no name] 5-Apr-11 9:58am
Hi thanks for the solution can you just elaborate it for me please i think that is what i am looking for.

Wendelius 5-Apr-11 14:17pm

For example using StringWriter.Write (I think this is an easier way) method (see: you could just pass the HTMLBody to the writer. For an example how to use stringwriter, see:
[no name] 5-Apr-11 16:32pm
i want to save the source of the html table for eg:if the body of the email is :
My First Heading
My first paragraph.
then i want the output to the file like these:

My First Heading

<p>My first paragraph.</p>


Wendelius 5-Apr-11 16:37pm
If you debug through the code, what do you see in the HTMLBody property. Is that containing a correct data?
[no name] 5-Apr-11 16:44pm
HTMLBody Property not showing me the correct data the way i want, it showing the view source code of the email body of outlook.Any suggestion?presently iam using these:
System.IO.TextWriter writeFile = File.CreateText(@"path\a.txt");
Wendelius 5-Apr-11 17:00pm
Ok, I don't have access to Outlook so I won't be able to test this, unfortunately. But if you use the debugger and stop before the write operation, you could have a look at the ((Microsoft.Office.Interop.Outlook.MailItem)Item in the quick watch window and open the different properties to see if the data exists in correct format in another way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900