8

Is there any way to programmatically search GMail, preferably using C#?

For example, I'd like to get all email messages matching the search label:MyLabel from:no_reply@foo.bar, so that I can parse the email bodies as required.

The only thing remotely feasible I've found is the GMail API by Johnvey Hwang, though it doesn't look like it supports searching email and also hasn't been updated in many years either.

In any case, I haven't had much luck in getting it to even connect to my account so far, and was wondering if this even works anymore?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mun
  • 14,098
  • 11
  • 59
  • 83
  • any final solution with full source code sample working about it ? – Kiquenet Oct 24 '13 at 08:22
  • @Kiquenet The project is on Github. However, I ended up not implementing any search functionality, but if you're interested in seeing the code anyway, it's at https://github.com/munr/iDeviceAppReporter. – Mun Oct 25 '13 at 00:39
  • @Kiquenet Actually, looking at this again, it seems like I did implement search after all. https://github.com/munr/iDeviceAppReporter/blob/master/ReportingApp/MainForm.cs#L143 – Mun Oct 25 '13 at 00:42

4 Answers4

8

Create an IMAP client, or use a library, to access Gmail and search for e-mail messages. The IMAP protocol explicitly supports searching on the server-side. Make sure IMAP access is enabled for the Gmail account.

Note that in Gmail, labels are treated like IMAP folders.

In silico
  • 51,091
  • 10
  • 150
  • 143
2

Eventually, I settled with using IMAPX, which works pretty well. Tried a few other IMAP libraries which weren't as powerful and seemed a bit flaky.

If anyone's interested, the outcome of this is iDevice App Reporter, which is a WinForms app that searches GMail (or any other IMAP mailbox) for iTunes app receipts and parses these to return a list of downloaded/purchased apps from the Apple AppStore.

Mun
  • 14,098
  • 11
  • 59
  • 83
2

it's 2015 now! You can use Google's Gmail API.

It has a search feature, you can do it easily just like this:

https://www.googleapis.com/gmail/v1/users/me/messages?q="in:sent after:2014/01/01 before:2014/01/30"
Or Duan
  • 13,142
  • 6
  • 60
  • 65
  • The Gmail API has a bug, it's going to be fixed soon, hopefully: https://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=4608 – Oded Breiner Jul 08 '16 at 06:17
1

To use a full gmail search (like multi labels, boolean operators) via IMAP, you need to use X-GM-RAW attribute. More on Google Apps docs.

Tobias Cudnik
  • 9,240
  • 4
  • 24
  • 18