0
votes

I am using EWS 1.2 to search my mailbox for emails with attachments and I am binding EmailMessage object to the EWS object. This is working fine but not detecting .msg files (outlook message files):

ItemView mailview = new ItemView (12);
FindItemsResults<Item> resultmail;

resultmail = Service.FindItems(WellKnownFolderName.Inbox, mailview);
foreach (Item item in resultmail.Items)
{                   
    EmailMessage email = EmailMessage.Bind(Service, item.Id,
        new PropertySet(BasePropertySet.FirstClassProperties, 
                        ItemSchema.Attachments));

    if (email.HasAttachments)
    {
        foreach (var attachment in email.Attachments)
        {
            if (attachment is FileAttachment)
            {
                Console.WriteLine("email has : " + email.Attachments.Count
                                + "attachement(s)" + "\n");
                Console.WriteLine("attachment name:" + attachment.Name);
            }
        }
    }
}
1

1 Answers

1
votes

if a mail is attached to a mail its not a fileattachment, it is a itemattachment. so you should extend your code like this:

...
if (attachment is FileAttachment) 
{ 
    Console.WriteLine("email has : " + email.Attachments.Count + "attachement(s)" + "\n"); 
    Console.WriteLine("attachment name:" + attachment.Name); 
}
else if (attachment is ItemAttachment)
{
    ItemAttachment itematt = (ItemAttachment) attachment;
    //with itematt.Item you can access the properties of the attachment.
}