Example #1
0
        public static MailboxReaderConfig GetBugNetConfig()
        {
            var state = new MailboxReaderThreadState
            {
                UploadsFolderPath = @"c:\temp\",
            };

            var hostSettings = HostSettingManager.LoadHostSettings();

            var emailFormat = HostSettingManager.Get(hostSettings, HostSettingNames.SMTPEMailFormat, EmailFormatType.Text);

            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, false),
                ReportingUserName             = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ReportingUsername, string.Empty),
                ProcessAttachments            = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ProcessAttachments, true),
                UploadsFolderPath             = state.UploadsFolderPath,
                EmailFormatType = emailFormat,
                BodyTemplate    = GetEmailTemplateContent()
            };

            return(mailBoxConfig);
        }
Example #2
0
        public static MailboxReaderConfig GetBugNetConfig()
        {
            var state = new MailboxReaderThreadState
            {
                UploadsFolderPath = @"c:\temp\",
            };

            var hostSettings = HostSettingManager.LoadHostSettings();

            var emailFormat = HostSettingManager.Get(hostSettings, HostSettingNames.SMTPEMailFormat, EmailFormatType.Text);

            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, false),
                ReportingUserName = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ReportingUsername, string.Empty),
                ProcessAttachments = HostSettingManager.Get(hostSettings, HostSettingNames.Pop3ProcessAttachments, true),
                UploadsFolderPath = state.UploadsFolderPath,
                EmailFormatType = emailFormat,
                BodyTemplate = GetEmailTemplateContent()
            };

            return mailBoxConfig;
        }
        /// <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();
        }