Exchange Web服务 - 处理邮件和访问附件

CJM*_*CJM 7 c# attachment exchangewebservices ews-managed-api

我正在编写一个监视特定交换邮箱的简单控制台应用程序,当收到符合特定条件的电子邮件时,应用程序将下载XML文件附件并存档电子邮件.

我已经连接到EWS OK,并且已经能够遍历任何电子邮件,但是我在创建一个可以用来访问附件的EmailMessage对象时遇到了困难.

在下面的示例代码中,该EmailMessage message = EmailMessage.Bind(...)行执行时没有错误,但不返回有效消息,因此当我访问和属性或方法时,我收到一个错误:'对象引用未设置为对象的实例'.

我是C#的新手,更不用说EWS,所以我很难知道从哪里开始......

代码片段:

    public static void FindItems()
    {
        try
        {
            ItemView view = new ItemView(10);
            view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
            view.PropertySet = new PropertySet(
                BasePropertySet.IdOnly,
                ItemSchema.Subject,
                ItemSchema.DateTimeReceived);

            findResults = service.FindItems(
                WellKnownFolderName.Inbox,
                new SearchFilter.SearchFilterCollection(
                    LogicalOperator.Or,
                    new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sales Enquiry")),
                view);

            log2.LogInfo("Total number of items found: " + findResults.TotalCount.ToString());

            foreach (Item item in findResults)
            {
                log2.LogInfo(item.Id);

                EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));

                Console.WriteLine(message.Subject.ToString());

                if (message.HasAttachments && message.Attachments[0] is FileAttachment)
                {
                    FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
                    fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
                    fileAttachment.Load();
                    Console.WriteLine("FileName: " + fileAttachment.FileName);
                }
            }
        }
        catch (Exception ex)
        {
            log2.LogError(ex.InnerException);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我访问附件的代码直接来自MSDN,所以我希望它有关于...有什么想法吗?

CJM*_*CJM 13

我担心我重新审视这个问题并设法治愈它.不幸的是,我当时太紧迫了回到这里并记录解决方案.时间过去了,我对改变的记忆已经消失了,但据我记忆,这是一个改变:

EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));
Run Code Online (Sandbox Code Playgroud)

这里的关键区别是我们已经指定BasePropertySet.FirstClassProperties了PropertySet的第一个参数,而不是BasePropertySet.IdOnly我们最初的参数.

我的原始代码是从一个在线的例子中提取出来的,这正是我想要实现的,所以要么这个例子不太正确,要么我错误地转录它或者误解了问题的某些方面.