简体   繁体   English

已读的 Exchange Web 服务更新电子邮件不起作用 C#

[英]Exchange Web Services update email as read does not work C#

I'm using Exchange Web Services to read email from exchange server and update it.我正在使用 Exchange Web 服务从 Exchange 服务器读取电子邮件并更新它。 I have an account with a parent email with subfolders in inbox, I can read the email I want successfully, but when I try to mark it as read it does not work, the email remains unread.我有一个带有收件箱子文件夹的父电子邮件帐户,我可以成功阅读我想要的电子邮件,但是当我尝试将其标记为已读时它不起作用,该电子邮件仍未读。

There is my code:有我的代码:

  static void Main(string[] args)
    {
        var exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
        exchange.Credentials = new WebCredentials("User", "Pass", "Domain");
        exchange.AutodiscoverUrl("Email", RedirectionCallback);
        FolderId folderToAccess = new FolderId(WellKnownFolderName.Inbox, "ParentEmail");
        FolderView view2 = new FolderView(100);
        view2.PropertySet = new PropertySet(BasePropertySet.IdOnly);
        view2.PropertySet.Add(FolderSchema.DisplayName);
        view2.Traversal = FolderTraversal.Deep;
        FindFoldersResults findFolderResults = exchange.FindFolders(folderToAccess, view2).Result;

        foreach (Folder f in findFolderResults)
        {
            if (f.DisplayName == "NameOfTheFolderUnderInbox")
            {
                var SearchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);
                ItemView view = new ItemView(100);
                FindItemsResults<Item> findResults = exchange.FindItems(f.Id, SearchFilter, view).Result;

                foreach (Item item in findResults)
                {
                    PropertySet props = new PropertySet(EmailMessageSchema.MimeContent, ItemSchema.Subject);
                    EmailMessage message = EmailMessage.Bind(exchange, item.Id, props).Result;
                    if (message.Subject.Contains("Text"))
                    {
                        string emlFileName = @"C:\export\email.eml";
                        using (FileStream fs = new FileStream(emlFileName, FileMode.Create, FileAccess.Write))
                        {
                            fs.Write(message.MimeContent.Content, 0, message.MimeContent.Content.Length);
                        }
                    }
                    message.IsRead = true;
                    message.Update(ConflictResolutionMode.AlwaysOverwrite, true);
                }
            }
        }
    }
    static bool RedirectionCallback(string url)
    {
        // Return true if the URL is an HTTPS URL.
        return url.ToLower().StartsWith("https://");
    }

Ok, I solved the problem and maybe could be usefull for someone else.好的,我解决了这个问题,也许对其他人有用。

After the update, making a new research the email went read also on outlook.更新后,进行了一项新的研究,电子邮件也在 Outlook 上阅读。

FindItemsResults<Item> UpdateResults = exchange.FindItems(f.Id, SearchFilter, view).Result

This after update make it work!这更新后使它工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM