简体   繁体   中英

Close draft mail without saving in Outlook using C#

When I try to close a draft mail in outlook it asks a message box to save, discard or cancel. I want to write the code to automatically discard it without prompting that message.

If I use mailItem.Save(); . That message box will not appear. But it saves the message in the folder what I didn't want.

So in order to make it as an answer for other people to see it - maybe what you need to do is the following:

  1. Save the item.
  2. Close the composer.
  3. Delete the item.

This way you will not have the item in any of the folders and you will still have avoided the save prompt?

So just call Item.Delete(); after you close the composer.

Hope this helps.

try something like below, I haven't tested bellow is working or not

Outlook.Application omApp = new Outlook.Application();
Outlook.NameSpace omNamespace = omApp.GetNamespace("MAPI");
Outlook.Recipient omUser = omNamespace.CreateRecipient("email1@abc.com");
omUser.Resolve();
if (!omUser.Resolved) return; 
Outlook.MAPIFolder omDrafts = omNamespace.GetSharedDefaultFolder(omUser, Outlook.OlDefaultFolders.olFolderDrafts);
Outlook.MailItem omMailItem = (Outlook.MailItem)omDrafts.Items.Add();
omMailItem.To = "email2@abc.com";
omMailItem.Subject = "Test";
omMailItem.Body = "Test email";
omMailItem.Save();
omMailItem.Move(omDrafts);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM