Trouble copying body of email "manually"

Add-in Express™ Support Service
That's what is more important than anything else

Trouble copying body of email "manually"
Need help copying the Body/HTMLBody from a MailItem to a manually-created MailItem. Encoding Issues? 
Daniel Garcia




Posts: 9
Joined: 2010-10-14
I'm experiencing (perhaps encoding?) issues when trying to copy the body contents of an email to a manually-created MailItem. I use the code below and everything seems to work well, except that the resulting text files look different***. Below is the code I'm using to test this:

Private Sub SavePlainTextBody_ByCopyingBodyOnly(ByVal originalMailItem As Outlook._MailItem)
   Dim theApp As Outlook.Application = Nothing
   Dim tempMailItem As Outlook.MailItem = Nothing
   Try
       ' create a temporary Mailtem
       theApp = originalMailItem.Application
       tempMailItem = theApp.CreateItem(Outlook.OlItemType.olMailItem)
       ' copy only what we want
       tempMailItem.Body = originalMailItem.Body
       tempMailItem.HTMLBody = originalMailItem.HTMLBody
       ' and save both to see the differences
       originalMailItem.SaveAs("c:email_original.txt", Outlook.OlSaveAsType.olTXT)
       tempMailItem.SaveAs("c:email_bodyonly.txt", Outlook.OlSaveAsType.olTXT)
    Finally
        If (Not tempMailItem Is Nothing) Then Marshal.ReleaseComObject(tempMailItem)
        If (Not theApp Is Nothing) Then Marshal.ReleaseComObject(theApp)
    End Try
 End Sub


*** Some of the differences are expected, since [email_original.txt] contains a bunch of header/attachment/etc info at the top that [email_bodyonly.txt] does not have, but I'm interested in the body content only.

Example: with a typical HTML email, the content of [email_original.txt] in Notepad looks like:

Redmond Developer News and FuseSource present: 
Apache Camel Getting Started 
 
Dear Daniel,  



Whereas the same section in [email_bodyonly.txt] looks like:

Red
mon
d 
Dev
elop
er 
New
s 
and 
Fuse
Sour
ce 
pres
ent: 
Apa
che 
Ca
mel 
Gett
ing 
Star
ted 
 
Dear 
Dani
el,  



What am I missing? Are there any other properties (beyond Body and HTMLBody) that I need to copy from [originalMailItem] so that saving [tempMailItem] produces the same output?
Posted 27 Jan, 2011 09:49:58 Top
Eugene Astafiev


Guest


Hi Daniel,

Please have a look at the BodyFormat property of the MailItem class in Outlook. What value does it return in both cases?

To get assistance with host applications?Â?Ð?é objects, their properties, and methods as well as help info, use the Object Browser. Go to the VBA environment (in the host application, choose menu Tools | Macro | Visual Basic Editor or just press {Alt+F11}), press {F2}, select the host application in the topmost combo and/or specify a search string in the search combo. Select a class /property /method and press {F1} to get the help topic that relates to the object.
Posted 27 Jan, 2011 12:20:39 Top
Daniel Garcia




Posts: 9
Joined: 2010-10-14
Hi Eugene,

In this case both return [olFormatHTML]

(altough I need this to work no matter what the format of the original item is)
Posted 27 Jan, 2011 12:29:36 Top
Daniel Garcia




Posts: 9
Joined: 2010-10-14
Eugene,

I have also tried 2 other approaches:

Approach 2) Saving the [originalMailItem] as .MSG and using that to populate my [tempMailItem]. Roughly:

originalMailItem.SaveAs("C:	emp.msg",olMSG)
tempMailItem = theApp.CreateItemFromTemplate("C:	emp.msg")
' then save both as olTXT, as per example


RESULT: exactly the same as in the original post


Approach 3) Using straight Copy, roughly:

tempMailItem = originalMailItem.Copy()
' then save both as olTXT, as per example
' and delete the temp email
tempMailItem.Delete()


RESULT: this works as expected and produces a "proper looking" [email_bodyonly.txt] file, unfortunately it seems that using Copy() creates a real email that actually shows up in Outlook and that does not go away even though I call tempMailItem.Delete() right after saving to olTXT. This approach is the one I like the least, but it would be workable if only I could prevent [tempMailItem] from showing up in Outlook or -at least- if I was able to delete it right away.
Posted 27 Jan, 2011 14:11:26 Top
Eugene Astafiev


Guest


Hi Daniel,

Unfortunately I couldn't reproduce the issue on my PC with Outlook 2007 SP2. I used the following code for testing:

Outlook.NameSpace ns = OutlookApp.GetNamespace("MAPI");
Outlook.MAPIFolder folder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = folder.Items;
Outlook.MailItem mail = items.Item(1) as Outlook.MailItem;
Outlook.MailItem newMail = items.Add(0) as Outlook.MailItem;
            
newMail.Body = mail.Body;
newMail.HTMLBody = mail.HTMLBody;

mail.SaveAs(@"C:mail.txt", Outlook.OlSaveAsType.olTXT);
newMail.SaveAs(@"C:
ewMail.txt", Outlook.OlSaveAsType.olTXT);
            
if (mail != null) Marshal.ReleaseComObject(mail);
if (newMail != null) Marshal.ReleaseComObject(newMail);
if (items != null) Marshal.ReleaseComObject(items);
if (folder != null) Marshal.ReleaseComObject(folder);
if (ns != null) Marshal.ReleaseComObject(ns);


Could you please provide me with a sample add-in project? Also I am interested in the Mail format settings in Outlook.
Posted 31 Jan, 2011 07:40:49 Top
Daniel Garcia




Posts: 9
Joined: 2010-10-14
Hi Eugene,

Thank you for looking into this, I tried your code above and it still shows the same problem I describe in the original post. So it seems going this way is not going to produce consistent results across different installations/versions/settings of Outlook, which is what we ultimately need.

We have decided to go another route to obtain the text representation of the body: just save the email to olTXT and "clean up" all the header information at the top of the file. It is not pretty, but at least it seems to work consistently.

Thanks gain!
Posted 09 Feb, 2011 09:23:46 Top
Eugene Astafiev


Guest


Hi Daniel,

Thank you for sharing the solution for others.

You are welcome!
Posted 09 Feb, 2011 09:29:29 Top