使用Microsoft exchange web服务从邮件中读取附件

本文关键字:读取 Microsoft exchange web 服务 使用 | 更新日期: 2023-09-27 18:13:26

我们有下面的代码来使用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()时,上面的代码正在工作。当线程。当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 

专家,请提供宝贵意见

使用Microsoft exchange web服务从邮件中读取附件

你的逻辑看起来不正确eg

 item.Move(unreadmailFolder.Id);
如果你想在最后移动消息,

不应该在Foreach循环中,只要使用Flag并在完成处理附件后处理它(如果你有两个ItemAttachment,你的逻辑就不工作了,这将执行两次)。睡眠工作的原因很可能是一旦执行了移动,Aysnc操作就完成了。这就是为什么它应该在foreach附件循环之外

相关文章: