2

I've tried everything I can think of and cannot figure this out. Basically, I'm making an Outlook 2010 Add-In that makes adjustments to incoming HTML formatted emails for the purposes of making them more Accessible to visually-impaired users (visually impaired users in particular.)

Everything works fine, except that when my Add-In runs, it actually attempts to modify the original email no matter what I try, which Exchange connected Outlook doesn't like at all and rejects. What I want to do is display my modified email message with all of the goodies (reply, reply-all, BCC, etc., just like you would normally use Outlook) without modifying the original message...that is, I only want to display my modified message, not modify the stored message or add a new message.

Such as:

if (selObject is Outlook.MailItem)
{
    Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
    Outlook.MailItem accessible_mail_item = mailItem;
    ...rest of my code...
    accessible_mail_item.Display(false);
}

The problem I have is, "accessible_mail_item" is not a copy of "mailItem"...it's a pointer to it. How can I make a copy/clone of this non-Serializable/Clonable Object? I've tried various "Deep Clone" functions out there, but they all give me the same errors about "not being serializable" or whatever.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • Note on writing questions: avoid "thank you notes" (upvote/comment/accept answers instead) and signature in the question as such text is rarely add value to the question. – Alexei Levenkov Sep 26 '12 at 04:30

3 Answers3

1

Is there any reason why you can't use the MailItem.Copy method?

Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
Outlook.MailItem accessible_mail_item = mailItem.Copy();
...rest of my code...
accessible_mail_item.Display(false);
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • Yes there is a reason. When calling MailItem.Copy(), it literally creates a copy of the message in the Inbox (end up with two copies of the same message in the folder.) Exchange is not liking this. – Kendell Welch Oct 04 '12 at 00:47
  • @KendellWelch: What do you mean by "not liking this"? If I recall correctly, the copy would have a new unique EntryID, and you can always move it to another folder (hidden and/or temporary) immediately after creating it. – Douglas Oct 04 '12 at 06:40
0

Sounds like you've been trying Deep Clone methods which are serializing the object out and then deserializing back into a new object (using BinaryFormatter or similar), thus creating the clone. This of course, requires that the objects you are cloning be Serializable.

Have you tried any other approaches to deep cloning? (e.g., Reflection, IL, ExpressionTrees). Using any of these should be magnitudes faster than the serialization approach, and won't require that the objects be marked up with Serializable, or implement ICloneable.

Try here for an IL deep clone implementation, or here for Reflection, Expression Tree approaches.

plmw
  • 321
  • 1
  • 8
  • Oooo. Tried Reflection...no luck. But, the IL idea never occurred to me. Gonna give that a try...gonna take several hours, but appreciate the suggestion! – Kendell Welch Sep 26 '12 at 04:18
0

If class is not designed to support copy/clone operation it is very unlikely you'll be able to come up with code that will produce functional clone. This is especially true for objects that represent different external entities ore resources. I.e. would you expect to create copy of a remote server page by cloning HttpResponse or creating duplicate SQL server by cloning corresponding managed object? The same with Outlook messages - object that represent mail item to managed code is not actual mail item, but rater way to obtain/modify one somewhere.

I would recommend to look through Outlook API and see if there is a way to create copy of an item the way you want it. You may also try to create new mail item via Outlook APIs and than manually copy properties you are interested in.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179