From e5404b320dac6476d1acfa117fe1bc64627215be Mon Sep 17 00:00:00 2001 From: Rob Wood Date: Sun, 23 Oct 2022 08:05:28 +0100 Subject: [PATCH] fix(IMAP) #1126 (#1142) * Add test to reproduce #1126 * fix(IMAP): Fix #1126 disposed object exception when marking read --- Rnwood.Smtp4dev.Tests/E2E/E2ETests_Imap.cs | 22 ++++++++++++++++++++-- Rnwood.Smtp4dev/Server/ImapServer.cs | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Rnwood.Smtp4dev.Tests/E2E/E2ETests_Imap.cs b/Rnwood.Smtp4dev.Tests/E2E/E2ETests_Imap.cs index a89d993db..c47c7bf0f 100644 --- a/Rnwood.Smtp4dev.Tests/E2E/E2ETests_Imap.cs +++ b/Rnwood.Smtp4dev.Tests/E2E/E2ETests_Imap.cs @@ -1,4 +1,5 @@ using LumiSoft.Net.Mime; +using MailKit; using MailKit.Net.Imap; using MailKit.Net.Smtp; using MailKit.Security; @@ -54,10 +55,27 @@ public void MessagesAvailable() { imapClient.Connect("localhost", context.ImapPortNumber); imapClient.Authenticate("user", "password"); - imapClient.Inbox.Open(MailKit.FolderAccess.ReadOnly); - var imapMessage = imapClient.Inbox.FirstOrDefault(); + imapClient.Inbox.Open(MailKit.FolderAccess.ReadWrite); + + var imapMessageSummary = imapClient.Inbox.Fetch(0, 0, MessageSummaryItems.UniqueId|MessageSummaryItems.Full); + Assert.Equal(messageSubject, imapMessageSummary[0].NormalizedSubject); + var imapMessage = imapClient.Inbox.GetMessage(imapMessageSummary[0].UniqueId); Assert.NotNull(imapMessage); Assert.Equal(messageSubject, imapMessage.Subject); + + imapClient.Inbox.AddFlags(imapMessageSummary[0].UniqueId, MessageFlags.Seen, true); + imapClient.Inbox.Close(); + } + + using (ImapClient imapClient = new ImapClient()) + { + imapClient.Connect("localhost", context.ImapPortNumber); + imapClient.Authenticate("user", "password"); + imapClient.Inbox.Open(MailKit.FolderAccess.ReadWrite); + + var imapMessageSummary = imapClient.Inbox.Fetch(0, 0, MessageSummaryItems.All); + Assert.True(imapMessageSummary[0].Flags.Value.HasFlag(MessageFlags.Seen)); + imapClient.Inbox.Close(); } }); diff --git a/Rnwood.Smtp4dev/Server/ImapServer.cs b/Rnwood.Smtp4dev/Server/ImapServer.cs index 31a0a43a9..ba7a95614 100644 --- a/Rnwood.Smtp4dev/Server/ImapServer.cs +++ b/Rnwood.Smtp4dev/Server/ImapServer.cs @@ -168,12 +168,12 @@ private void Session_Store(object sender, IMAP_e_Store e) { if (e.Flags.Contains("Seen", StringComparer.OrdinalIgnoreCase)) { - messagesRepository.MarkMessageRead(new Guid(e.MessageInfo.ID)); + messagesRepository.MarkMessageRead(new Guid(e.MessageInfo.ID)).Wait(); } if (e.Flags.Contains("Deleted", StringComparer.OrdinalIgnoreCase)) { - messagesRepository.DeleteMessage(new Guid(e.MessageInfo.ID)); + messagesRepository.DeleteMessage(new Guid(e.MessageInfo.ID)).Wait(); } } }