Exchange Web服务API:收到邮件的附件(Exchange Web Services AP

2019-08-03 14:09发布

我使用的是EWS 1.2 API来访问我们的Exchange服务器上的邮箱。 这只是工作正常,但有一件事我不能达到:收到邮件的附件。

林写了下面几行:

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(); 好像不是“看”的附件。

你有什么我错过任何想法?

非常感谢。

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


文章来源: Exchange Web Services API : get mail attachments