0

I am beginning to have a look at Outlook addin using C# and want to know how to get notified in my addin when Send email is clicked. Is this possible in addin?

Also I want to know the email that was sent along with it's title, body, to address. I am a beginner in addin and completely confused how to achieve this.

Mrug
  • 481
  • 1
  • 6
  • 21
  • Start by looking at the Microsoft documentation. https://learn.microsoft.com/en-us/outlook/add-ins/ – Stephen Wilson Sep 27 '18 at 10:41
  • This site it for answering specific questions. Try including some code and explanations of what you've tried – Red Sep 27 '18 at 10:50

3 Answers3

3

You can use Application.ItemSend event for that. The item being sent will be passed as an argument to your event handler. You can check that you get a MailItem object (you can also have MeetingItem etc.) by trying to cast the object to MailItem.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
1

You didn't specify what add-in technology you're using, but as you mentioned C# then I'll assume that you're using Microsoft.Office.Interop.Outlook

It's possible to capture the send event of an EmailItem. You can retrieve EmailItem object and access its contents by using Inspector.

Sample code:

    private void Inspectors_NewInspectorEvent(Outlook.Inspector inspector)
    {
        var currentAppointment = inspector.CurrentItem as Outlook.MailItem;
        ((Outlook.ItemEvents_10_Event)currentAppointment).Send += ThisAddIn_Send;
    }

    private void ThisAddIn_Send(ref bool Cancel)
    {
        //Handle send event
    }

If you're creating a web add-in using Office.js, send event is currently only available in Office365 OWA. Here's the reference

Update to include Dmitry's comment:

You should use Application.Itemsend, you will then need to check if the being sent object is an email.

  • 1
    There is absolutely no reason to use MailItem.Send event - you will need to track each and every MailItem object that can potentially be sent. Your code above miss messages sent without being open in an inspector - a user can reply/forward an email inline. Use Application.ItemSend event. – Dmitry Streblechenko Sep 27 '18 at 14:08
0

I'm not sure if you use Web add-in technology, but here is a sample about WEB Outlook add-in on send code. The main code as below:

// Check if the subject should be changed. If it is already changed allow send, otherwise change it.
// <param name="subject">Subject to set.</param>
// <param name="event">MessageSend event passed from the calling function.</param>
function subjectOnSendChange(subject, event) {
    mailboxItem.subject.setAsync(
        subject,
        { asyncContext: event },
        function (asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                mailboxItem.notificationMessages.addAsync('NoSend', { type: 'errorMessage', message: 'Unable to set the subject.' });

                // Block send.
                asyncResult.asyncContext.completed({ allowEvent: false });
            }
            else {
                // Allow send.
                asyncResult.asyncContext.completed({ allowEvent: true });
            }

        });
}

More information please see, How to hook event on sending mail in Office add-in (OWA, Windows Outlook 2016)

How To: Change an Outlook e-mail message before sending using C#

Simon Li
  • 303
  • 2
  • 4