none
System.InvalidOperationException: This operation can't be performed because this service object doesn't have an Id while sending a mail using EWS service RRS feed

  • Question

  • Hi,
    We are trying to send mails through EWS services and we encountered the following exception:

    System.InvalidOperationException: This operation can't be performed because this service object doesn't have an Id.
       bij Microsoft.Exchange.WebServices.Data.ServiceObject.ThrowIfThisIsNew()
       bij Microsoft.Exchange.WebServices.Data.Item.InternalDelete(DeleteMode deleteMode, Nullable`1 sendCancellationsMode, Nullable`1 affectedTaskOccurrences, Boolean suppressReadReceipts)
       bij Microsoft.Exchange.WebServices.Data.Item.Delete(DeleteMode deleteMode, Boolean suppressReadReceipts)

    This doesn't happen with all the users but only some random users. As a result, some mails go out perfectly fine while others doesn't. I will attach the code we are using for the send mails down below for your reference.

    Microsoft.Exchange.WebServices.Data.ExchangeService service = new Microsoft.Exchange.WebServices.Data.ExchangeService(Exchange2007Version);
    service.Url = new Uri(ExchangeURL);
    service.Credentials = new WebCredentials(POPMailAddress, POPMailPass);
    EmailMessage message = new EmailMessage(service);
    message.Importance = ((objPriority == MailPriority.Low) ? Microsoft.Exchange.WebServices.Data.Importance.Low : ((objPriority == MailPriority.High) ? Microsoft.Exchange.WebServices.Data.Importance.High : Microsoft.Exchange.WebServices.Data.Importance.Normal));
                    
    message.From = strFrom;
    message.Subject = strSubject;
    message.ToRecipients.Add(toAddress);
    message.IsDeliveryReceiptRequested = blDeliveryReceipt;
    message.IsReadReceiptRequested = blReadReceipt;
    message.Body = new MessageBody(strBody);
    message.Attachments.AddFileAttachment(filename);
    message.Save();
    message.Load();
    var strEntityId = Convert.ToString(message.Id);
    message.SendAndSaveCopy();

    Tuesday, May 7, 2019 5:09 AM

All replies

  • If you look at the stack trace you posted it doesn't relate to the code sample you posted eg the error is coming from

    Item.Delete

    Method but you don't call this method anywhere in the sample you posted. Sounds like you may have a debugging or logging issue that you not getting the correct feedback from your code. Maybe enable tracing https://blogs.msdn.microsoft.com/webdav_101/2015/05/03/ews-best-practices-tracing-and-logging/ at least this will be a sequential log of what is happening. But for that error look at whatever code you have that's doing a delete sounds to me you missing a Load or validation.

    Cheers
    Glen

    Tuesday, May 7, 2019 11:44 PM
  • Hi Glen,

    Thanks for the response!

    The only place where I have used the delete operation is in the exception handling area. If a mail doesn't go out successfully, the same mail is deleted from the outbox folder of the mail account. That is where we have used the delete functionality. The code used is as follows :

    if (emailMessage != null)
       emailMessage.Delete(DeleteMode.HardDelete);
    This code is managed inside the catch statements. 

    Wednesday, May 8, 2019 4:56 AM
  • Okay well that make sense then if there is an error at send then the Item won't have an ItemId and your code in the exception handle will fail with the error you posted. What you should be doing in your exception handler is check if the Item has a valid ItemId before you call the Delete method which should fix the exception you posted about. 

    For your issue sending you need to look at firstly at the exception your getting that is firing the above code ? throttling possibly ?

    Cheers
    Glen

    Thursday, May 9, 2019 12:06 AM