Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Outlook SSIS script
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 9:26am
Edited 4-Apr-11 9:34am
v2
Comments
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.
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
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.
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
Mika Wendelius at 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
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 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 4 Apr 2011
Copyright © CodeProject, 1999-2014
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