방법: 전자 메일 메시지를 받은 경우 작업 수행

업데이트: 2007년 11월

적용 대상

이 항목의 정보는 지정된 Visual Studio Tools for Office 프로젝트 및 Microsoft Office 버전에만 적용됩니다.

프로젝트 형식

  • 응용 프로그램 수준 프로젝트

Microsoft Office 버전

  • Outlook 2003

  • Outlook 2007

자세한 내용은 응용 프로그램 및 프로젝트 형식에 따라 사용 가능한 기능을 참조하십시오.

이 예제에서는 사용자가 전자 메일 메시지를 받을 때 사용자 지정 작업을 수행하도록 Microsoft.Office.Tools.Outlook.Application.NewMail 이벤트를 사용합니다.

예제

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

참고 항목

작업

방법: Visual Studio Tools for Office에서 이벤트 처리기 만들기

개념

메일 항목 작업

응용 프로그램 수준 추가 기능 프로그래밍 시작