Dmitry Kostochko

HowTo: Handle the Outlook ItemSend event: C#, VB.NET examples

One of the most frequent questions we get concerning the Outlook Object Model is “How to handle the Send button event?”

Well, if you want to know:

  • How to hook the ItemSend event (Send button event) in an Outlook email inspector?
  • How to prevent the message from being sent?
  • How to add / delete addressees at the moment of sending?
  • How to modify attachments when an e-mail message is being sent?
  • How to change Subject and Body of the outgoing email message?

Here is the answer to all the questions above:

You need to use the Application.ItemSend event, which is raised when Outlook sends mail or any other items. More precisely, this event is raised immediately after clicking the much talked-about Send button but before the Inspector containing the item being sent is closed. Using the ItemSend event you can do all the above, and besides that copy the original item to some folder, copy the original item and send it to other recipients, save the item to a file, etc.

Below you can see a code example showing how to process MailItem:

Private Sub adxOutlookEvents_ItemSend(ByVal sender As System.Object, _
    ByVal e As AddinExpress.MSO.ADXOlItemSendEventArgs) _
    Handles adxOutlookEvents.ItemSend
    e.Cancel = False
    Dim mailItem As Outlook._MailItem = TryCast(e.Item, Outlook._MailItem)
    If mailItem IsNot Nothing Then
        Dim olForm As ADXOlForm1 = TryCast( _
            AdxOlFormsManager1.Items(0).GetCurrentForm( _
                AddinExpress.OL.EmbeddedFormStates.Visible), ADXOlForm1)
        mailItem.Save()
        If olForm.checkBoxAddRecipient.Checked Then
            mailItem.Recipients.Add(olForm.textBoxAddRecipient.Text)
        End If
        If olForm.checkBoxModifySubject.Checked Then
            mailItem.Subject = (mailItem.Subject & " ") +_
              DateTime.Now.ToString()
        End If
        If olForm.checkBoxAddTextBody.Checked Then
            DoAddTextBody(mailItem, olForm)
        End If
        If olForm.checkBoxAddAttachment.Checked Then
            DoAddAttachment(mailItem, olForm)
        End If
        If olForm.checkBoxSaveMailItemFolder.Checked Then
            DoSaveMailItemFolder(mailItem, olForm)
        End If
        If olForm.checkBoxSaveMailItemFile.Checked Then
            DoSaveMailItemFile(mailItem, olForm)
        End If
        If olForm.checkBoxForbidSending.Checked Then
            e.Cancel = True
            System.Windows.Forms.MessageBox.Show("Action canceled!")
        End If
    End If
End Sub

In a similar way you can process other items that can be sent by Outlook, e.g. TaskRequestItem and MeetingRequestItem.

You may also be interested in:

Outlook COM add-in programming
Advanced Outlook regions: To-Do bar, Reading pane, Navigation pane

Available downloads:

The sample add-ins below were written using Add-in Express for Office and .net

C# sample Outlook add-in
VB.NET sample Outlook addin

