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

发布于 2024-09-05 08:50:34 字数 2146 浏览 1 评论 0原文

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

我已经连接到 EWS,并且能够循环浏览任何电子邮件,但在创建可用于访问附件的 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);
        }
    }

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

I'm in the process of writing a simple console app that monitors a particular exchange mailbox, and when emails meeting particular criteria are received, the app will download an XML file attachment, and archive the email.

I've connected to EWS OK, and have been able to loop through any emails, but I'm struggling when it comes to create an EmailMessage object which I can use to access the attachments.

In the example code below, the EmailMessage message = EmailMessage.Bind(...) line executes without error, but doesn't return a valid message so when I access and properties or methods, I get an error: 'Object reference not set to an instance of an object'.

I'm new to C# let alone EWS so I'm struggling to know where to start...

Code Snippet:

    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);
        }
    }

My code for accessing the attachments is straight from MSDN so I'm hoping it is there are thereabouts... Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-09-12 08:50:34

恐怕我重新审视了这个问题并设法解决了它。不幸的是,我当时压力太大,无法回到这里并记录解决方案。随着时间的流逝,我对所做更改的记忆已经消失,但据我所知,这是一行更改:

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

这里的关键区别是我们已将 BasePropertySet.FirstClassProperties 指定为第一个PropertySet 的参数,而不是我们原来的 BasePropertySet.IdOnly

我的原始代码是从一个在线示例中提取出来的,该示例正是我想要实现的目标,因此要么该示例不太正确,要么我错误地转录了它或误解了问题的某些方面。

I'm afraid I revisited this problem and managed to cure it. Unfortunately I was too pressed at the time to come back here and document the solution. Time having passed, and my memory of what I changed has faded, but as far as I can remember it was a one line change:

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

The key difference here is that we have specified BasePropertySet.FirstClassProperties as the first parameter of PropertySet, rather than the BasePropertySet.IdOnly that we originally had.

My original code was lifted from an example online that did precisely what I was trying to achieve, so either the example wasn't quite right, or I transcribed it incorrectly or misunderstood some facet of the problem.

白昼 2024-09-12 08:50:34
foreach(EmailMessage message in findResults)
{
    message.Load();

    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);
    }
}
foreach(EmailMessage message in findResults)
{
    message.Load();

    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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文