Quantcast
Channel: Outlook IT Pro Discussions Forum
Viewing all articles
Browse latest Browse all 3066

Outlook 2010 - Removing and re-adding the attachments back in an RTF mail, makes all the attachments disappear!

$
0
0

Hi,

We have a scenario where when we forward an incoming mail, containing around 80 doc attachments,  inRTF format, the attachments are getting lost when we do some pre-processing on the documents. Below are the steps:

1. Have a mail with around 80 doc attachments send in HTML format
2. Forward the mail
3. Have an Outlook plugin that does the following :
    a) Intercepts the Send
    b) Downloads all the attachments to TEMP (for some document tagging etc operations)
    c) Remove the existing attachments from MailItem
    d) Re-attach the downloaded (processed) attachments

We are seeing, that whilst we are reattaching the documents, after around 46 docs, the attachment count gets reset to zero automatically and we are unable to add any further attachments. When the mail is finally sent, the count is still zero and no attachments are sent out.

Below is the code in the Outlook plugin that we have:

public void ProcessAttachments(Outlook.MailItem mailItem)
{
    Dictionary<string, Outlook.Attachment> items = new Dictionary<string, Outlook.Attachment>();
    long count = mailItem.Attachments.Count;

    // download all the attachments to TEMP for local processing
    for (long lIndex = 1; lIndex <= count; lIndex++)
    {
        Outlook.Attachment attachment = mailItem.Attachments[lIndex];
        if (attachment.Type == Outlook.OlAttachmentType.olByValue) // i.e we can save to file.
        {
            string sPhysicalFile = Path.GetTempFileName();
            attachment.SaveAsFile(sPhysicalFile);

            string sFinalFile = Path.Combine(Path.GetDirectoryName(sPhysicalFile), attachment.DisplayName);
            File.Move(sPhysicalFile, sFinalFile);

            // -- any local processing of the downloaded file goes here --

            // we add the index as same file or similar filenames can be added multiple times
            items.Add(string.Format("{0}#{1}", sFinalFile, lIndex), attachment);
        }
    }

    // now replace them
    // Remove all the attachments first
    for (long index = count; index >= 0; index--)
    {
        mailItem.Attachments.Remove((int)index);
    }
    mailItem.Save();

    // Re-add them
    foreach (var item in items)
    {
        var oldAttachment = item.Value;
        var fileName = item.Key.Split(new char[] { '#' })[0];

        int position = (int)oldAttachment.Position + 1;
        mailItem.Attachments.Add(fileName, Outlook.OlAttachmentType.olByValue, position, oldAttachment.DisplayName);
        mailItem.Save(); // moving Save outside this loop does not helps either
    }
}

As seen in above code, when we are re-attaching the documents, the count automatically gets reset to zero after 46th attachment.

Any inputs will be highly appreciated.

Thanks
Sandy


Viewing all articles
Browse latest Browse all 3066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>