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:
The sample add-ins below were written using Add-in Express for Office and .net