【问题标题】:Find all unread emails using Exchange Web Service 2010 then mark as read?使用 Exchange Web Service 2010 查找所有未读电子邮件然后标记为已读?
【发布时间】:2013-06-07 17:18:38
【问题描述】:

我正在使用 Exchang Web Services 2010 尝试阅读邮箱中的所有未读电子邮件,然后将它们标记为已读。

我以这个例子为基础:

http://msdn.microsoft.com/en-us/library/exchange/aa563373(v=exchg.140).aspx

并想出了这个来查找所有未读的电子邮件,并阅读正文内容:

        //Set up the connection to exchange service
        ExchangeServiceBinding exchangeService = new ExchangeServiceBinding();
        exchangeService.Credentials = new NetworkCredential("userid", "password", "domain");
        exchangeService.Url = "https://exchangeserver/ews/exchange.asmx";

        //REturn all properties
        FindItemType findType = new FindItemType();
        findType.Traversal = ItemQueryTraversalType.Shallow;
        findType.ItemShape = new ItemResponseShapeType();
        findType.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

        //Only search the inbox
        DistinguishedFolderIdType[] foldersToSearch = new DistinguishedFolderIdType[1];
        foldersToSearch[0] = new DistinguishedFolderIdType();
        foldersToSearch[0].Id = DistinguishedFolderIdNameType.inbox;
        findType.ParentFolderIds = foldersToSearch;

        //Only unread emails
        RestrictionType restriction = new RestrictionType();
        IsEqualToType isEqualTo = new IsEqualToType();
        PathToUnindexedFieldType pathToFieldType = new PathToUnindexedFieldType();
        pathToFieldType.FieldURI = UnindexedFieldURIType.messageIsRead;

        //Not IsRead
        FieldURIOrConstantType constantType = new FieldURIOrConstantType();
        ConstantValueType constantValueType = new ConstantValueType();
        constantValueType.Value = "0";
        constantType.Item = constantValueType;
        isEqualTo.Item = pathToFieldType;
        isEqualTo.FieldURIOrConstant = constantType;
        restriction.Item = isEqualTo;

        //set the not IsRead restriction
        findType.Restriction = restriction;

        try
        {
            FindItemResponseType findResponse = exchangeService.FindItem(findType);
            ResponseMessageType[] responseMessType = findResponse.ResponseMessages.Items;

            List<ItemIdType> unreadItemIds = new List<ItemIdType>();

            //get all unread item IDs
            foreach (ResponseMessageType respMessage in responseMessType)
            {
                if(respMessage is FindItemResponseMessageType)
                {
                    FindItemResponseMessageType itemResponse = (FindItemResponseMessageType)respMessage;

                    if (itemResponse.ResponseClass == ResponseClassType.Success)
                    {
                        if (itemResponse.RootFolder.Item != null)
                        {

                            if (itemResponse.RootFolder.Item is ArrayOfRealItemsType)
                            {
                                ArrayOfRealItemsType items = (ArrayOfRealItemsType)itemResponse.RootFolder.Item;

                                if (items.Items != null)
                                {
                                    ItemType[] itemTypes = items.Items;

                                    foreach (ItemType item in itemTypes)
                                    {
                                        if (item is MessageType)
                                        {
                                            unreadItemIds.Add(item.ItemId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (unreadItemIds.Count == 0)
                MessageBox.Show("No unread emails found");
            else
            {
                //Get all unread mail messages, display body
                GetItemType getItemType = new GetItemType();
                getItemType.ItemShape = new ItemResponseShapeType();
                getItemType.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
                getItemType.ItemShape.BodyType = BodyTypeResponseType.Text;
                getItemType.ItemShape.BodyTypeSpecified = true;
                getItemType.ItemIds = unreadItemIds.ToArray();

                GetItemResponseType getItemResponse = exchangeService.GetItem(getItemType);

                if(getItemResponse.ResponseMessages != null)
                {
                    ArrayOfResponseMessagesType responseMessages = getItemResponse.ResponseMessages;
                    foreach(ResponseMessageType responseMessage in responseMessages.Items)
                    {
                        if (responseMessage is ItemInfoResponseMessageType)
                        {
                            ItemInfoResponseMessageType responseItemInfo = (ItemInfoResponseMessageType)responseMessage;
                            if (responseItemInfo.Items != null)
                            {
                                ArrayOfRealItemsType responseRealItems = (ArrayOfRealItemsType)responseItemInfo.Items;

                                if (responseRealItems.Items != null)
                                {
                                    foreach (ItemType responseItemType in responseRealItems.Items)
                                    {
                                        if (responseItemType is MessageType)
                                        {
                                            MessageType fullMessage = (MessageType)responseItemType;

                                            BodyType body = fullMessage.Body;

                                            if (body != null)
                                            {
                                                MessageBox.Show(body.Value);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        catch(Exception ee)
        {
            MessageBox.Show(ee.Message +" " + (ee.InnerException ?? new Exception("")).Message);
        }

这确实会返回所有未读电子邮件正文的文本版本,但是必须有更有效的方法,不是吗?

有谁知道我如何将电子邮件MessageTypes 更新为已读并将其发送回服务器?

【问题讨论】:

    标签: c# soap exchangewebservices exchange-server-2010


    【解决方案1】:

    没有使用 EWS v1 将 MessageType 标记为已读的好方法。如果您无法使用 EWS v1,请参阅 this MSDN blog post 了解解决方法。

    EWS v2 将 IsRead property 引入为可写 (message.IsRead = true; message.Update();),使其变得微不足道。您可以将 EWS v2 托管 API 用于 Exchange 2007 及更高版本,但它是一个单独的安装。请参阅MSDN page on EWS Managed API 2.0 了解更多信息。

    【讨论】:

    • 谢谢 Mitch,但您确定您链接到了正确的博客文章吗?
    • 我将 EWS 2.0 与 Exchange 2013 一起使用,并且我没有来自消息对象的任何 IsRead 属性。我以“Is”开头:IsAssociated、IsAttachment、IsDirty、IsDraft、IsFromMe、IsNew、IsReminderSet、IsResend、IsSubmitted 和 IsUnmodified。我是否必须激活某些东西或使用 property 的属性?如果有人可以请帮助我,谢谢。
    • 听起来您正在处理Item (msdn.microsoft.com/en-us/library/…) 的类型。 IsRead 存在于 EmailMessage (msdn.microsoft.com/en-us/library/…) 上。确保你选对了课程。
    【解决方案2】:

    我带来了另一个答案元素,以帮助那些与我陷入相同陷阱的人,以及那些使用 EWS 2.0 的人:

    我在循环中使用 Item 类型来获取邮箱中的邮件。并且 Item 对象没有 IsRead 属性(但类似于 EmailMessage 对象)。因此,您可以在循环中将 Item 类型替换为 EmailMessage 类型(因为允许从 Item 类型转换为 EmailMessage 类型):

    foreach (EmailMessage message in findResults.Items)
    {
       if(message.IsRead==false) //if the current message is unread
       {
          //treatment
       } 
       else
       {
    
       }
    }
    

    这就是我让它工作的方式。

    【讨论】:

      【解决方案3】:
              For Each appointment As EmailMessage In service.FindItems(WellKnownFolderName.Inbox, New SearchFilter.SearchFilterCollection(LogicalOperator.And, New SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, False)), New ItemView(999))
                  ' set as read 
                  appointment.IsRead = True
                  appointment.Update(ConflictResolutionMode.AlwaysOverwrite)
              Next
      

      【讨论】:

        猜你喜欢
        • 2011-10-14
        • 1970-01-01
        • 2017-12-15
        • 2014-09-09
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-01
        相关资源
        最近更新 更多