使用Microsoft Exchange Web服务从邮件中读取附件

时间:2016-10-18 11:17:48

标签: c# multithreading exception exchangewebservices

我们使用以下代码来阅读使用EWS的附件。

     FindItemsResults<Item> foundItems = service.FindItems(FolderId, new ItemView(1000));

    var orderItems = from list in foundItems
                     orderby list.DateTimeReceived
                     select list;
    foreach (EmailMessage item in orderItems)
    {
        item.Load();//SARANYA
        EmailMessage foundEmail = (EmailMessage)item;
        EmailMessage message = EmailMessage.Bind(service, new ItemId(item.Id.ToString()), new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments, ItemSchema.Body));
        if (message.Attachments.Count > 0)
        {
            FileAttachment[] attachments = null;
            attachments = new FileAttachment[message.Attachments.Count];

            foreach (Attachment attachment in message.Attachments)
            {
                try
                {
                    if (attachment is FileAttachment)
                    {

                        FileAttachment fileAttachment = attachment as FileAttachment;
                        //   System.Threading.Thread.Sleep(2000);

                        fileAttachment.Load();
                        byte[] FolloupMailFileAttachmentContentBytes = fileAttachment.Content;
                        bool isScreenshot = false;
                        string ScreenfileName = "";

                        for (int i = 0; i < imgSrcs.Count; i++)
                        {
                            if (imgSrcs[i].ToString() == fileAttachment.Name.ToString())
                            {
                                isScreenshot = true;
                                if (!imgSrcs[i].ToString().Contains(".png"))
                                    ScreenfileName = "cid:" + imgSrcs[i].ToString() + ".png";
                                else
                                    ScreenfileName = "cid:" + imgSrcs[i].ToString();
                                break;
                            }
                        }

                        if (FolloupMailFileAttachmentContentBytes != null)
                        {
                            if (isScreenshot && RemoveSuccess == 1)
                            {
                                InsertMailItemAttachment(ScreenfileName, FolloupMailFileAttachmentContentBytes, caseid);
                            }
                            else
                                InsertMailItemAttachment(fileAttachment.Name.ToString(), FolloupMailFileAttachmentContentBytes, caseid);
                        }
                    }
                    else if (attachment is ItemAttachment)
                    {
                        item.Move(unreadmailFolder.Id);
                    }
                }
                catch (Exception exe)
                {
                    if (!ReadMoved)
                    {
                        item.Move(readmailFolder.Id);
                        ReadMoved = true;
                    }
                    logfile.HandleError(exe, "Attachment Exception \n\nEmailbox - " + EMailBox + "\n\nEmail Subject - " + strSubject + " \n - Could not load the attachment (" + attachment.Name.ToString() + ")");

                }
            }
        }
    }

当我在fileattachment.load()之前提供thread.sleep()时,上面的代码正在工作。当删除thread.sleep时,我得到以下异常。

  Error Source        : Microsoft.Exchange.WebServices
Target Site         :   Void InternalThrowIfNecessary()
System Message      :   The specified object was not found in the store.
Stack Trace         :      at Microsoft.Exchange.WebServices.Data.ServiceResponse.InternalThrowIfNecessary()
   at Microsoft.Exchange.WebServices.Data.ServiceResponse.ThrowIfNecessary()
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalGetAttachments(IEnumerable`1 attachments, Nullable`1 bodyType, IEnumerable`1 additionalProperties, ServiceErrorHandling errorHandling)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAttachment(Attachment attachment, Nullable`1 bodyType, IEnumerable`1 additionalProperties)
   at Microsoft.Exchange.WebServices.Data.Attachment.InternalLoad(Nullable`1 bodyType, IEnumerable`1 additionalProperties)
   at Microsoft.Exchange.WebServices.Data.Attachment.Load()
   at EMT_Office365_MailFetch_Scheduler.Program.FindEmail(Object threadState) in 

专家,请提供您宝贵的意见

1 个答案:

答案 0 :(得分:0)

你的逻辑看起来不正确,例如

 item.Move(unreadmailFolder.Id);

如果你想在最后移动消息,不应该在Foreach循环内部只需使用一个Flag并在完成处理附件后处理它(如果你有两个ItemAttachment,那么你的逻辑不起作用)。睡眠工作的原因很可能是Aysnc操作在您执行移动后即将完成。这就是为什么这应该在foreach附件循环之外