共用方式為


HOW TO:在收到電子郵件訊息時執行動作

更新:2011 年 4 月

這個範例會在使用者收到電子郵件訊息時執行自訂動作。

**適用於:**本主題中的資訊適用於 Outlook 2007 和 Outlook 2010 的應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

範例

Private Sub ThisAddIn_NewMail() Handles Application.NewMail
    Dim filter As String = "USED CARS"

    Dim outlookNameSpace As Outlook.NameSpace = Me.Application.GetNamespace("MAPI")
    Dim inbox As Outlook.MAPIFolder = _
        outlookNameSpace.GetDefaultFolder( _
        Outlook.OlDefaultFolders.olFolderInbox)
    Dim items As Outlook.Items = inbox.Items

    items.Restrict("[Unread] = true")

    ' If the mail item matches the specified filter,
    ' move it to the junk e-mail folder.
    For Each mail As Outlook.MailItem In items
        If mail.MessageClass = "IPM.Note" And _
            mail.Subject.ToUpper.Contains(filter.ToUpper) Then
            mail.Move(outlookNameSpace.GetDefaultFolder( _
                Outlook.OlDefaultFolders.olFolderJunk))
        End If
    Next
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.NewMail += new Microsoft.Office.Interop.
        Outlook.ApplicationEvents_11_NewMailEventHandler(
        ThisApplication_NewMail);
}

void ThisApplication_NewMail()
{
    string filter = "USED CARS";

    Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
    Outlook.MAPIFolder inbox = outlookNameSpace.GetDefaultFolder(
        Microsoft.Office.Interop.Outlook.
        OlDefaultFolders.olFolderInbox);

    Outlook.Items items = inbox.Items;
    items.Restrict("[Unread] = true");

    // If the mail item matches the specified filter,
    // move it to the junk e-mail folder.
    foreach (Outlook.MailItem mail in items)
    {
        if (mail.MessageClass == "IPM.Note" &&
            mail.Subject.ToUpper().Contains(filter.ToUpper()))
        {
            mail.Move(outlookNameSpace.GetDefaultFolder(
                Microsoft.Office.Interop.Outlook.
                OlDefaultFolders.olFolderJunk));
        }
    }
}

請參閱

工作

HOW TO:在 Office 專案中建立事件處理常式

概念

使用郵件項目

應用程式層級增益集程式設計入門

變更記錄

日期

記錄

原因

2011 年 4 月

根據最近一些客戶回函來更新程式碼範例。

客戶回函。