简体   繁体   English

Outlook 加载项开发:防止转发邮件“作为附件”

[英]Outlook Add-in Development : Prevent Forwarding Mail 'As Attatchment'

How to detect 'Forward As Attatchment' event in C# Outlook Add-in development.如何在 C# Outlook 加载项开发中检测“转发为附件”事件。

I want to show message 'You can't forward this mail as attatchment' in message box.我想在消息框中显示消息“您不能将此邮件作为附件转发”。

Note that, This is not VSTO application.请注意,这不是 VSTO 应用程序。

There is no straight scenario or trivial way for handling such cases in Outlook. There are several ways to handle such scenario as possible workarounds.在 Outlook 中没有处理此类情况的直接方案或简单方法。有几种方法可以处理此类情况作为可能的解决方法。

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.第一种可能的解决方案是处理MailItem.Forward事件,该事件在用户为某个项目选择Forward操作时触发,或者在为该项目调用Forward方法时触发,该项目是父 object 的一个实例。在事件处理程序中您可以检查附加文件、显示消息框并在需要时取消操作。 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.第二种可能的解决方案是在 Outlook 中处理Application class 的ItemSend事件,您可以在其中处理所有外发电子邮件,而不仅仅是转发。

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.第三种解决方案是重新调整负责 Outlook 中的操作的 UI 控件的用途。因此,您可以将默认操作替换为您自己的操作,或者只是在其前面添加您的自定义逻辑。 See Temporarily Repurpose Commands on the Office Fluent Ribbon for more information.有关详细信息,请参阅临时重新利用 Office Fluent 功能区上的命令

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". Outlook Object Model 没有明确(通过其类型库)公开OnForwardAsAttachment事件(仅Reply / ReplyAll / Forward事件),即使当用户单击“作为附件转发”时它会触发 dispid 为0xF618的事件。

If using Redemption is an option (I am its author), it exposes a cancelable SafeMailItem .如果使用Redemption是一个选项(我是它的作者),它会公开一个可取消的SafeMailItem OnForwardAsAttachment event: OnForwardAsAttachment事件:

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}'.";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM