PiotrKowalski
PiotrKowalski

Reputation: 135

Moving Email Attachment to another folder in Outlook

I want to move/copy my Email Attachments to new folder in outlook ;/ and my code doesn't work properly.

foreach(Item item in findResults.Items)
        {                
             EmailMessage email = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));

             if(false)
             {
                 // OTC Marker HTML Body
             }
             else
             {
                 if (email.HasAttachments)
                 {
                     foreach (Attachment attachment in email.Attachments)
                     {
                         EmailMessage emailAttachment = EmailMessage.Bind(service, attachment.Id, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));

                         ItemAttachment itemAttachment = attachment as ItemAttachment;
                         itemAttachment.Load();

                         EmailMessage mess = itemAttachment.Item as EmailMessage;
                         moveToTestFolder (mess, @"TestFolder");
                     }
                 }
                 else
                 {
                     //to do
                 }
             }  
           }

And my moveToTestFolder method:

    private void moveToTestFolder (EmailMessage item, string folderName)
    {
       Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
       rootfolder.Load();

       var folders = rootfolder.FindFolders(new FolderView(20));
       var folderItemToMove = folders.FirstOrDefault(f => f.DisplayName.Equals(folderName, StringComparison.OrdinalIgnoreCase));

       item.Move(folderItemToMove.Id);
    }

I'am trying to move attachment (if it is an email) to special folder in outlook. Moving the normal message is working now.

Upvotes: 0

Views: 661

Answers (1)

Glen Scales
Glen Scales

Reputation: 22032

That won't work because you can only use the Move and Copy operations to copy an actual Mailbox Item not Attachments (you should be getting an error about and Invalid Id). One workaround for this is to get the MimeContent for the Email Attachment you want to move and then create a New object from that MimeCotent and save it to the folder you want to move the Item to eg

        foreach (Attachment Attach in EWSItem.Attachments)
        {
            if (Attach is ItemAttachment)
            {
                PropertySet psProp = new PropertySet(BasePropertySet.FirstClassProperties);
                psProp.Add(ItemSchema.MimeContent);
                ((ItemAttachment)Attach).Load(psProp);
                if (((ItemAttachment)Attach).Item.MimeContent != null)
                {
                    EmailMessage NewMessage = new EmailMessage(service);
                    NewMessage.MimeContent = ((ItemAttachment)Attach).Item.MimeContent;
                    NewMessage.SetExtendedProperty(new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer), "1");
                    NewMessage.Save(folderItemToMove.Id);
                }
            }
        }  

You don't get full Fidelity of all the Exchange properties on the Message with this method as only the MimeContent is copied which is generally not a problem with Email but will be an issue for other objects types like Contacts, Tasks etc.

Cheers Glen

Upvotes: 1

Related Questions