none
How to send an email in the background without triggering the ItemSend event RRS feed

  • Question

  • Hi all, 

    I've been struggling with finding a solution for this so hoping someone can help on here.

    I'm looking for a way to intercept the send event when the user sends an email in Outlook, cancel this event and move the email somewhere for processing in the background (to not freeze up UI), and then after processing it send it on as normal.

    Currently I can trigger off the ItemSend event and cancel this, close the email so it looks like it sent but actually remains in drafts where I can access it for processing, afterwards I can then send the email using MailItem.Send from there.

    The problem is that if there are other add-ins installed that also trigger off the Item.Send event then these keep triggering again when I send the email a second time from the draft folder. Is there anyway to send the email programatically without triggering the send event again since these other addins should only trigger when the user clicks send?

    e.g. are there any MAPI properties I can set before doing MailItem.Send to avoid triggering the event, or maybe is there another way to send the email?

    I was hoping there might be a way because what happens if an add-in has to send out automated report emails in the background, surely these don't trigger all the other add-ins that intercept the send and require user response. (e.g. email tagging add-ins)

    Thanks in advance! 
    Tom

    Thursday, August 4, 2016 10:15 PM

Answers

All replies

  • You can bypass ItemSend event only if you do not use the Outlook Object Model to send the messages - use Extended MAPI (C++ or Delphi - IMessage::SubmitMessage) or Redemption (any language).


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

    Thursday, August 4, 2016 10:23 PM
  • Hi Dmitry,

    That is great, we actually already use your Redemption extension for other things.  Could you point me in the direction of how I would use it to send the email in that way?

    Thanks,

    Tom

    Friday, August 5, 2016 7:23 AM
  • Something to consider --

    The order in which Outlook sends the ItemSend event to loaded add-ins is not documented.  (If I remember correctly some have observed it is based on add-in progid's). 

    In a limited test with several add-ins loaded into Outlook 2013 I noticed that when an add-in cancels a send event in it's ItemSend handler that other add-ins may not receive the event.  If you then send that message using a method that avoids triggering Outlook's ItemSend event those other add-ins that receive ItemSend events after you will never see the sent item.

    Friday, August 5, 2016 12:33 PM
  • Yes we already use this theory to try and make our add-in always trigger last since we rely on some other 3rd party add-ins adding their subject tags to the outgoing email before we process it.  The difficulty is that because our processing may take a while we can't do it all within that ItemSend event since it holds up the UI, and so it is necessary to cancel the event and move the email temporarily to drafts while we process the data in a background thread.

    The difficulty is that when we want to finally send the email on, calling MailItem.Send() will trigger those other 3rd party add-ins again on the ItemSend event... but we don't want this because those add-ins have already done their thing.  So I need to find a way to send this email on from drafts without triggering the ItemSend event.

    Any thoughts on how this would be possible with Redemption?

    Friday, August 5, 2016 1:04 PM
  • Tom, create a new instance of RDOSession, cal RDOSession.GetRDOObjectFromOutklookObject passing the MailItem object. Cast the returned object to RDOMail, call RDOMail.Send

    Or create a new instance of the SafeMailItem object, set its Item property to MailItem object, call SafeMailItem.Send.


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

    Friday, August 5, 2016 2:07 PM
  • Thanks Dmitry will give this a try.  Will this work also on an Outlook account connected to gmail (IMAP/SMTP) or is this just for Exchange accounts?
    Friday, August 5, 2016 4:18 PM
  • Hi Dmitry,

    Two quick questions on the implementation here.

    1) After casting the RDOMail and then calling RDOMail.Send, should I release the COM Object from either the RDOMail or MailItem objects that I am using?  (the email is currently stored in drafts and I have initially referenced it using GetItemFromID function)

    2) After sending the email using RDOMail.Send do I need to then delete the email from drafts / move the email to Sent Items, or is this done automatically?

    Thanks!

    Friday, August 5, 2016 4:36 PM
  • Hi moatak787,

    >> After sending the email using RDOMail.Send do I need to then delete the email from drafts / move the email to Sent Items, or is this done automatically?

    Have you achieved this? To be honesty, I am not familiar with RDOMail, I think you could make a test with RDOMail.Send, if it did not move automatically, you will need to move it.

    Since your issue is more related with RDOMail, I will suggest you try Contact Redemption Support. They are more familiar with it.

    Thanks for your understanding.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, August 8, 2016 8:14 AM
  • 1. yes, you can release the way you normally release COM objects.

    2. This is done automatically unless you reset the SaveSentMessageFolder property or set the DeleteAfterSubmit property to true.


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

    Monday, August 8, 2016 2:22 PM
  • Hi Dmitry,

    Thanks a lot for your help here, I have now got everything working and the emails are sending without triggering the Send event again!

    I have three smaller issues now that I can't figure out:

    1) How can I move the draft email from Drafts to Outbox after sending, currently it stays in Drafts before being sent and if I try to Move it to Outbox using the below it then just remains in the Outbox without sending.  Should I move it to the Outbox first and then use RDOMail.Send, or do I have to cast it to a new object?

    outbox = rSession.GetDefaultFolder(rdoDefaultFolders.olFolderOutbox)
    rdoMailItem.Move(outbox)
    rdoMailItem.Send()

    2) Sometimes the email stays in Drafts for a while before sending, I have tried using 

    Dim _MAPIUtils As MAPIUtils = Redemption.RedemptionLoader.new_MAPIUtils
    _MAPIUtils.DeliverNow(0, 0)

    but this doesn't seem to make it any quicker, is there anything else I can try here to make the email send more instantly?

    3) This solution doesn't seem to work when I am testing in Outlook which is connected to a Gmail account (IMAP/SMTP), I've read that the above MAPIUtils.DeliverNow should have solved that but I can't get it to send when using it, any ideas?

    Cheers,

    Tom

    Monday, August 8, 2016 2:44 PM
  • 1. Move is a function that returns the new item, not a sub:

    rdoMailItem = rdoMailItem.Move(outbox)

    2. An email can be sent from nay folder, Outbox is just an eye candy. Last time MAPIUtils.DeliverNow worked was i nOutlook 2000 - see http://www.dimastr.com/redemption/faq.htm#1

    3. You need to force Send/Receive (see the link from #2).


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

    Monday, August 8, 2016 3:05 PM
  • Ah okay got it, I note that if Outlook is started without an explorer (which might be the case for us) you recommend using the Namespace.SyncObjects collection to force the sending of the email, and that for Outlook 2010 there is Namespace.SendAndReceive method.

    Is there any reason why I should also use the simulation of clikcking the Send/Receive button or can I just use those two functions above for explorer & no explorer?

    Is there an equivalent function(s) for Outlook 2007 & 2016?

    Thanks,

    Tom

    In Outlook 2010 you can also use Namespace.SendAndReceive method
    Monday, August 8, 2016 3:39 PM
  • If you are only using Outlook 2010 or newer (including 2016), use Namespace.SendAndReceive.

    Simulating a click on the Send/Receive button is only necessary for the older versions of Outlook (such as 2007).


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

    Monday, August 8, 2016 3:55 PM
  • Perfect thanks!  Everything seems to be working well now.  Really appreciate the help!
    Monday, August 8, 2016 4:41 PM