Posted By

kyrathaba on 10/02/10


Tagged

gmail ImapX GoogleMail


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

wraith808
kyrathaba
Tyster
mheinrich


Check Gmail using ImapX.dll


 / Published in: C#
 

URL: http://hellowebapps.com/wp-content/uploads/2010/07/ImapX_1.2.3.126.zip

Hacked together 10/01/2010\r\n\r\nPosted about it here:\\http://www.donationcoder.com/Forums/bb/index.php?topic=24148.0\r\n\r\nDownload sample solution from here:\\r\\nhttp://www.glensforkumc.com/non_church/csSampleSolutions/MyIMAP.zip Note: the sample solution contains the ImapX.dll file

  1. /* Required for this example:
  2.   *
  3.   * a C# WinForms application
  4.   * add reference to ImapX.dll available here:
  5.   * http://hellowebapps.com/wp-content/uploads/2010/07/ImapX_1.2.3.126.zip
  6.   * http://dl.dropbox.com/u/12124382/ImapX.zip
  7.   * a button <button1>
  8.   * a textbox <textBox1>
  9.   *
  10.   * Notes: I used a very specific Subject line as my search criteria, but you need not do this. You could
  11.   * just as easily have your code search through and download each and every email and/or every email's attachments.
  12.   */
  13.  
  14.  
  15. ImapX.ImapClient client = new ImapX.ImapClient("imap.gmail.com", 993, true);
  16. bool result = client.Connection();
  17. if (result) {
  18. result = client.LogIn("[email protected]", "mypassword");
  19. if (result) {
  20. textBox1.AppendText(Environment.NewLine + Environment.NewLine + "Log-on successful" +
  21. Environment.NewLine + Environment.NewLine);
  22. ImapX.FolderCollection folders = client.Folders;
  23. foreach (ImapX.Message m in client.Folders["INBOX"].Messages)
  24. {
  25. m.Process();
  26.  
  27. if (m.Subject == "test email with 3 attachments (txt, png, wav)") {
  28. textBox1.AppendText(Environment.NewLine + Environment.NewLine + "Found email in question..." +
  29. Environment.NewLine + Environment.NewLine);
  30. string path = Application.StartupPath + "\\email\\";
  31. string filename = "TxtPngWav";
  32. //comment-out following line if you don't want to d/l the actual email
  33. m.SaveAsEmlToFile(path, filename);
  34. //use above line, and comment out the following if(){}, if you prefer to download the whole
  35. //email in .eml format
  36. if (m.Attachments.Count > 0) {
  37. for (int i = 0; i < m.Attachments.Count; i++) {
  38. m.Attachments[i].SaveFile(path);
  39. textBox1.AppendText("Saved attachment #" + (i + 1).ToString());
  40. textBox1.AppendText(Environment.NewLine + Environment.NewLine);
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }
  47. else {
  48. textBox1.AppendText("connection failed");
  49. }

Report this snippet  

You need to login to post a comment.