0

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.

9T9
  • 698
  • 2
  • 9
  • 22

2 Answers2

2

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.

Pavel Donchev
  • 1,809
  • 1
  • 17
  • 29
1

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);
Damith
  • 62,401
  • 13
  • 102
  • 153
  • I don't want to save the currently opened mail in anywhere. I just want to close it without saving + without the message box that appears when we try to close a message which is not saved. – 9T9 Jun 12 '12 at 08:31
  • 1
    I don't think there's a way to close unsaved item. When you close the composer - Outlook will notify the user asking him / her if changes should be saved. Maybe you want to use the above code with a small change to it - instead the last line that moves it to the Drafts folder, just call: omMailItem.Delete(); to delete it (after the composer is closed). Would that work for you? – Pavel Donchev Jun 18 '12 at 05:39