0 votes
by (120 points)
edited

hi there,

do you know how to retreive the gmail conversation (all email in a conversation) that contains a given email.

thanks.

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (70.2k points)
edited

The Gmail exposes the "[Gmail]/All Mail" folder. You can search its content to find all incoming and outgoing emails at once. Please note that this folder aggregates emails from all folders except the "Bin" and "Spam" folder. So folders such as "Draft" and all user folders are to be searched.

The code can look like this:

string subject = "subject to find";
string email = "email to find";

Imap imap = new Imap();
imap.Connect("imap.gmail.com", Imap.DefaultImplicitSslPort, null, ImapSecurity.Implicit);
imap.Login("username", "password");

imap.SelectFolder("[Gmail]/All Mail");
ImapMessageCollection mails = imap.Search(
    ImapSearchParameter.Subject(subject),
    ImapSearchParameter.Or(
        ImapSearchParameter.From(email),
        ImapSearchParameter.To(email),
        ImapSearchParameter.CC(email),
        ImapSearchParameter.Bcc(email)));

If you rather want to receive conversation only from "Inbox" and "Sent mails" you can use this code:

List<ImapMessageInfo> mails = new List<ImapMessageInfo>();

imap.SelectFolder("Inbox");
mails.AddRange(imap.Search(
    ImapSearchParameter.Subject(subject), 
    ImapSearchParameter.From(email)));

imap.SelectFolder("[Gmail]/Sent Mail");
mails.AddRange(imap.Search(
    ImapSearchParameter.Subject(subject), 
    ImapSearchParameter.Or(
        ImapSearchParameter.To(email),
        ImapSearchParameter.CC(email),
        ImapSearchParameter.Bcc(email))));

Also please note the Subject search mechanism in Gmail:

  1. Parameter is searched anywhere in subject. So searching the "Photos for Mike" will also find "My nice photos for Mike and Bob".
  2. Parameter matches only whole worlds. So searching the "fox" will find the "Lazy fox hound" and also the "Lazy fox-hound", but not the "Lazy foxhound".

It means that to receive only specific conversation you should enumerate the result and check the subject of each email. Please keep in mind that the subject can also contains "RE:", "FW:" and similar prefixes.

...