none
How to replace attachments of mail object without impacting performance RRS feed

  • Question

  • Hi All,

    We had one requirement that when we open the mail, we need to do some processing and replace the attachments. It should be only to show user and this change should not reflect in exchange server.

    Now we have handled following events :--

    ItemEvents_10_OpenEventHandler Open

    ItemEvents_10_CloseEventHandler Close;

    In open event, we go through attachments and remove existing attachments and cache them locally.

                                            int numOfAttachments = objMail.Attachments.Count;
                                            for (int index = numOfAttachments; index > 0; --index)
                                            {
                                                Attachment attachment = objMail.Attachments[index];
                                                attachment.SaveAsFile(directory + "\\" + attachment.FileName);
                                                attachment.Delete();
                                                Marshal.ReleaseComObject(attachment);
                                            }

    Then we add new attachments like below :---

                 

    foreach (DarkClientHandler.AttachmentInfo attachInfo in managedMail.attachCol)
    {

    //Convert Byte stream to file first

                                                    string originalAttachName = attachInfo.fileName;
                                                    attachInfo.fileName = GetFileNameForSanitization(attachInfo.fileName);
                                                    string tmpFile = (directory + "\\" + attachInfo.fileName);

                                                    if (attachInfo.content != null)
                                                    {
                                                        File.WriteAllBytes(tmpFile, attachInfo.content);
                                                    }

       Attachment attach = objMail.Attachments.Add(tmpFile, attachType, index, originalAttachName);

    }

    Now while closing we follow reverse process and attach original attachments again.

    Problem with this approach is that it is very time taking. If we have many attachments of 4-8 MB, it take lots of time (specifically when we add them) .

    1- Is there any way, by which we can replace all attachments in one shot ?

    2- If above is not possible, is it possible to (cache or create a dummy mail object and replace existing one in one shot) ?

    Please suggest.

    Thanks

    Dev




    Thursday, November 21, 2019 7:21 AM

All replies

  • Why not create your own custom form that will show whatever attachments you deem necessary without modifying the original message?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Saturday, November 23, 2019 10:19 PM
  • Hi Dmitry,

    Thanks for your reply. I agree that we could make custom right pane but it is not a solution which we could implement in 2-3 days and it requires lot of effort to provide same editor as Outlook provides. In current stage of our project, we could not afford it.

    As of now I am looking for some quick solution which could solve our issue. 

    Thanks

    Dev

    Monday, November 25, 2019 6:27 AM
  • A "quick solution" will always require more and more band-aids as you work through new scenarios  when Outlook either does not see your new changes or attempts to persist your temporary changes.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, November 25, 2019 6:51 AM
  • Hi Dmitry,

    Thanks for reply. As a long term solution, we are planning to have our customized form. But we sincerely need some short term solution as well. I am still in shock that outlook does not provide any work around to replace attachments quickly.

    Thanks

    Dev

    Monday, November 25, 2019 10:30 AM