Exchange Web Services API : get mail attachments

房东的猫 提交于 2020-01-11 20:07:21

问题


I am using the EWS API 1.2 to access mailboxes on our Exchange Server. This just works fine but there is one thing I cannot achieve : get mail attachments.

Im wrote the following lines :

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

I get all mails in the tested mailbox but IEnumerator<Attachment> e = item.Attachments.GetEnumerator(); seems not to "see" attachments.

Have you got any idea of what I missed ?

Thanks a lot.


回答1:


I finally managed to get email attachments. I modified my code as below

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


来源:https://stackoverflow.com/questions/13725774/exchange-web-services-api-get-mail-attachments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!