Search code examples
c#imapmailkit

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

  • First, I need to make sure that I correctly understand what you mean when you say that you are trying to get your "replies on an email".

    What I assume you mean right now is that someone sent you a message (let's call him Joe) and you replied back to him and you want to write a program that will connect to the IMAP server and download your reply back to Joe.

    Let's pretend that the Subject of the message is "drinks?"

    In other words:

    From: Joe <[email protected]>
    To: Malek <[email protected]>
    Subject: drinks?
    Message-Id: <[email protected]>
    
    Hey Malek, wanna grab drinks after work tonight?
    

    ...and you replied back to him:

    From: Malek <[email protected]>
    To: Joe <[email protected]>
    Subject: Re: drinks?
    Message-Id: <[email protected]>
    References: <[email protected]>
    In-Reply-To: <[email protected]>
    
    Can't tonight, but how about after work Friday?
    

    You are searching your own Inbox folder for the message containing "Re: drinks?" and ending up with no matches? And you also tried searching for the message in the Inbox using the Message-Id value and got no matches?

    Most likely, that won't work because whatever client you used to send the reply likely did not end up in the Inbox, it likely ended up in the Sent folder (or in a local Sent folder located on your hard drive).

    You mentioned that you also tried searching the Sent folder and still got no matches which suggests to me that the message isn't there. It's probably on your hard drive if you used a desktop mail client to send it (often desktop clients can be configured to save sent message on the server, but sometimes they default to the local hard drive instead).

    If you used MailKit's SmtpClient to send the message (or System.Net.Mail's SmtpClient), then the message will not have been saved anywhere - hence why it is also not in your Sent folder.

    When an SMTP server receives a message for sending, it does NOT copy it to your Sent folder. That has to be done using IMAP.