/// <summary> /// Schedule's the work callback. /// </summary> /// <param name="sender">The sender.</param> private static void ScheduledWorkCallback(object sender) { var state = sender as MailboxReaderThreadState; // if the timer is disabled the exit out lock (_locker) if (_timerIsDisabled) return; // are we currently processing the mailbox? // this is here to stop the mailbox processing when the callback is called again and we have not finished // processing the previous poll lock (_locker) if (_isMailboxReaderProcessing) return; try { if (_timer == null) { if (_readerErrors.Equals(0)) { Log.Error("MailboxReaderModule: First instance of mailbox reader timer is null"); } throw new Exception("MailboxReaderModule: Mailbox reader timer is null"); } // set the flag we are processing lock (_locker) _isMailboxReaderProcessing = true; //stop the timer lock (_locker) _timer.Change(Timeout.Infinite, Timeout.Infinite); var assemblyUri = new Uri(Assembly.GetExecutingAssembly().CodeBase); var path = Path.GetDirectoryName(assemblyUri.LocalPath).Replace("\\bin", ""); var hostSettings = HostSettingManager.LoadHostSettings(); var emailFormat = HostSettingManager.Get(hostSettings, HostSettingNames.SMTPEMailFormat, EmailFormatType.Text); var pop3TemplatePath = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3BodyTemplate, "templates/NewMailboxIssue.xslt"); var mailBoxConfig = new MailboxReaderConfig { Server = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Server, string.Empty), Port = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Port, 110), UseSsl = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3UseSSL, false), Username = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Username, string.Empty), Password = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Password, string.Empty), ProcessInlineAttachedPictures = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3InlineAttachedPictures, false), DeleteAllMessages = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3DeleteAllMessages, true), ReportingUserName = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ReportingUsername, string.Empty), ProcessAttachments = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ProcessAttachments, true), UploadsFolderPath = (state == null) ? Path.Combine(HostSettingManager.Get(HostSettingNames.AttachmentUploadPath), path) : state.UploadsFolderPath, AllowedFileExtensions = HostSettingManager.Get(hostSettings, HostSettingNames.AllowedFileExtensions, "."), FileSizeLimit = HostSettingManager.Get(hostSettings, HostSettingNames.FileSizeLimit, 0), EmailFormatType = emailFormat }; if (File.Exists(Path.Combine(path, pop3TemplatePath))) { mailBoxConfig.BodyTemplate = File.ReadAllText(Path.Combine(path, pop3TemplatePath)); } var mailboxReader = new MailboxReader.MailboxReader(mailBoxConfig); // read the mail for the mailboxes var result = mailboxReader.ReadMail(); // handle the result of the read // we should never get an exception from the reader only statuses, this so we don't kill the background thread // unless it is absolute switch (result.Status) { case ResultStatuses.None: case ResultStatuses.Success: lock (_locker) _readerErrors = 0; break; case ResultStatuses.Failed: foreach (var processingMessage in result.ProcessingMessages) { Log.Warn(processingMessage); } break; case ResultStatuses.FailedWithException: Log.Error(result.LastException); lock (_locker) _readerErrors++; break; default: throw new ArgumentOutOfRangeException(); } } catch(Exception ex) { Log.Error(ex); lock (_locker) _readerErrors++; } finally { // set the flag back so we are not processing _isMailboxReaderProcessing = false; } lock (_locker) { if (_readerErrors < CNST_MAX_READER_ERRORS) { if (_timer != null) { // start the timer up again lock (_locker) _timer.Change(_interval, _interval); return; } } } Log.Error(string.Format("MailboxReaderModule: The Mailbox reader has thrown [{0}] consecutive errors and will be disabled", _readerErrors)); _timerIsDisabled = true; if (_timer == null) return; _timerIsActive = false; _timer.Dispose(); }
/// <summary> /// Schedule's the work callback. /// </summary> /// <param name="sender">The sender.</param> private static void ScheduledWorkCallback(object sender) { var state = sender as MailboxReaderThreadState; // if the timer is disabled the exit out lock (_locker) if (_timerIsDisabled) { return; } // are we currently processing the mailbox? // this is here to stop the mailbox processing when the callback is called again and we have not finished // processing the previous poll lock (_locker) if (_isMailboxReaderProcessing) { return; } try { if (_timer == null) { if (_readerErrors.Equals(0)) { Log.Error("MailboxReaderModule: First instance of mailbox reader timer is null"); } throw new Exception("MailboxReaderModule: Mailbox reader timer is null"); } // set the flag we are processing lock (_locker) _isMailboxReaderProcessing = true; //stop the timer lock (_locker) _timer.Change(Timeout.Infinite, Timeout.Infinite); var assemblyUri = new Uri(Assembly.GetExecutingAssembly().CodeBase); var path = Path.GetDirectoryName(assemblyUri.LocalPath).Replace("\\bin", ""); var hostSettings = HostSettingManager.LoadHostSettings(); var emailFormat = HostSettingManager.Get(hostSettings, HostSettingNames.SMTPEMailFormat, EmailFormatType.Text); var pop3TemplatePath = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3BodyTemplate, "templates/NewMailboxIssue.xslt"); var mailBoxConfig = new MailboxReaderConfig { Server = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Server, string.Empty), Port = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Port, 110), UseSsl = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3UseSSL, false), Username = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Username, string.Empty), Password = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3Password, string.Empty), ProcessInlineAttachedPictures = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3InlineAttachedPictures, false), DeleteAllMessages = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3DeleteAllMessages, true), ReportingUserName = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ReportingUsername, string.Empty), ProcessAttachments = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ProcessAttachments, true), UploadsFolderPath = (state == null) ? Path.Combine(HostSettingManager.Get(HostSettingNames.AttachmentUploadPath), path) : state.UploadsFolderPath, AllowedFileExtensions = HostSettingManager.Get(hostSettings, HostSettingNames.AllowedFileExtensions, "."), FileSizeLimit = HostSettingManager.Get(hostSettings, HostSettingNames.FileSizeLimit, 0), EmailFormatType = emailFormat }; if (File.Exists(Path.Combine(path, pop3TemplatePath))) { mailBoxConfig.BodyTemplate = File.ReadAllText(Path.Combine(path, pop3TemplatePath)); } var mailboxReader = new MailboxReader.MailboxReader(mailBoxConfig); // read the mail for the mailboxes var result = mailboxReader.ReadMail(); // handle the result of the read // we should never get an exception from the reader only statuses, this so we don't kill the background thread // unless it is absolute switch (result.Status) { case ResultStatuses.None: case ResultStatuses.Success: lock (_locker) _readerErrors = 0; break; case ResultStatuses.Failed: foreach (var processingMessage in result.ProcessingMessages) { Log.Warn(processingMessage); } break; case ResultStatuses.FailedWithException: Log.Error(result.LastException); lock (_locker) _readerErrors++; break; default: throw new ArgumentOutOfRangeException(); } } catch (Exception ex) { Log.Error(ex); lock (_locker) _readerErrors++; } finally { // set the flag back so we are not processing _isMailboxReaderProcessing = false; } lock (_locker) { if (_readerErrors < CNST_MAX_READER_ERRORS) { if (_timer != null) { // start the timer up again lock (_locker) _timer.Change(_interval, _interval); return; } } } Log.Error(string.Format("MailboxReaderModule: The Mailbox reader has thrown [{0}] consecutive errors and will be disabled", _readerErrors)); _timerIsDisabled = true; if (_timer == null) { return; } _timerIsActive = false; _timer.Dispose(); }
/// <summary> /// Polls the mailbox using the specified context. /// </summary> /// <param name="context">The context.</param> private void Poll(HttpContext context) { //poll mailboxes here. HttpContext.Current = context; try { var mailboxReader = new MailboxReader.MailboxReader(HostSettingManager.Get(HostSettingNames.Pop3Server), Convert.ToInt32(HostSettingManager.Get(HostSettingNames.Pop3Port)), Boolean.Parse(HostSettingManager.Get(HostSettingNames.Pop3UseSSL)), HostSettingManager.Get(HostSettingNames.Pop3Username), HostSettingManager.Get(HostSettingNames.Pop3Password), Convert.ToBoolean(HostSettingManager.Get(HostSettingNames.Pop3InlineAttachedPictures)), HostSettingManager.Get(HostSettingNames.Pop3BodyTemplate), Convert.ToBoolean(HostSettingManager.Get(HostSettingNames.Pop3DeleteAllMessages)), HostSettingManager.Get(HostSettingNames.Pop3ReportingUsername), Convert.ToBoolean(HostSettingManager.Get(HostSettingNames.Pop3ProcessAttachments))); mailboxReader.ReadMail(); // Clear the number of consecutive errors. _readerErrors = 0; } catch (Exception ex) { // Log.Error("Mailbox reader failed", ex); // too many log entries // Increment the number of consecutive errors. _readerErrors++; } // Are there too many errors? CheckToDisableReader(); }