Liz
Liz

Reputation: 413

vsto + cannot get the filename of the attachment from a RTF email

I am saving the attachments from a mail item. I am checking first the body format so that I only get true attachments. Please ignore the if else statements below with only comments as the next statements. I'll code that once I fix my problem. Now, my problem is I am getting this error when getting the filename of the attachment of a RichText body format mail. All is well in plain and HTML format.

'currentMailItem.Attachments[1].FileName' threw an exception of type 'System.Runtime.InteropServices.COMException' base {System.Runtime.InteropServices.ExternalException}: {"Outlook cannot perform this action on this type of attachment."}

public static void SaveData(MailItem currentMailItem)
{
    if (currentMailItem != null)
    {       
        string PR_ATTACH_METHOD = "http://schemas.microsoft.com/mapi/proptag/0x37050003";
        string PR_ATTACH_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x37140003";

        if (currentMailItem.Attachments.Count > 0)
        {
            for (int i = 1; i <= currentMailItem.Attachments.Count; i++)
            {
                var attachMethod = currentMailItem.Attachments[i].PropertyAccessor.GetProperty(PR_ATTACH_METHOD);
                var attachFlags = currentMailItem.Attachments[i].PropertyAccessor.GetProperty(PR_ATTACH_FLAGS);

                if (currentMailItem.BodyFormat == OlBodyFormat.olFormatPlain)
                    //no attachment is inline
                else if (currentMailItem.BodyFormat == OlBodyFormat.olFormatRichText)
                {
                    if (attachMethod == 6)
                        //attachment is inline
                    else
                        //attachment is normal
                }
                else if (currentMailItem.BodyFormat == OlBodyFormat.olFormatHTML)
                {
                    if (attachFlags == 4)
                        //attachment is inline
                    else
                        //attachment is normal
                }
                currentMailItem.Attachments[i].SaveAsFile(@"C:\TestFileSave\" + currentMailItem.Attachments[i].FileName);
            }
        }
    }   
}

Upvotes: 2

Views: 1874

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

For the RTF format, you have embedded OLE objects used by the RTF body to show the attachment preview and to edit it in-place. You can either filter out such attachment by the attachment type (Attachment.Type == olAttachmentType.olOLE (6)) or extract the attachment data using either

  1. Extended MAPI (C++ or Delphi only) - call IAttach::OpenProperty(PR_ATTACH_DATA_OBJ, IID_IStorage, ...) to open the attachment data as IStorage and then extract file data from one of the IStorage streams. The exact stream name and format depends on the particular application used to create it (Word, Adobe, Paintbrush, Excel, etc.). You can see the data in OutlookSpy (I am its author): select a message with an attachment like that, click IMessage button on the OutlookSpy ribbon, go to the GetAttachmentTable tab, double click on the attachment, select the PR_ATTACH_DATA_OBJ property, right click, select OpenProperty, select IID_IStorage.

  2. Redemption (any language - I am also its author): its RDOAttachment object is smart enough to extract the actual file data for the OLE attachments when you call SaveAsFile or access the FileName property.

Upvotes: 2

Abiy
Abiy

Reputation: 1

This may be old question ...

I have seen similar issue. If image is attached in the body of rich text formatted mailItem then I get similar exception.

However, if the attachment is not image, say a .doc extension then I don't see the error. As far as I understand the issue is with getting attached image's file name. Some how Outlook doesn't know how to get in body attached image's file name from the rich text formatted email item.

Upvotes: 0

Related Questions