'How to Get My Replies on an email using MailKit library?
I am using MailKit Library to fetch messages from an IMAP Server.
I tried to get an email based on its Subject, but it only returns Client Messages' IDs without my replies
var uidsX = client.Inbox.Search (SearchQuery.SubjectContains ("Message\'s Subject"));
I also tried to hard-code my message id header's value after getting it from Gmail, but i received an Empty List!
const string myReplyId = @"<CAAWrOQmJib_RHUn+3vB9GqATim165zq_Upn_8OTatZZMYGtf5w@mail.gmail.com>";
var myReply = client.Inbox.Search(SearchQuery.HeaderContains("Message-ID", myMessageId));
I even tried to See if i can find my replies in Sent Folder Instead of Inbox, but i'm getting an empty List.
So could any One help me in this?
Solution 1:[1]
Here's how I'm doing it using MailKit. Replace msg with the inbox folder message you want to get a reply to.
MessageSummaryItems pullSummaries = MessageSummaryItems.Envelope | MessageSummaryItems.Flags | MessageSummaryItems.InternalDate | MessageSummaryItems.Size | MessageSummaryItems.UniqueId | MessageSummaryItems.GMailLabels | MessageSummaryItems.BodyStructure;
//Open the sent mailbox
await ((ImapFolder)Client.GetFolder(SpecialFolder.Sent)).OpenAsync(FolderAccess.ReadOnly, CancelToken.Token);
//Query the message by the InReplyTo header
var replMsg = await SentFolder.SearchAsync(SearchQuery.HeaderContains("In-Reply-To", msg.Envelope.MessageId));
if (replMsg.Count() > 0)
{
var replFetch = await SentFolder.FetchAsync(replMsg, pullSummaries);
foreach (var sentItem in replFetch)
{
MimeMessage RepliedEmail = await SentFolder.GetMessageAsync(sentItem.Index);
Console.WriteLine($"{RepliedEmail.Subject} from {string.Join(",", RepliedEmail.From.OfType<MailboxAddress>().Select(f => $"{f.Name} <{f.Address}>").ToArray())}");
Console.WriteLine($"{RepliedEmail.GetTextBody(MimeKit.Text.TextFormat.Plain)}");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Szhlopp |
