none
How to stop outlook to create extra mail in draft RRS feed

  • Question

  • Hi All,

    I have figured out this issue in outlook 2016 (it was not in outlook 2013). I have implemented our plug-in. We are doing our process on send button click.

    Following code I have in Add-In start up :--

                // Get the Application object
                Outlook.Application application = this.Application;

                // Subscribe to the ItemSend event, that it's triggered when an email is sent
                application.ItemSend += 
                    new Outlook.ApplicationEvents_11_ItemSendEventHandler(
                        ItemSend_BeforeSend);

    Then I have following code in ItemSend event.

            void ItemSend_BeforeSend(object item, ref bool cancel)
            {
                MessageBox.Show("BeforeSend");

                cancel = false;
            }

    Now, when my message has been displayed, I could see a mail in draft folder which is same as our compose mail. I have tested very quickly and put breakpoint in auto save as well. So I dont think, auto save has been triggered.

    Can somebody please suggest, why it is happening and if there is any way around to avoid it ?

    Thanks

    Dev

    Sunday, July 28, 2019 1:07 PM

Answers

  • The BeforeAutoSave event is fired when before the item is automatically saved by Outlook. But this event is not fired when other application or add-in saves the item programmatically. Instead, I'd recommend handling the Write and AfterWrite events. The first event is fired when an item is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector). The AfterWrite event is fired after Microsoft Outlook has saved the item. The AfterWrite event occurs after the Write event. This event is not cancelable.

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Sunday, July 28, 2019 8:36 PM
  • Most likely you would not be able to delete the object raising the event from any event handler. What you can do is store the entry id in a variable and start a timer. When it fires, you will be out of the event handler and you will be able to open the item and delete it.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 29, 2019 1:36 PM

All replies

  • Hello Devendra,

    Do you have any other add-ins that may save the item running in Outlook? 

     I have tested very quickly and put breakpoint in auto save as well. So I dont think, auto save has been triggered.

    Which event did you try to handle?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Sunday, July 28, 2019 4:26 PM
  • New message is always created in the Drafts folder. Is your issue that it gets saved? Why does it matter? Are you saying that only happens when you handle the Application.ItemSend event?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, July 28, 2019 5:29 PM
  • <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} </style>

    HI Eugene,

    Thanks for response. For auto save, I have handled following event :--

    ItemEvents_10_BeforeAutoSaveEventHandler

    I have normal microsoft office plug-ins. Like Microsoft share point plug-in, exchange add-in plug-in etc. Do you want me to check any specific plug-in.

    Sunday, July 28, 2019 7:22 PM
  • HI Dmitry,

    Thanks for your reply. To be honest, I got confused after reading your reply :--

    1- This was not happening in outlook 2013. 

    2- New mail appears in draft only when we click on send button (i.e. in ItemSend event).

    3- My issue is that if I am overriding this behavior in send, then we need to handle delete of this mail item in draft as well. Now lets suppose my draft has 500 items, then searching for right mail is not easy. Is there any easy way to do it ?

    Thanks

    Dev

    Sunday, July 28, 2019 7:27 PM
  • Nothing prevents the user from hitting Ctrl+S. Or for autosave to kick in. You needs to be able to handle that.

    Why is finding the email is a problem? When ItemSend event fires, save the message by calling MailItem.Save, then read its EntryID property(it is guaanteed to be available after it is saved at least once). You can then reopen the message by its entry id (Namespace.GetItemFromID) at any moment, there is no need to search for anything.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Sunday, July 28, 2019 7:32 PM
  • The BeforeAutoSave event is fired when before the item is automatically saved by Outlook. But this event is not fired when other application or add-in saves the item programmatically. Instead, I'd recommend handling the Write and AfterWrite events. The first event is fired when an item is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector). The AfterWrite event is fired after Microsoft Outlook has saved the item. The AfterWrite event occurs after the Write event. This event is not cancelable.

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Sunday, July 28, 2019 8:36 PM
  • HI Dmitry,

    I guess you have completely misunderstood my question. As I had mentioned, user is not clicking save button or auto save is not getting fired. Therefore when user click on send button, I did not expect that when flow is in itemsend, outlook will automatically create a mail in draft from composed mail. Outlook is doing it automatically.

    My guess is outlook deletes it when send operation completes. But my flow is, I cancel that operation , close the compose window (using discard option) and then create copy of composed mail (actually this is first step) and then send programmatically. In this process this draft which was created by outlook as soon as we enter in itemsend event, left behind. So I have to programmatically go through draft items and delete that which slow down my process. As some users has more than 400 mails in draft.

    Thanks

    Dev

    Monday, July 29, 2019 3:36 AM
  • HI Eugene,

    Thanks for reply and suggestion. Let me try this and get back to you.

    However I have still a question (I hope some microsoft guy is also listening) that why outlook is creating mail in draft on click of send button ? This was not the case in outlook 2013. I could see this in outlook 2016 only. Is this a bug ? Because even they want to do it, they could do it in some hidden folder ? What is point to create in normal draft folder ?

    Or is there any setting which I am missing ?

    Thanks

    Dev


    Monday, July 29, 2019 3:37 AM
  • I think you misunderstood my reply - firstly, what *if* the user saves the message?  Or it gets autosaved? You cannot prevent that. So why the message gets saved is really a purely theoretical question.

    If you have the message entry id (after it was saved for whatever reason), you don't need to search for anything no matter how many items are in the Drafts folder - just open it by entry id (Namespace.GetItemFromID) and delete it (MailItem.Delete). Quick and easy.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 29, 2019 3:46 AM
  • HI Dimtry,

    Aah ! I got your point. I have tested and it seems Entry ID of mail object in compose window is same of draft id. However I have to test that if I delete this in item_send event, I am not cutting the branch , on which one is sitting. 

    Let me test it and get back to you. 

    Thanks

    Dev

    Monday, July 29, 2019 12:17 PM
  • Most likely you would not be able to delete the object raising the event from any event handler. What you can do is store the entry id in a variable and start a timer. When it fires, you will be out of the event handler and you will be able to open the item and delete it.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Monday, July 29, 2019 1:36 PM
  • HI Microsoft,

    I have marked it as answered as we have implemented work around. But microsoft should treat this as bug. Why they need to create extra mail in draft ? Even of they want to, they could do in hidden draft folder. Is not it ?

    Thanks

    Dev

    Sunday, October 13, 2019 3:24 AM