【问题标题】:Exchange Web Services API : get mail attachmentsExchange Web 服务 API:获取邮件附件
【发布时间】:2012-11-23 10:17:35
【问题描述】:

我正在使用 EWS API 1.2 访问我们 Exchange Server 上的邮箱。 这很好用,但有一件事我无法实现:获取邮件附件。

我写了以下几行:

class Program
{
    public static void Main(string[] args)
    {
        try {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new WebCredentials("login","password");
            service.AutodiscoverUrl("mail@domaine.fr");

            ItemView view = new ItemView(10);
            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

            if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
                foreach (Item item in findResults.Items)
                {
                    if (item.Attachments != null)
                    {
                        IEnumerator<Attachment> e = item.Attachments.GetEnumerator();
                    }   
                    Console.WriteLine(item.Subject);
                }
            else
                Console.WriteLine("no items");
        } 
        catch (Exception e) {
            Console.WriteLine(e.Message);
        }
        Console.ReadLine();
    }
}

我收到了测试邮箱中的所有邮件,但IEnumerator&lt;Attachment&gt; e = item.Attachments.GetEnumerator(); 似乎没有“看到”附件。

你知道我错过了什么吗?

非常感谢。

【问题讨论】:

  • 你怎么知道,它没有“看到”它们?您没有以任何方式使用枚举器...
  • Daniel :我实际上在枚举器上有一个断点,列表是空的。
  • 下面代码而不是调用GetEnumerator的结果是什么? var count = item.Attachments.Count();
  • 丹尼尔:count()的结果是0
  • 对了,ExchangeService是怎么得到的?

标签: c# .net exchange-server


【解决方案1】:

我终于设法获得了电子邮件附件。我修改了我的代码如下

class Program
{
    public static void Main(string[] args)
    {
        try {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
            service.Credentials = new WebCredentials("login","pwd");
            service.AutodiscoverUrl("mail@domaine.com");

            ItemView view = new ItemView(10);
            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(10));

            if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
                foreach (Item item in findResults.Items)
                {
                    EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments));
                    foreach (Attachment attachment in message.Attachments)
                    {
                        if (attachment is FileAttachment)
                        {
                            FileAttachment fileAttachment = attachment as FileAttachment;
                            fileAttachment.Load();
                            Console.WriteLine("Attachment name: " + fileAttachment.Name);
                        }
                    }
                    Console.WriteLine(item.Subject);
                }
            else
                Console.WriteLine("no items");
        } catch (Exception e) {

            Console.WriteLine(e.Message);
        }
        Console.ReadLine();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多