private async Task <bool> ZFetchToStream(cUID pUID, cSection pSection, eDecodingRequired pDecoding, string pTitle, Stream pStream) { frmProgress lProgress = null; try { lProgress = new frmProgress(pTitle); ZChildAdd(lProgress); await mClient.SelectedMailbox.UIDFetchAsync(pUID, pSection, pDecoding, pStream, new cBodyFetchConfiguration(lProgress.CancellationToken, lProgress.Increment)); return(true); } catch (OperationCanceledException) { } catch (Exception ex) { if (!IsDisposed) { MessageBox.Show(this, $"problem when fetching\n{ex}"); } } finally { if (lProgress != null) { lProgress.Complete(); } } return(false); }
public frmMessageData(cUID pUID, cSection pSection, eDecodingRequired pDecoding, string pData) { mUID = pUID; mSection = pSection; mDecoding = pDecoding; mData = pData; InitializeComponent(); }
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(); }
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(); }
private bool ZFetchParameters(out cUID rUID, out cSection rSection, out eDecodingRequired rDecoding) { rUID = null; rSection = null; rDecoding = eDecodingRequired.other; if (!ValidateChildren(ValidationConstraints.Enabled)) { MessageBox.Show(this, "there are issues with the data entered"); return(false); } if (string.IsNullOrWhiteSpace(txtUID.Text)) { MessageBox.Show(this, "you must enter a UID"); return(false); } try { rUID = new cUID(uint.Parse(txtUIDValidity.Text), uint.Parse(txtUID.Text)); string lPart; if (string.IsNullOrWhiteSpace(txtPart.Text)) { lPart = null; } else { lPart = txtPart.Text.Trim(); } eSectionTextPart lTextPart; if (rdoAll.Checked) { lTextPart = eSectionTextPart.all; } else if (rdoHeader.Checked) { lTextPart = eSectionTextPart.header; } else if (rdoFields.Checked) { lTextPart = eSectionTextPart.headerfields; } else if (rdoFieldsNot.Checked) { lTextPart = eSectionTextPart.headerfieldsnot; } else if (rdoText.Checked) { lTextPart = eSectionTextPart.text; } else { lTextPart = eSectionTextPart.mime; } if (lTextPart == eSectionTextPart.headerfields || lTextPart == eSectionTextPart.headerfieldsnot) { if (string.IsNullOrWhiteSpace(txtFieldNames.Text)) { MessageBox.Show(this, "must enter some field names"); return(false); } if (!ZTryParseHeaderFieldNames(txtFieldNames.Text, out var lNames)) { MessageBox.Show(this, "must enter valid field names"); return(false); } rSection = new cSection(lPart, lNames, rdoFieldsNot.Checked); } else { rSection = new cSection(lPart, lTextPart); } if (rdoNone.Checked) { rDecoding = eDecodingRequired.none; } else if (rdoQuotedPrintable.Checked) { rDecoding = eDecodingRequired.quotedprintable; } else if (rdoQuotedPrintable.Checked) { rDecoding = eDecodingRequired.base64; } return(true); } catch (Exception e) { MessageBox.Show(this, $"there are issues with the data entered:\n{e.ToString()}"); return(false); } }
public cFilter Filter() { List <cFilter> lTerms = new List <cFilter>(); // build if (chkAnswered.Checked) { lTerms.Add(cFilter.Answered); } if (chkDeleted.Checked) { lTerms.Add(cFilter.Deleted); } if (chkDraft.Checked) { lTerms.Add(cFilter.Draft); } if (chkFlagged.Checked) { lTerms.Add(cFilter.Flagged); } if (chkRecent.Checked) { lTerms.Add(cFilter.Recent); } if (chkSeen.Checked) { lTerms.Add(cFilter.Seen); } if (chkForwarded.Checked) { lTerms.Add(cFilter.Forwarded); } if (chkSubmitPending.Checked) { lTerms.Add(cFilter.SubmitPending); } if (chkSubmitted.Checked) { lTerms.Add(cFilter.Submitted); } // see comments in the library as to why this is commented out //if (chkMDNSent.Checked) lTerms.Add(cFilter.MDNSent); if (ZTryParseFlagNames(txtSet.Text, out var lSet) && lSet != null) { lTerms.Add(cFilter.FlagsContain(lSet)); } if (chkUnanswered.Checked) { lTerms.Add(!cFilter.Answered); } if (chkUndeleted.Checked) { lTerms.Add(!cFilter.Deleted); } if (chkUnDraft.Checked) { lTerms.Add(!cFilter.Draft); } if (chkUnflagged.Checked) { lTerms.Add(!cFilter.Flagged); } if (chkUnrecent.Checked) { lTerms.Add(!cFilter.Recent); } if (chkUnseen.Checked) { lTerms.Add(!cFilter.Seen); } if (chkUnforwarded.Checked) { lTerms.Add(!cFilter.Forwarded); } if (chkUnsubmitPending.Checked) { lTerms.Add(!cFilter.SubmitPending); } if (chkUnsubmitted.Checked) { lTerms.Add(!cFilter.Submitted); } // see comments in the library as to why this is commented out //if (chkUnMDNSent.Checked) lTerms.Add(!cFilter.MDNSent); if (ZTryParseFlagNames(txtNotSet.Text, out var lNotSet) && lNotSet != null) { lTerms.Add(!cFilter.FlagsContain(lNotSet)); } lTerms.AddRange(from r in mPartsBindingList where !string.IsNullOrWhiteSpace(r.Contains) select r.FilterPartContains); if (dtpRAfter.Format != DateTimePickerFormat.Custom) { lTerms.Add(cFilter.Received > dtpRAfter.Value); } if (dtpROn.Format != DateTimePickerFormat.Custom) { lTerms.Add(cFilter.Received == dtpROn.Value); } if (dtpRBefore.Format != DateTimePickerFormat.Custom) { lTerms.Add(cFilter.Received < dtpRBefore.Value); } if (dtpSAfter.Format != DateTimePickerFormat.Custom) { lTerms.Add(cFilter.Sent > dtpSAfter.Value); } if (dtpSOn.Format != DateTimePickerFormat.Custom) { lTerms.Add(cFilter.Sent == dtpSOn.Value); } if (dtpSBefore.Format != DateTimePickerFormat.Custom) { lTerms.Add(cFilter.Sent < dtpSBefore.Value); } lTerms.AddRange(from r in mHeadersBindingList where r.IsValid select r.FilterHeaderFieldContains); uint lUInt; if (uint.TryParse(txtSizeLarger.Text, out lUInt)) { lTerms.Add(cFilter.Size > lUInt); } if (uint.TryParse(txtSizeSmaller.Text, out lUInt)) { lTerms.Add(cFilter.Size < lUInt); } if (rdoImpLow.Checked) { lTerms.Add(cFilter.Importance == eImportance.low); } if (rdoImpNormal.Checked) { lTerms.Add(cFilter.Importance == eImportance.normal); } if (rdoImpHigh.Checked) { lTerms.Add(cFilter.Importance == eImportance.high); } if (!rdoUIDAll.Checked && uint.TryParse(txtUIDValidity.Text, out var lUIDValidity) && lUIDValidity > 0 && uint.TryParse(txtUID.Text, out var lUID) && lUID > 0) { cUID lUIDc = new cUID(lUIDValidity, lUID); if (rdoUIDLT.Checked) { lTerms.Add(cFilter.UID < lUIDc); } else if (rdoUIDLE.Checked) { lTerms.Add(cFilter.UID <= lUIDc); } else if (rdoUIDEQ.Checked) { lTerms.Add(cFilter.UID == lUIDc); } else if (rdoUIDGT.Checked) { lTerms.Add(cFilter.UID > lUIDc); } else if (rdoUIDGE.Checked) { lTerms.Add(cFilter.UID >= lUIDc); } else if (rdoUIDNEQ.Checked) { lTerms.Add(cFilter.UID != lUIDc); } } if (chkAll.Checked) { lTerms.Add(cFilter.All); } if (chkNone.Checked) { lTerms.Add(cFilter.None); } if (!rdoMSNAll.Checked && int.TryParse(txtOffset.Text, out var lOffset)) { cFilterMSNOffset lOffsetc; if (rdoFromFirst.Checked) { lOffsetc = cFilter.First.MSNOffset(lOffset); } else { lOffsetc = cFilter.Last.MSNOffset(lOffset); } if (rdoMSNLT.Checked) { lTerms.Add(cFilter.MSN < lOffsetc); } else if (rdoMSNLE.Checked) { lTerms.Add(cFilter.MSN <= lOffsetc); } else if (rdoMSNGT.Checked) { lTerms.Add(cFilter.MSN > lOffsetc); } else if (rdoMSNGE.Checked) { lTerms.Add(cFilter.MSN >= lOffsetc); } } // return if (lTerms.Count == 0) { return(null); } cFilter lResult = null; foreach (var lTerm in lTerms) { if (lResult == null) { lResult = lTerm; } else { lResult = lResult & lTerm; } } if (chkInvert.Checked) { lResult = !lResult; } return(lResult); }