hi there,

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

thanks.

asked 28 Sep '11, 05:00

hollymatrix's gravatar image

hollymatrix
15
accept rate: 0%

edited 11 Oct '11, 17:05

Martin%20Vobr's gravatar image

Martin Vobr ♦♦
335310


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.

link

answered 29 Sep '11, 12:10

Lukas%20Matyska's gravatar image

Lukas Matyska ♦♦
90117
accept rate: 28%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×76
×53
×20

Asked: 28 Sep '11, 05:00

Seen: 347 times

Last updated: 18 Oct '11, 04:52