Click here to Skip to main content
13,148,224 members (53,437 online)
Rate this:
 
Please Sign up or sign in to vote.
Hi,

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; 
   try 
   {
        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;
           System.Windows.Forms.Application.DoEvents();
           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;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML);
                  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.
thanks
Posted 4-Apr-11 8:26am
Updated 4-Apr-11 8:34am
v2
Comments
orc_orc_orc 4-Apr-11 14:34pm
   
Edited for readability.
Member 7811863 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You may want to look into

[Microsoft Exchange Web Services]

It's much more robust than the outlook interop.

Good Luck
  Permalink  
Comments
Member 7811863 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Member 7811863 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.

thanks
Mika Wendelius 5-Apr-11 14:17pm
   
Hi,

For example using StringWriter.Write (I think this is an easier way) method (see: http://msdn.microsoft.com/en-us/library/80fs4221.aspx) you could just pass the HTMLBody to the writer. For an example how to use stringwriter, see: http://msdn.microsoft.com/en-us/library/system.io.stringwriter.aspx
Member 7811863 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:
<html>
<body>

My First Heading



<p>My first paragraph.</p>

</body>
</html>

thanks
Mika 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?
Member 7811863 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");
writeFile.Write(((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody);
writeFile.Close();
thanks
Mika 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 4 Apr 2011
Copyright © CodeProject, 1999-2017
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