private void btnStart_Click(object sender, System.EventArgs e) { _enableSettingControls(false, false); if (!_tryOauth()) { _enableSettingControls(true, false); return; } if (ComboBoxAuthType.SelectedIndex == (int)ServerAuthType.AuthXOAUTH2) { textUser.Text = _oauthWrapper.OauthProvider.UserEmail; textPassword.Text = _oauthWrapper.OauthProvider.AccessToken; } if (!_validateInput()) { _enableSettingControls(true, false); return; } _enableSettingControls(false, true); // UIDL is the identifier of every email on POP3/IMAP4/Exchange server, to avoid retrieve // the same email from server more than once, we record the email UIDL retrieved every time // if you delete the email from server every time and not to leave a copy of email on // the server, then please remove all the function about uidl. // UIDLManager wraps the function to write/read uidl record from a text file. UIDLManager oUIDLManager = new UIDLManager(); bool _isUidlLoaded = false; try { // For evaluation usage, please use "TryIt" as the license code, otherwise the // "Invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then // "Trial version expired" exception will be thrown. MailClient client = new MailClient("TryIt"); // Catching the following events is not necessary, // just make the application more user friendly. // If you use the object in asp.net/windows service or non-gui application, // You need not to catch the following events. // To learn more detail, please refer to the code in EAGetMail EventHandler region client.OnAuthorized += new MailClient.OnAuthorizedEventHandler(OnAuthorized); client.OnConnected += new MailClient.OnConnectedEventHandler(OnConnected); client.OnIdle += new MailClient.OnIdleEventHandler(OnIdle); client.OnSecuring += new MailClient.OnSecuringEventHandler(OnSecuring); client.OnReceivingDataStream += new MailClient.OnReceivingDataStreamEventHandler(OnReceivingDataStream); _isCancelOperation = false; textStatus.Text = "Connecting ..."; // generate a log file for debug // client.LogFileName = string.Format("{0}\\log.txt", _currentPath); MailServer server = _buildServer(); client.Connect(server); string localInbox = _createLocalInbox(); // load existed uidl records to UIDLManager string uidlfile = string.Format("{0}\\{1}", localInbox, _uidlFile); oUIDLManager.Load(uidlfile); _isUidlLoaded = true; _setMailRange(ref client); textStatus.Text = "Retrieveing email list ..."; MailInfo[] infos = client.GetMailInfos(); textStatus.Text = string.Format("Total {0} email(s)", infos.Length); if (ComboBoxDateRange.SelectedIndex == 0) { // Remove the local uidl that is not existed on the server, // we only synchronize it with uidl of all emails in current mail folder oUIDLManager.SyncUIDL(server, infos); oUIDLManager.Update(); } for (int i = 0; i < infos.Length; i++) { pgBar.Maximum = 100; pgBar.Minimum = 0; pgBar.Value = 0; textStatus.Text = string.Format("Checking {0}/{1}...", i + 1, infos.Length); MailInfo info = infos[i]; if (oUIDLManager.FindUIDL(server, info.UIDL) != null) { //this email has been downloaded before. continue; } textStatus.Text = string.Format("Retrieving {0}/{1}...", i + 1, infos.Length); Mail mail = client.GetMail(info); string fileName = _generateFileName(i + 1); string fullFileName = string.Format("{0}\\{1}", localInbox, fileName); mail.SaveAs(fullFileName, true); _addNewMailToListView(ref mail, fullFileName); mail.Clear(); LabelTotal.Text = string.Format("Total {0} email(s)", ListViewMail.Items.Count); // If retrieve only new emails, after we retrieved it, mark it as read, // With this feature, you don't have to UIDLManager to prevent duplicated emails. if (chkNewOnly.Checked && ComboBoxProtocol.SelectedIndex != (int)ServerProtocol.Pop3) { client.MarkAsRead(info, true); } if (chkLeaveCopy.Checked) { // Add the email uidl to uidl file to avoid we retrieve it next time. oUIDLManager.AddUIDL(server, info.UIDL, fileName); } else { textStatus.Text = string.Format("Deleting {0}...", i + 1); client.Delete(info); // Remove UIDL from local uidl file. oUIDLManager.RemoveUIDL(server, info.UIDL); } } // Delete method just mark the email as deleted, // Quit method expunge the emails from server exactly. client.Quit(); // Update the uidl list to local uidl file and then we can load it next time. oUIDLManager.Update(); textStatus.Text = "Completed"; } catch (Exception ep) { if (_isUidlLoaded) { // Update the uidl list to local uidl file and then we can load it next time. oUIDLManager.Update(); } MessageBox.Show(ep.Message); textStatus.Text = ep.Message; } _enableSettingControls(true, false); }
private void btnStart_Click(object sender, System.EventArgs e) { string server, user, password; server = textServer.Text.Trim(); user = textUser.Text.Trim(); password = textPassword.Text.Trim(); if (server.Length == 0 || user.Length == 0 || password.Length == 0) { MessageBox.Show("Please input server, user and password."); return; } btnStart.Enabled = false; btnCancel.Enabled = true; ServerAuthType authType = ServerAuthType.AuthLogin; if (lstAuthType.SelectedIndex == 1) { authType = ServerAuthType.AuthCRAM5; } else if (lstAuthType.SelectedIndex == 2) { authType = ServerAuthType.AuthNTLM; } ServerProtocol protocol = (ServerProtocol)lstProtocol.SelectedIndex; MailServer oServer = new MailServer(server, user, password, chkSSL.Checked, authType, protocol); //For evaluation usage, please use "TryIt" as the license code, otherwise the //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then //"trial version expired" exception will be thrown. MailClient oClient = new MailClient("TryIt"); //Catching the following events is not necessary, //just make the application more user friendly. //If you use the object in asp.net/windows service or non-gui application, //You need not to catch the following events. //To learn more detail, please refer to the code in EAGetMail EventHandler region oClient.OnAuthorized += new MailClient.OnAuthorizedEventHandler(OnAuthorized); oClient.OnConnected += new MailClient.OnConnectedEventHandler(OnConnected); oClient.OnIdle += new MailClient.OnIdleEventHandler(OnIdle); oClient.OnSecuring += new MailClient.OnSecuringEventHandler(OnSecuring); oClient.OnReceivingDataStream += new MailClient.OnReceivingDataStreamEventHandler(OnReceivingDataStream); bool bLeaveCopy = chkLeaveCopy.Checked; // UIDL is the identifier of every email on POP3/IMAP4/Exchange server, to avoid retrieve // the same email from server more than once, we record the email UIDL retrieved every time // if you delete the email from server every time and not to leave a copy of email on // the server, then please remove all the function about uidl. // UIDLManager wraps the function to write/read uidl record from a text file. UIDLManager oUIDLManager = new UIDLManager(); try { // load existed uidl records to UIDLManager string uidlfile = String.Format("{0}\\{1}", m_curpath, m_uidlfile); oUIDLManager.Load(uidlfile); string mailFolder = String.Format("{0}\\inbox", m_curpath); if (!Directory.Exists(mailFolder)) { Directory.CreateDirectory(mailFolder); } m_bcancel = false; lblStatus.Text = "Connecting ..."; oClient.Connect(oServer); MailInfo[] infos = oClient.GetMailInfos(); lblStatus.Text = String.Format("Total {0} email(s)", infos.Length); // remove the local uidl which is not existed on the server. oUIDLManager.SyncUIDL(oServer, infos); oUIDLManager.Update(); int count = infos.Length; ArrayList arReport = new ArrayList(); for (int i = 0; i < count; i++) { MailInfo info = infos[i]; if (oUIDLManager.FindUIDL(oServer, info.UIDL) != null) { // This email has been downloaded or checked before. continue; } lblStatus.Text = String.Format("Retrieving mail header {0}/{1}...", info.Index, count); System.DateTime d = System.DateTime.Now; System.Globalization.CultureInfo cur = new System.Globalization.CultureInfo("en-US"); string sdate = d.ToString("yyyyMMddHHmmss", cur); string fileName = String.Format("{0}\\{1}{2}{3}.eml", mailFolder, sdate, d.Millisecond.ToString("d3"), i); Mail oMail = new Mail("TryIt"); oMail.Load(oClient.GetMailHeader(info)); if (!oMail.IsReport) { // Not a report, continue // Add the email uidl to uidl file to avoid we check it next time. oUIDLManager.AddUIDL(oServer, info.UIDL, fileName); continue; } // This is a report, get the entire email. oMail = oClient.GetMail(info); oMail.SaveAs(fileName, true); ListViewItem item = new ListViewItem(oMail.From.ToString()); item.SubItems.Add(oMail.Subject); item.SubItems.Add(oMail.ReceivedDate.ToString("yyyy-MM-dd HH:mm:ss")); item.Font = new System.Drawing.Font(item.Font, FontStyle.Bold); item.Tag = fileName; lstMail.Items.Insert(0, item); oMail.Clear(); lblTotal.Text = String.Format("Total {0} email(s)", lstMail.Items.Count); arReport.Add(info); // Add the report mail info to arraylist, // then we can delete it later. if (bLeaveCopy) { // Add the email uidl to uidl file to avoid we retrieve it next time. oUIDLManager.AddUIDL(oServer, info.UIDL, fileName); } } if (!bLeaveCopy) { lblStatus.Text = "Deleting ..."; count = arReport.Count; for (int i = 0; i < count; i++) { MailInfo info = arReport[i] as MailInfo; oClient.Delete(info); // Remove UIDL from local uidl file. oUIDLManager.RemoveUIDL(oServer, info.UIDL); } } // Delete method just mark the email as deleted, // Quit method pure the emails from server exactly. oClient.Quit(); } catch (Exception ep) { MessageBox.Show(ep.Message); } // Update the uidl list to local uidl file and then we can load it next time. oUIDLManager.Update(); lblStatus.Text = "Completed"; pgBar.Maximum = 100; pgBar.Minimum = 0; pgBar.Value = 0; btnStart.Enabled = true; btnCancel.Enabled = false; }