Sam Blake
Sam Blake

Reputation: 47

Saving msg file attachment using EWS API

Hope someone can help. I am implementing a feature where you can choose an email attachment and save it within a database. The feature works fine with PDF etc but when it comes to MSG files it creates a ItemAttachment not a Fileattachment and does not give me the ability to get the content or content type.

I have found this post about saving messages as .eml but ideally as the email had an .msg attached this is what should be saved in the system. I have done some extensive searching but have come to a dead end.

I am using asp.net so answers in VB.net would be appreciated.

Thanks

Upvotes: 0

Views: 1618

Answers (2)

Serge Semenov
Serge Semenov

Reputation: 10042

When querying an ItemAttachment, add the MimeContentPropertyDefinition (sorry, don't remember the exact names and classes). Then your ItemAttachment will have that MimeContent property set - that's a text (MIME) that you can save to a file in UTF-8 encoding with .EML extension. MIME is a standard, so that .EML file can be opened by any mail client app.

Upvotes: 1

HarryvdBlok
HarryvdBlok

Reputation: 11

EDIT: Sorry, I did not see you were using VB, for now I won't delete my post since it can still give you an idea of how to handle this, I know that when I struggled with this, my thought of process was just incorrect and the actual syntax was not a challenge.

I am fairly new to this website so excuse me if I am not formatting my answer correctly.

Assuming you already established your exchange connection, created a new ItemView, and retrieved all emails from your inbox, we will start off by creating a secondary list of emails, but we are only going to list the emails that contain an Item Attachment.

List<EmailMessage> emailsWithItemAttachment =
            emails.Where(e => e.HasAttachments && e.Attachments[0] is ItemAttachment).ToList();

Now, we can loop only the emails with Item attachment(s)

 foreach (EmailMessage emailMessage in emailsWithItemAttachment)
        {
            //Loads all emails with Item attachments as an item attachment

            foreach (Attachment attachment in emailMessage.Attachments)
            {
                attachment.Load();
                ItemAttachment itemAttachment = attachment as ItemAttachment;
                if (itemAttachment == null) continue;
                ItemAttachment itemattachment = attachment as ItemAttachment;
                itemattachment.Load(new PropertySet(ItemSchema.Attachments));

                //Loads the scanned Attachment as an Item Attachment

                foreach (Attachment scannedAttachment in itemattachment.Item.Attachments)
                {
                    scannedAttachment.Load();

                    //Loads all Item Attachments as File Attachments

                    FileAttachment fileAttachment = scannedAttachment as FileAttachment;
                    if (fileAttachment != null)
                    {

                     //All Done! Your attachment will be "fileAttachment", from here you can do whatever you want

                    }
                }
            }
        }

I really hope this helps you, and again if anything is wrong about my answer please do not hesitate to edit and/or contact me!

Upvotes: 1

Related Questions