다음을 통해 공유


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

업데이트: 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));
        }
    }
}

참고 항목

작업

방법: Office 프로젝트에서 이벤트 처리기 만들기

개념

메일 항목 작업

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

변경 기록

날짜

변경 내용

이유

2011년 4월

최근의 사용자 의견을 반영하여 코드 예제를 업데이트했습니다.

고객 의견