Home > Back-end >  Outlook Add-in Development : Prevent Forwarding Mail 'As Attatchment'
Outlook Add-in Development : Prevent Forwarding Mail 'As Attatchment'

Time:12-31

How to detect 'Forward As Attatchment' event in C# Outlook Add-in development.

I want to show message 'You can't forward this mail as attatchment' in message box.

Note that, This is not VSTO application.

CodePudding user response:

There are several ways to handle such scenario.

The first possible solution is to handle the MailItem.Forward event which is fired when the user selects the Forward action for an item, or when the Forward method is called for the item, which is an instance of the parent object. In the event handler you may check for attached files, display a message box and cancel the action if required. To handle item-lvel events you may consider creating an inspector wrapper (or item-wrapper) where you could set up event handlers correctly, see Implement a wrapper for inspectors and track item-level events in each inspector for more information.

The second possible solution is to handle the ItemSend event of the Application class in Outlook where you could handle all outgoing emails, not only forwarded.

The third solution is to repurpose the UI control which is responsible for the action in Outlook. So, you may replace the default action with your own or just prepend it with your custom logic. See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.

CodePudding user response:

Outlook Object Model does not explicitly (through its type library) expose the OnForwardAsAttachment event (only Reply/ReplyAll/Forward events), even though it fires an event with dispid of 0xF618 when a user clicks "Forward as Attachment".

If using Redemption is an option (I am its author), it exposes a cancelable SafeMailItem.OnForwardAsAttachment event:

private SafeMailItem _sItem;
private MailItem _oItem;
...
_oItem = _application.ActiveExplorer().Selection[1];
_sItem = new SafeMailItem();
_sItem.Item = _oItem;
_sItem.ForwardAsAttachment  = OnForwardAsAttachment;
...
private void OnForwardAsAttachment(object Forward, ref bool Cancel)
{
    MailItem newMessage = (MailItem)Forward;
    if (OlSensitivity.olConfidential == _oItem.Sensitivity)
    {
        MessageBox.Show($"Confidential message '{_oItem.Subject}' cannot be forwarded");
        Cancel = true;
    }
    else
    {
        newMessage.Subject = _oItem.Subject;
        newMessage.Body = $"Please see the attached message '{_oItem.Subject}'.";
    }
}
  • Related