Skip to content

Instantly share code, notes, and snippets.

@kyrathasoft
Created June 17, 2014 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyrathasoft/81d398958e40e10c462f to your computer and use it in GitHub Desktop.
Save kyrathasoft/81d398958e40e10c462f to your computer and use it in GitHub Desktop.
Send GMail via ImapX.dll
/* Required for this example:
*
* a C# WinForms application
* add reference to ImapX.dll available here:
* http://hellowebapps.com/wp-content/uploads/2010/07/ImapX_1.2.3.126.zip
* http://dl.dropbox.com/u/12124382/ImapX.zip
* a button <button1>
* a textbox <textBox1>
*
* Notes: I used a very specific Subject line as my search criteria, but you need not do this. You could
* just as easily have your code search through and download each and every email and/or every email's attachments.
*/
ImapX.ImapClient client = new ImapX.ImapClient("imap.gmail.com", 993, true);
bool result = client.Connection();
if (result) {
result = client.LogIn("myself@gmail.com", "mypassword");
if (result) {
textBox1.AppendText(Environment.NewLine + Environment.NewLine + "Log-on successful" +
Environment.NewLine + Environment.NewLine);
ImapX.FolderCollection folders = client.Folders;
foreach (ImapX.Message m in client.Folders["INBOX"].Messages)
{
m.Process();
if (m.Subject == "test email with 3 attachments (txt, png, wav)") {
textBox1.AppendText(Environment.NewLine + Environment.NewLine + "Found email in question..." +
Environment.NewLine + Environment.NewLine);
string path = Application.StartupPath + "\\email\\";
string filename = "TxtPngWav";
//comment-out following line if you don't want to d/l the actual email
m.SaveAsEmlToFile(path, filename);
//use above line, and comment out the following if(){}, if you prefer to download the whole
//email in .eml format
if (m.Attachments.Count > 0) {
for (int i = 0; i < m.Attachments.Count; i++) {
m.Attachments[i].SaveFile(path);
textBox1.AppendText("Saved attachment #" + (i + 1).ToString());
textBox1.AppendText(Environment.NewLine + Environment.NewLine);
}
}
}
}
}
}
else {
textBox1.AppendText("connection failed");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment