Exchange Web Services - Processing Messages and Accessing Attachments
Asked Answered
B

2

7

I'm in the process of writing a simple console app that monitors a particular exchange mailbox, and when emails meeting particular criteria are received, the app will download an XML file attachment, and archive the email.

I've connected to EWS OK, and have been able to loop through any emails, but I'm struggling when it comes to create an EmailMessage object which I can use to access the attachments.

In the example code below, the EmailMessage message = EmailMessage.Bind(...) line executes without error, but doesn't return a valid message so when I access and properties or methods, I get an error: 'Object reference not set to an instance of an object'.

I'm new to C# let alone EWS so I'm struggling to know where to start...

Code Snippet:

    public static void FindItems()
    {
        try
        {
            ItemView view = new ItemView(10);
            view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
            view.PropertySet = new PropertySet(
                BasePropertySet.IdOnly,
                ItemSchema.Subject,
                ItemSchema.DateTimeReceived);

            findResults = service.FindItems(
                WellKnownFolderName.Inbox,
                new SearchFilter.SearchFilterCollection(
                    LogicalOperator.Or,
                    new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sales Enquiry")),
                view);

            log2.LogInfo("Total number of items found: " + findResults.TotalCount.ToString());

            foreach (Item item in findResults)
            {
                log2.LogInfo(item.Id);

                EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

                Console.WriteLine(message.Subject.ToString());

                if (message.HasAttachments && message.Attachments[0] is FileAttachment)
                {
                    FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
                    fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
                    fileAttachment.Load();
                    Console.WriteLine("FileName: " + fileAttachment.FileName);
                }
            }
        }
        catch (Exception ex)
        {
            log2.LogError(ex.InnerException);
        }
    }

My code for accessing the attachments is straight from MSDN so I'm hoping it is there are thereabouts... Any ideas?

Bignonia answered 14/6, 2010 at 11:31 Comment(1)
I don't think you'll be able to print the Subject line as your property set doesn't include the subject. What are you being returned from Exchange with that code? Is the emailmessage object valid? The following might help:social.technet.microsoft.com/Forums/en/exchangesvrdevelopment/… You don't need to loop through the item to bind - try LoadPropertiesForItems and see if that worksAddressee
B
13

I'm afraid I revisited this problem and managed to cure it. Unfortunately I was too pressed at the time to come back here and document the solution. Time having passed, and my memory of what I changed has faded, but as far as I can remember it was a one line change:

EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));

The key difference here is that we have specified BasePropertySet.FirstClassProperties as the first parameter of PropertySet, rather than the BasePropertySet.IdOnly that we originally had.

My original code was lifted from an example online that did precisely what I was trying to achieve, so either the example wasn't quite right, or I transcribed it incorrectly or misunderstood some facet of the problem.

Bignonia answered 6/7, 2010 at 15:35 Comment(0)
B
0
foreach(EmailMessage message in findResults)
{
    message.Load();

    Console.WriteLine(message.Subject.ToString());

    if (message.HasAttachments && message.Attachments[0] is FileAttachment)
    {
        FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
        fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
        fileAttachment.Load();
        Console.WriteLine("FileName: " + fileAttachment.FileName);
    }
}
Broadminded answered 21/2, 2017 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.