const string kPassword = "******"; // the correct password on my test server - you'll need to change this static void Main(string[] args) { WebVersion(); return; try { using (cIMAPClient lClient = new cIMAPClient()) { // get the settings var lConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var lSettings = lConfig.AppSettings.Settings; var lUIDValiditySetting = lSettings[kUIDValidity]; if (lUIDValiditySetting == null) { lUIDValiditySetting = new KeyValueConfigurationElement(kUIDValidity, string.Empty); lSettings.Add(lUIDValiditySetting); } var lUIDSetting = lSettings[kUID]; if (lUIDSetting == null) { lUIDSetting = new KeyValueConfigurationElement(kUID, string.Empty); lSettings.Add(lUIDSetting); } // connect // lClient.SetServer(kHost); // if you are using this against a production server you'll likely need to specify SSL and maybe the port lClient.SetPlainCredentials(kUserId, kPassword, eTLSRequirement.indifferent); // if you are using this against a production server you will most likely want to require TLS (which is the default that I have overridden here) lClient.Connect(); // select the inbox lClient.Inbox.Select(true); // get the UID we should inspect from cUID lFromUID; // check the UIDValidity to make sure it hasn't changed if (uint.TryParse(lUIDValiditySetting.Value, out var lUIDValidity) && lUIDValidity == lClient.Inbox.UIDValidity.Value && uint.TryParse(lUIDSetting.Value, out var lUID)) { lFromUID = new cUID(lUIDValidity, lUID); } else { lFromUID = new cUID(lClient.Inbox.UIDValidity.Value, 1); } // note the UIDNext for the next run uint lUIDNext = lClient.Inbox.UIDNext.Value; // no point doing anything if there are no more messages than last time if (lUIDNext > lFromUID.UID) { // this example is meant to demonstrate filtering, so here it is var lFilter = cFilter.UID >= lFromUID& cFilter.From.Contains("*****@*****.**") & !cFilter.Deleted; // loop through the messages foreach (var lMessage in lClient.Inbox.Messages(lFilter, cSort.None, fMessageProperties.attachments | fMessageProperties.uid)) { // only process the message if it looks as expected if (lMessage.Attachments.Count == 1 && lMessage.PlainText() == "FILE FOR PROCESSING") { // save the attachment lMessage.Attachments[0].SaveAs($".\\SavedAttachment.{lMessage.UID.UIDValidity}.{lMessage.UID.UID}"); // mark the message as deleted lMessage.Deleted = true; } } // store the start point for the next run lUIDValiditySetting.Value = lFromUID.UIDValidity.ToString(); lUIDSetting.Value = lUIDNext.ToString(); lConfig.Save(ConfigurationSaveMode.Modified); // expunge the deleted messages lClient.Inbox.Expunge(true); } // done lClient.Disconnect(); } } catch (Exception e) { Console.WriteLine($"something bad happened\n{e}"); } Console.WriteLine("press enter to continue"); Console.Read(); }
// to test this include ", eTLSRequirement.indifferent" on the SetPlainCredentials static void NewWebVersion() { cIMAPClient lClient = new cIMAPClient(); // connect lClient.SetServer(kHost); lClient.SetPlainCredentials(kUserId, kPassword); lClient.Connect(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); // list mailboxes in the first personal namespace Console.WriteLine("A list of mailboxes;"); var lNamespace = lClient.Namespaces.Personal[0]; foreach (var lMailbox in lNamespace.Mailboxes()) { Console.WriteLine(lMailbox.Name); } Console.WriteLine(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); // list status of inbox Console.WriteLine( "{0} unseen messages out of {1} in the inbox", lClient.Inbox.UnseenCount, lClient.Inbox.MessageCount); Console.WriteLine(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); // select the inbox so we can look at the messages in it lClient.Inbox.Select(); // list unseen messages in the inbox foreach (var lMessage in lClient.Inbox.Messages(!cFilter.Seen)) { Console.WriteLine("Sent: " + lMessage.Sent); Console.WriteLine("From: " + lMessage.From.DisplaySortString); Console.WriteLine("Subject: " + lMessage.Subject); Console.WriteLine(); var lAttachments = lMessage.Attachments; if (lAttachments.Count > 0) { Console.WriteLine(lAttachments.Count + " attachments;"); foreach (var lAttachment in lAttachments) { Console.WriteLine( lAttachment.FileName + "\t" + lAttachment.SaveSizeInBytes() + "b"); } Console.WriteLine(); } Console.WriteLine(lMessage.PlainText()); Console.WriteLine(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); } // done lClient.Disconnect(); lClient.Dispose(); }
static void WebVersion() { // the first part of the example is just getting settings // it isn't really what I want to show you var lConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var lSettings = lConfig.AppSettings.Settings; var lUIDValiditySetting = lSettings[kUIDValidity]; if (lUIDValiditySetting == null) { lUIDValiditySetting = new KeyValueConfigurationElement(kUIDValidity, string.Empty); lSettings.Add(lUIDValiditySetting); } var lUIDSetting = lSettings[kUID]; if (lUIDSetting == null) { lUIDSetting = new KeyValueConfigurationElement(kUID, string.Empty); lSettings.Add(lUIDSetting); } // now what I want to show you cIMAPClient lClient = new cIMAPClient(); // connect // lClient.SetServer(kHost); lClient.SetPlainCredentials(kUserId, kPassword, eTLSRequirement.indifferent); lClient.Connect(); // select the inbox for update lClient.Inbox.Select(true); cUID lFromUID; // check the UIDValidity to make sure it hasn't changed if (uint.TryParse(lUIDValiditySetting.Value, out var lUIDValidity) && lUIDValidity == lClient.Inbox.UIDValidity.Value && uint.TryParse(lUIDSetting.Value, out var lUID)) { lFromUID = new cUID(lUIDValidity, lUID); } else { lFromUID = new cUID(lClient.Inbox.UIDValidity.Value, 1); } // note the UIDNext for the next run uint lUIDNext = lClient.Inbox.UIDNext.Value; // only process if there are more messages than at the last run if (lUIDNext > lFromUID.UID) { // this example is meant to demonstrate building a filter, so here it is var lFilter = cFilter.UID >= lFromUID& cFilter.From.Contains("*****@*****.**") & !cFilter.Deleted; // loop through the messages foreach (var lMessage in lClient.Inbox.Messages(lFilter)) { // only process the message if it looks as expected if (lMessage.Attachments.Count == 1 && lMessage.PlainText() == "FILE FOR PROCESSING") { // save the attachement lMessage.Attachments[0].SaveAs ($".\\SavedAttachment.{lMessage.UID.UIDValidity}.{lMessage.UID.UID}"); // mark the message as deleted lMessage.Deleted = true; } } // store the start point for the next run lUIDValiditySetting.Value = lFromUID.UIDValidity.ToString(); lUIDSetting.Value = lUIDNext.ToString(); lConfig.Save(ConfigurationSaveMode.Modified); // expunge the deleted messages lClient.Inbox.Expunge(true); } // done lClient.Disconnect(); lClient.Dispose(); }
const string kPassword = "******"; // the correct password on my test server - you'll need to change this static void Main(string[] args) { try { using (cIMAPClient lClient = new cIMAPClient()) { // connect // lClient.SetServer(kHost); // if you are using this against a production server you'll likely need to specify SSL and maybe the port lClient.SetPlainCredentials(kUserId, kPassword, eTLSRequirement.indifferent); // if you are using this against a production server you will most likely want to require TLS (which is the default that I have overridden here) lClient.Connect(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); // list the mailboxes that the user has at the top level of their first personal namespace // Console.WriteLine("A list of mailboxes;"); foreach (var lMailbox in lClient.Namespaces.Personal[0].Mailboxes()) { Console.WriteLine(lMailbox.Name); } Console.WriteLine(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); // get a reference to the inbox var lInbox = lClient.Inbox; // show some information about the status of the inbox // Console.WriteLine($"There are {lInbox.UnseenCount} unseen messages out of {lInbox.MessageCount} in the inbox"); Console.WriteLine(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); // select the inbox so we can look at the messages in it lInbox.Select(); // list some details of the unseen messages // (for efficiency specify the message properties to retrieve) // foreach (var lMessage in lInbox.Messages(!cFilter.Seen, new cSort(cSortItem.Received), fMessageProperties.envelope | fMessageProperties.bodystructure)) { Console.WriteLine("Sent: " + lMessage.Sent); Console.WriteLine("From: " + lMessage.From.DisplaySortString); Console.WriteLine("Subject: " + lMessage.Subject); Console.WriteLine(); var lAttachments = lMessage.Attachments; if (lAttachments.Count > 0) { Console.WriteLine(lAttachments.Count + " attachments;"); foreach (var lAttachment in lAttachments) { Console.WriteLine(lAttachment.FileName + "\t" + lAttachment.SaveSizeInBytes() + "b"); } Console.WriteLine(); } Console.WriteLine(lMessage.PlainText()); Console.WriteLine(); Console.WriteLine(new string('-', 79)); Console.WriteLine(); } // done lClient.Disconnect(); } } catch (Exception e) { Console.WriteLine($"something bad happened\n{e}"); } NewWebVersion(); Console.WriteLine("press enter to continue"); Console.Read(); }