방법: Outlook 전자 메일 항목의 첨부 파일 저장

업데이트: 2007년 11월

적용 대상

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

프로젝트 형식

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

Microsoft Office 버전

  • Outlook 2003

  • Outlook 2007

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

이 예제에서는 지정한 폴더에 전자 메일 첨부 파일을 저장합니다.

예제

Private Sub ThisAddIn_NewMail() Handles Application.NewMail
    Dim inBox As Outlook.MAPIFolder = Me.Application.ActiveExplorer() _
        .Session.GetDefaultFolder(Outlook. _
        OlDefaultFolders.olFolderInbox)
    Dim inBoxItems As Outlook.Items = inBox.Items
    Dim newEmail As Outlook.MailItem
    inBoxItems = inBoxItems.Restrict("[Unread] = true")
    Try
        For Each collectionItem As Object In inBoxItems
            newEmail = TryCast(collectionItem, Outlook.MailItem)
            If newEmail IsNot Nothing Then
                If newEmail.Attachments.Count > 0 Then
                    For i As Integer = 1 To newEmail.Attachments.Count
                        Dim saveAttachment As Outlook.Attachment = _
                            newEmail.Attachments(i)
                        newEmail.Attachments(i).SaveAsFile _
                            ("C:\TestFileSave\" & (newEmail _
                            .Attachments(i).FileName))
                    Next i
                End If
            End If
        Next collectionItem
    Catch ex As Exception
        If Left(ex.Message, 11) = "Cannot save" Then
            MessageBox.Show("Create Folder C:\TestFileSave")
        End If
    End Try
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.NewMail += new Microsoft.Office.Interop.Outlook
        .ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
}

private void ThisApplication_NewMail()
{
    Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
        .Session.GetDefaultFolder(Outlook
        .OlDefaultFolders.olFolderInbox);
    Outlook.Items inBoxItems = inBox.Items;
    Outlook.MailItem newEmail = null;
    inBoxItems = inBoxItems.Restrict("[Unread] = true");
    try
    {
        foreach (object collectionItem in inBoxItems)
        {
            newEmail = collectionItem as Outlook.MailItem;
            if (newEmail != null)
            {
                if (newEmail.Attachments.Count > 0)
                {
                    for (int i = 1; i <= newEmail
                       .Attachments.Count; i++)
                    {
                        newEmail.Attachments[i].SaveAsFile
                            (@"C:\TestFileSave\" +
                            newEmail.Attachments[i].FileName);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        string errorInfo = (string)ex.Message
            .Substring(0, 11);
        if (errorInfo == "Cannot save")
        {
            MessageBox.Show(@"Create Folder C:\TestFileSave");
        }
    }
}

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

  • C 디렉터리의 루트에 있는 TestFileSave 폴더

참고 항목

작업

방법: 이름으로 폴더 검색

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

방법: 특정 폴더 내용 검색

개념

메일 항목 작업