Click here to Skip to main content
11,496,146 members (653 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C# Outlook SSIS script

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.
Posted 4-Apr-11 9:26am
Edited 4-Apr-11 9:34am
orc_orc_orc at 4-Apr-11 14:34pm
Edited for readability.
Member 10454303 at 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
dbrenth at 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.
Mika Wendelius at 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:
Mika Wendelius at 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?
Mika Wendelius at 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)

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 4 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100