private async void cmdConnect_Click(object sender, EventArgs e) { if (!ValidateChildren(ValidationConstraints.Enabled)) { return; } try { mClient.SetServer(txtHost.Text.Trim(), int.Parse(txtPort.Text), chkSSL.Checked); if (rdoCredNone.Checked) { mClient.SetNoCredentials(); } else { eTLSRequirement lTLSRequirement; if (rdoTLSIndifferent.Checked) { lTLSRequirement = eTLSRequirement.indifferent; } else if (rdoTLSRequired.Checked) { lTLSRequirement = eTLSRequirement.required; } else { lTLSRequirement = eTLSRequirement.disallowed; } if (rdoCredAnon.Checked) { mClient.SetAnonymousCredentials(txtTrace.Text.Trim(), lTLSRequirement, chkTryIfNotAdvertised.Checked); } else { mClient.SetPlainCredentials(txtUserId.Text.Trim(), txtPassword.Text.Trim(), lTLSRequirement, chkTryIfNotAdvertised.Checked); } } fMailboxCacheDataItems lMailboxCacheData = 0; if (chkCacheSubscribed.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.subscribed; } if (chkCacheChildren.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.children; } if (chkCacheSpecialUse.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.specialuse; } if (chkCacheMessageCount.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.messagecount; } if (chkCacheRecentCount.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.recentcount; } if (chkCacheUIDNext.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.uidnext; } if (chkCacheUIDValidity.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.uidvalidity; } if (chkCacheUnseenCount.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.unseencount; } if (chkCacheHighestModSeq.Checked) { lMailboxCacheData |= fMailboxCacheDataItems.highestmodseq; } mClient.MailboxCacheDataItems = lMailboxCacheData; fCapabilities lKnownCapabilities = 0; if (chkIgnoreStartTLS.Checked) { lKnownCapabilities |= fCapabilities.starttls; } if (chkIgnoreEnable.Checked) { lKnownCapabilities |= fCapabilities.enable; } if (chkIgnoreUTF8.Checked) { lKnownCapabilities |= fCapabilities.utf8accept | fCapabilities.utf8only; } if (chkIgnoreId.Checked) { lKnownCapabilities |= fCapabilities.id; } if (chkIgnoreNamespace.Checked) { lKnownCapabilities |= fCapabilities.namespaces; } if (chkIgnoreMailboxReferrals.Checked) { lKnownCapabilities |= fCapabilities.mailboxreferrals; } if (chkIgnoreListExtended.Checked) { lKnownCapabilities |= fCapabilities.listextended; } if (chkIgnoreListStatus.Checked) { lKnownCapabilities |= fCapabilities.liststatus; } if (chkIgnoreSpecialUse.Checked) { lKnownCapabilities |= fCapabilities.specialuse; } if (chkIgnoreCondStore.Checked) { lKnownCapabilities |= fCapabilities.condstore; } if (chkIgnoreQResync.Checked) { lKnownCapabilities |= fCapabilities.qresync; } if (chkIgnoreLiteralPlus.Checked) { lKnownCapabilities |= fCapabilities.literalplus | fCapabilities.literalminus; } if (chkIgnoreBinary.Checked) { lKnownCapabilities |= fCapabilities.binary; } if (chkIgnoreIdle.Checked) { lKnownCapabilities |= fCapabilities.idle; } if (chkIgnoreSASLIR.Checked) { lKnownCapabilities |= fCapabilities.sasl_ir; } if (chkIgnoreESearch.Checked) { lKnownCapabilities |= fCapabilities.esearch; } if (chkIgnoreSort.Checked) { lKnownCapabilities |= fCapabilities.sort; } if (chkIgnoreSortDisplay.Checked) { lKnownCapabilities |= fCapabilities.sortdisplay; } //if (chkIgnoreThreadOrderedSubject.Checked) lKnownCapabilities |= fCapabilities.threadorderedsubject; //if (chkIgnoreThreadReferences.Checked) lKnownCapabilities |= fCapabilities.threadreferences; if (chkIgnoreESort.Checked) { lKnownCapabilities |= fCapabilities.esort; } mClient.IgnoreCapabilities = lKnownCapabilities; mClient.MailboxReferrals = chkMailboxReferrals.Checked; mClient.NetworkWriteConfiguration = new cBatchSizerConfiguration(int.Parse(txtNWMin.Text), int.Parse(txtNWMax.Text), int.Parse(txtNWMaxTime.Text), int.Parse(txtNWInitial.Text)); await mClient.ConnectAsync(); } catch (Exception ex) { if (!IsDisposed) { MessageBox.Show(this, $"Connect error\n{ex}"); } } }
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(); }