Search code examples
c#outlookmailitem

MailItem.Sent gives error 'Item is moved or deleted' when checking if user has sent mail


When I execute the following code, the error appears the item is moved or deleted

Outlook.Application outlookApp = new Outlook.Application();
Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
mailItem.Subject = mailSubject;
mailItem.To = "";
mailItem.CC = "";
mailItem.Attachments.Add(totalPath);
mailItem.Body = mailBody;
mailItem.Importance = Outlook.OlImportance.olImportanceNormal;
mailItem.Display(true);
//Mail is send succesfully?
Bool sent = true;
if (!mailItem.Sent)
{
   sent = false;
}

The error appears when I check the mailItem.Sent property.

My question is how does .Sent work? I am trying to display an email message to an end-user and then check if they have sent the email.


Solution

  • The mail item may not exist any longer after calling the Display method (passing true). It can be moved to the Outbox folder for further processing by the transport provide.

    The Sent property is set once and not what you are looking for. Moreover, checking the Sent property value right after calling the Display method is not a good idea. The mail item can be marked for processing by the transport provider, not being yet sent. Instead, you need to handle the ItemSend event in the code. But checking the Subject line is not a stable solution. At least users may change the pre-set value while the inspector window is displayed.

    Before calling the display method of the MailItem class you can add a user property (your own ID) to the MailItem. Then in the ItemSend event handler you may check out the value and delete it if required. Thus, you can be sure that the item is going to be sent (not actually sent).

    If you need to be sure that the mail item was sent for sure I'd recommend handling the ItemAdd event of the Items class (see the corresponding property of the Folder class). For example, when an Outlook item is sent, a sent copy is placed to the Sent Items folder in Outlook. You may handle the ItemAdd event for that folder to be sure that the item was sent for sure. Consider adding a user property before displaying the Outlook item and checking it in the ItemAdd event handler to identify the item uniquely.

    Be aware, you can specify a custom folder where to place sent mails. The SaveSentMessageFolder property of the MailItem class allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent. So, you can move sent mails to your own custom folder.