I';我试图在C#中创建一个外接程序来验证mailItem.Body以避免特定内容,但我的事件没有';当我发送消息时无法激活

I';我试图在C#中创建一个外接程序来验证mailItem.Body以避免特定内容,但我的事件没有';当我发送消息时无法激活,c#,outlook-addin,C#,Outlook Addin,我试图验证每封电子邮件正文中的特定文本。代码在会话开始时运行,但在我发送消息时不运行。如何将此代码段与发送事件关联 namespace OutlookAddIn2 { public partial class ThisAddIn { Outlook.Inspectors inspectors; private void ThisAddIn_Startup(object sender, System.EventArgs e) { inspectors

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

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();
                }

            }
        }
    }

首先,您从未设置
MailItem.Send
事件处理程序


其次,您不需要-只需使用
Application.ItemSend
event即可。

谢谢您的回答。这是我第一次尝试为outlook编程。您是否有任何样本或指南可供遵循。 通过您的评论,我找到了额外的信息,我可以在Vb中完成我的外接程序。我粘贴代码以备将来参考。再次感谢

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

公开课

区域“类级声明” 末端区域
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