Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29745

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

$
0
0

I'm using Exchange Web Services to read email from exchange server and update it. 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://");
    }

Viewing all articles
Browse latest Browse all 29745

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>