we send emails with attachments by the ews-service with the follwing code...

EmailMessage message = new EmailMessage(ExchangeServiceController);

EmailAddress sender = new EmailAddress
{
 MailboxType = MailboxType.Mailbox,
 Address = senderAdress
};

message.From = sender;
message.ToRecipients.AddRange(email.SendTo.GetEmailAdressRange());
message.Subject = email.Subject;
message.Body = new MessageBody(BodyType.HTML, emailText);
                
string[] fileEntries = SignatureController.GetSignatureImagesFiles();
int i = 0;

foreach (string fileName in fileEntries.Where(f => f.Contains("image")))
{
 FileInfo info = new FileInfo(fileName);

 message.Attachments.AddFileAttachment(info.Name, fileName);
 message.Attachments[i].IsInline = true;
 message.Attachments[i].ContentId = info.Name;
 i++;
}

foreach (string filename in GetAttachmentsAsFilePathList())
{
 message.Attachments.AddFileAttachment(filename);
}

var mailbox = new Mailbox(sender.Address);
var folderIdDrafts = new FolderId(WellKnownFolderName.Drafts, mailbox);
var folderIdSent = new FolderId(WellKnownFolderName.SentItems, mailbox);

// save email at draft to get the mime-content
message.Save(folderIdDrafts);
message.Load(new PropertySet(ItemSchema.MimeContent));
email.EmlContent = message.MimeContent;

if (template.SaveMessageOnSend)
{
 message.SendAndSaveCopy(folderIdSent);
}
else
{
 message.Send();
}

The email saved in draft. After this we get sometimes an follwoing error...

Type: Microsoft.Exchange.WebServices.Data.ServiceResponseException Source: Microsoft.Exchange.WebServices Message: The operation can't be performed because the item is out of date. Reload the item and try again. HResult: -2146233088 (0x80131500) InnerException: null HashedSignature: 49E2BF981546C920B6A4BC0B27476A40 StackTrace: bei Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary() bei Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() bei Microsoft.Exchange.WebServices.Data.ExchangeService.SendItem(Item item, FolderId savedCopyDestinationFolderId) bei Microsoft.Exchange.WebServices.Data.EmailMessage.InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition)

It happens often with attachment over 6 MB.

The timeout is over 1 minute and the error becomes before this time.

The maxsize is over 10 MB

When i take this email at outlook and send it from the draft it works fine.

Have anybody an idea to solve this problem?

best regards steve

1

There are 1 best solutions below

1
On

The reason you get the error is because the changekey is stale at the time you submit the message. This can happen if another client makes a modification on the message or Store level AV can do it see changekey in https://learn.microsoft.com/en-us/exchange/client-developer/web-service-reference/itemid?redirectedfrom=MSDN. With the line

message.Load(new PropertySet(ItemSchema.MimeContent));

This will be forcing the Exchange Store to do an one the fly conversion of the Message to Mime. I'd would try doing another load before sending the message and just ask for the IdOnly

message.Load(new PropertySet(ItemSchema.MimeContent));
email.EmlContent = message.MimeContent;
message.Load(new PropertySet(BasePropertySet.IdOnly));

That should ensure you have the latest changekey before send, otherwise you could probably just build the MIME message with something like MailKit and send it in one operation rather then saving the draft.