8 Comments

  • https://secure.gravatar.com/avatar/816768e870c52fdd62a9f26868cec409?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Arijit says:

    Hi…

    Great code. I am very novis in VB and MS technology, working mainly in Oracle.

    Can you please let me know where exactly this code needs to be placed? In Tool -> Macro in outlook or some where else.
    Thanks,
    Arijit

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Arijit,

    The code above is VB.NET code, not VBA. This code (as well as the blog post itself) shows how to handle the ItemSend event in a COM add-in based on our Add-in Express for Office and .net product.

    I think you can handle the ItemSend event in VBA too, please have a look at the following MSDN article for details:
    http://msdn.microsoft.com/en-us/library/aa171284(v=office.11).aspx

  • https://secure.gravatar.com/avatar/42763fb8ebdaea37aa02a0fae00917dc?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Denis says:

    This example assumes that there is only one ‘New Mail’ at a time, as it uses hard-coded indexes:
    AdxOlFormsManager1.Items(0).FormInstances(0)

    If I create multiple ‘new mail’ instances, then this sample does not work properly.

    How to determine the right form index in ItemSend event?

    thanks

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Denis,

    Thank you for the bug report.

    You can use the GetCurrentForm method to get access to a particular instance of the embedded form. I have just modified both projects and updated the code in the post.

    Thank you!

  • https://secure.gravatar.com/avatar/3040ec749ed968382f96943a415f4eab?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Kim Bouwman says:

    Hi Dmitry

    Thanks for this code example helps me a lot to understand AddinExpress.

    I have a question about the ItemSend. When setting the ADXOlItemSendEventArgs.Cancel member to true the Inspector isn’t closed anymore. Is this normal behavior?

    How can I prevent sending for example MeetingItem Request/Responses, these item mostly don’t have an inspector at all.

    Could you give me a hint?

    Thanks

  • https://secure.gravatar.com/avatar/ab4ec2858cfdf1e44dadf8c50fae314d?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Dmitry Kostochko (Add-in Express Team) says:

    Hi Kim,

    >> When setting the ADXOlItemSendEventArgs.Cancel member to true the Inspector isn’t closed anymore. Is this normal behavior?

    I suppose yes. By setting Cancel to true you just prevents an item from being sent, and you do nothing with regard to the Inspector. So, when the user clicks the Send button nothing happens – an item is not sent and the inspector window remains open. If you have this behavior, then yes, it is normal. BTW, you can call the e.GetInspector method to access the item’s inspector and then close it by using the Close method.

    >> How can I prevent sending for example MeetingItem Request/Responses, these item mostly don’t have an inspector at all.

    This is a specificity of the ItemSend event – it raises when any Outlook item (mail, meeting request, task request, etc.) is about to be sent, regardless whether it has an inspector or not. Please see the code snippet (C#) below that processes sending mail and meeting items:

    private void adxOutlookEvents_ItemSend(object sender,
        ADXOlItemSendEventArgs e)
    {
        if (e.Item is Outlook._MailItem)
        {
            MessageBox.Show("Mail");
            Outlook._MailItem mail = e.Item as Outlook._MailItem;
            // TODO
        }
     
        if (e.Item is Outlook._MeetingItem)
        {
            MessageBox.Show("Meeting");
            Outlook._MeetingItem meeting = e.Item as Outlook._MeetingItem;
            e.Cancel = true;
            // TODO
        }
    }
  • https://secure.gravatar.com/avatar/fd951c376d9e824031d18cf222435338?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ben Shaw says:

    Hi,

    I’m writing an Addon and the functionality of it is perfect for what I want it to do. If the user is trying to send an email to a restricted user then a pop up appears warning them.

    However, this only works for when an email is sent via the send button. We use an external program which passes emails straight to the outbox and they’re sent from here. Is it possible to catch the event of an email being sent without the need for the send button to be clicked?

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Ben,

    Make sure you intercept the ItemSend event. In Add-in Express, this event is mapped to the ItemSend event of the Outlook Events component (ADXOutlookAppEvents).

    I suppose this event won’t occur if the external application sends emails using Simple MAPI or Extended MAPI. In this case, I’d try to intercept the ItemAdd event of the Outbox folder. If this event occurs for an emails sent by the external appplication, the event handler should determine whether the message had been already processed by the ItemSend event handler or not.

    If not, and if the event handler finds a recipient to be a restricted user, react e.g. by showing a message box and deleting (or moving) the email.

    When debugging the check above, pay attention to the font that Outlook uses to display a message in Outbox: it differs for emails sendable and non-sendable; if you read a sendable email (programmatically or by opening it in the Outlook UI), this makes the email non-sendable; and the font change reflects this.

    That is, if you check an email in Outbox and find the email okay for sending, you must resend it. Make sure that all event handlers foresee this scenario.

    When testing all this stuff, test also the state of the “Send immediately when connected” check box. See also https://support.microsoft.com/en-us/help/2797572/email-stays-in-the-outbox-folder-until-you-manually-initiate-a-send-re. Also, when debugging sending an email, I usually select the “Disable Scheduled Send/Receive” Ribbon check box.

    Good luck!

Post a comment

Have any questions? Ask us right now!