2
votes

I am currently working on a former colleague's project that deals with saving emails from Exchange to our ERP system.

But I have run into a strange problem. Sometimes when the system receives an email, that contains an email as an attachment. It throws an error because the DataTimeReceived date is not set. Even after a .Load();

 private int HandleEmail(Item item, Folder moveToFolder, Folder moveToFailedFolder, Mailbox mailbox, int fatherId = 0, string uploaderEmail = "", bool isEmbeddedMail = false)

The way it work. is that the HandleEmail() method goes through the original email until it gets the if-check. If the attachment is not a FileAttachment, and is an ItemAttachment. The following code is called.

else if (attachment is ItemAttachment)
{
 var itemAttachment = attachment as ItemAttachment;
 itemAttachment.Load(new PropertySet(ItemSchema.Attachments, ItemSchema.TextBody, EmailMessageSchema.Sender, EmailMessageSchema.DisplayCc, EmailMessageSchema.DateTimeReceived, EmailMessageSchema.From, ItemSchema.MimeContent, ItemSchema.Body, ItemSchema.TextBody, EmailMessageSchema.BccRecipients, ItemSchema.Attachments));
  var item_ = itemAttachment.Item;

 HandleEmail(item_, null, null, mailbox, (int)mailid, uploaderEmail, true); // The attached email is then looped through like it is an regular email instead of like an image.
}

In this check. It takes the item attachment and loads an additional property set, which contains some data that is needed. Finally the method calls it self, but now with the Item Attachment as to read it like a regular email. My issue is the fact, that some attached emails do not have the DatetimeReceived property set. Instead this error is shown in its place.

Microsoft.Exchange.Webservice.Data.ServicesObjectPropertyException

And this expection message is thrown

You must load or assign this property before you can read its value

And I can't quite figure out why it only happens to some attached emails. When I look at the emails it self, it does have an received date. But for some reason I can't get it using .Load()

I've tried a few things, such as using .Load for both Item and ItemAttachment, both without getting anything worthwhile. Tried looking into using the service.loadpropertiesforitems() I've forgotten a few of the other things I've looked into, since I have been looking at this for a few days before the weekend aswell.

1
two suggestions i would have is first try the EmailMessageSchema.DateTimeSent (which should be the same as DateTimeReceived). The other thing would be enable tracing and have a look at the traces to see what is actually coming back from the server also maybe just try loading the BasePropertySet.FirstClassProperties rather then you custom set (just to test that property anyway).Glen Scales
@GlenScales I decided to follow your first advice and so far its worked as it should, so thank you for that. But I will probably try your other aswell. If you make your suggestion an answer, I will gladly accept it.Carsten

1 Answers

1
votes

Two suggestions i would have is first try the EmailMessageSchema.DateTimeSent (which should be the same as DateTimeReceived) the difference maybe being caused when attaching a messages that was sent. The other thing would be enable tracing and have a look at the traces to see what is actually coming back from the server also maybe just try loading the BasePropertySet.FirstClassProperties rather then your custom set (just to test that property anyway)