vsto outlook自动读取邮件

通过使用Vsto Outlook读取邮箱Inbox的邮件, 并将邮件的所有扩展后缀为xls|xlsx|XLS|XLSX的文件保存到配置目录.

这个应该怎么去实现呢.

可以通过使用Microsoft.Office.Interop.Outlook的.NET组件来实现此功能。代码示例如下:

using Microsoft.Office.Interop.Outlook;
using System.IO;

namespace ReadOutlookEmails
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建Outlook应用程序对象
            Application outlookApplication = new Application();

            // 获取收件箱
            Folder inbox = outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

            // 遍历所有邮件
            foreach (MailItem mail in inbox.Items)
            {
                // 检查邮件是否包含附件
                if (mail.Attachments.Count > 0)
                {
                    // 遍历所有附件
                    foreach (Attachment attachment in mail.Attachments)
                    {
                        // 如果附件为.xlsx或.xlsx文件,则保存
                        if (attachment.FileName.EndsWith(".xlsx") || attachment.FileName.EndsWith(".xls"))
                        {
                            // 指定配置目录
                            string savePath = Path.Combine(@"C:\ConfigDirectory", attachment.FileName);

                            // 保存附件
                            attachment.SaveAsFile(savePath);
                        }
                    }
                }
            }
        }
    }
}

请注意,需要引用Microsoft.Office.Interop.Outlook组件,并且需要在系统上安装Outlook。