首页 > 解决方案 > 我正在尝试在 C# 中创建一个插件来验证 mailItem.Body 以避免特定内容,但是当我发送消息时我的事件没有激活

问题描述

我正在尝试验证每封电子邮件正文中的特定文本。代码在会话开始时运行,但在我发送消息时它不运行。如何将此代码段与发送事件相关联?

namespace OutlookAddIn2
{

public partial class ThisAddIn
{

    Outlook.Inspectors inspectors;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(ItemSend);

    }


      void ItemSend(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        Outlook.NameSpace session = mailItem.Session;
        Outlook.Accounts accounts = session.Accounts;
        mailItem.SendUsingAccount = Application.Session.Accounts[1];

        if (mailItem.Body != null) //--- validate mail item is not empty
        {
            string strBody;
            string prompt;
            // ---inicialize variables
            strBody = mailItem.Body.ToUpper();
            //--- validate content
            if (strBody.Contains("SPECIAL TEXT"))
            {
                prompt = "This email look like content Special Text information. Do you want to send it anyway?";

                DialogResult result;

                result = MessageBox.Show(prompt, "Warning", MessageBoxButtons.YesNo);

                //-- give them chance to send it or not
                if (result == DialogResult.Yes)
                {
                    mailItem.Send();
                }

            }
        }
    }

标签: c#outlook-addin

解决方案


感谢您的回答。这是我第一次尝试为 Outlook 编程。您是否有任何示例或指南可遵循来执行此操作。通过您的评论,我找到了额外的信息,我可以在 Vb 中完成我的加载项。我粘贴我的代码以供将来参考。再次感谢

导入 Outlook = Microsoft.Office.Interop.Outlook 导入 Microsoft.Office.Tools

公共类 ThisAddIn

区域“类级别声明”

Private m_objOutlook As Outlook.Application
Private WithEvents m_objNS As Outlook.NameSpace

'Event-aware references to Explorers collection & ActiveExplorer
Private WithEvents m_colExplorers As Outlook.Explorers
Private WithEvents m_olExplorer As Outlook.Explorer

'Event-aware references to Inspectors collection & ActiveInspector
Private WithEvents m_colInspectors As Outlook.Inspectors
Private WithEvents m_olInspector As Outlook.Inspector
Dim Items As Outlook.Items
Dim Item As Object
'  Dim ol As OutlookHandler
Public Delegate Sub ApplicationEvents_11_ItemSendEventHandler(ByVal Item As Object, ByRef Cancel As Boolean)

结束区域

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    ' Store a reference to the Outlook.Application object.
    m_objOutlook = Me.Application
    Items = Application.ActiveExplorer.CurrentFolder.Items


    'Item = Items.Add()
    'Item.Display

    AddHandler m_objOutlook.ItemSend, AddressOf MyItemSendEventHandler



End Sub

Public Sub MyItemSendEventHandler(ByVal Item As Object, ByRef Cancel As Boolean)

    '--- this macro advice about confidential information in the content.
    '--- create variables to save message body and a counter of confidential words
    Dim strBody As String
    Dim intConfidential As Integer
    Dim prompt As String
    '--- inicialize variables
    strBody = UCase(Item.Body)
    intConfidential = InStr(1, strBody, "SPECIAL TEXT", 1)
    '--- validate content
    If intConfidential >= 1 Then
        prompt = "API: This email look like content confidential information. Do you want to send it anyway?"
        '-- givethem chance to sendit or not
        If MsgBox(prompt, vbYesNo + vbQuestion + vbMsgBoxSetForeground, "Check Body Text") = vbNo Then
            Cancel = True
        End If
    End If

End Sub


Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

End Sub

推荐阅读