8

我正在使用 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<Attachment> e = item.Attachments.GetEnumerator();似乎没有“看到”附件。

你知道我错过了什么吗?

非常感谢。

4

1 回答 1

19

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

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();
    }
}
于 2012-12-06T11:13:50.887 回答