c# office怎樣讀取郵件附件

c#
小樊
95
2024-07-26 20:08:14

您可以使用Microsoft Office Outlook Interop庫(kù)來讀取郵件附件。以下是一個(gè)示例代碼,演示如何讀取郵件附件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Outlook;

namespace ReadEmailAttachments
{
    class Program
    {
        static void Main(string[] args)
        {
            Application outlookApp = new Application();
            NameSpace outlookNamespace = outlookApp.GetNamespace("MAPI");
            MAPIFolder inbox = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

            foreach (MailItem mail in inbox.Items)
            {
                foreach (Attachment attachment in mail.Attachments)
                {
                    attachment.SaveAsFile(@"C:\Attachments\" + attachment.FileName);
                }
            }

            Console.WriteLine("Attachments saved successfully!");
            Console.ReadLine();
        }
    }
}

在這個(gè)示例代碼中,我們首先創(chuàng)建了一個(gè)Outlook應(yīng)用程序?qū)ο螅@取了默認(rèn)收件箱的文件夾。然后,我們遍歷收件箱中的每封郵件,并遍歷每封郵件的附件。最后,我們保存每個(gè)附件到指定的文件夾中。

請(qǐng)注意,您需要在項(xiàng)目中引用Microsoft.Office.Interop.Outlook庫(kù),并確保計(jì)算機(jī)上安裝了Outlook應(yīng)用程序。

0