【问题标题】:Reading attachments from mail using Microsoft exchange web service使用 Microsoft Exchange Web 服务从邮件中读取附件
【发布时间】:2016-10-18 11:17:47
【问题描述】:

我们有以下代码可以使用 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 

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

【问题讨论】:

  • 请格式化您的代码,使其更具可读性
  • 我已经格式化了。

标签: c# multithreading exception exchangewebservices


【解决方案1】:

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

 item.Move(unreadmailFolder.Id);

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

【讨论】:

  • 嗨,格伦,你是对的。项目附件中的逻辑不正确,我会纠正它。这部分代码永远不会执行,因为我们的邮件不包含任何项目附件。我的查询是关于文件附件部分,当我引入睡眠时,附件会被读取,但是当它被删除时,我得到一个错误。我们的逻辑中没有任何异步操作。我想知道这是否与许可等有关,但由于它适用于睡眠,我也排除了该选项。
  • 但是我们有不同的线程处理不同的邮箱。这有什么影响吗?
  • 如果您有多个线程在同一个项目上工作,那么是的,您可能会发生更新冲突。
猜你喜欢
  • 2012-11-23
  • 2011-08-02
  • 2011-08-24
  • 2011-10-12
  • 2012-07-13
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
相关资源
最近更新 更多