Example #1
0
        public bool SendMail()
        {
            bool success = true;

            try
            {
                MailClient.Host = SmtpServer;
                if (UseSSL.HasValue)
                {
                    MailClient.EnableSsl = UseSSL.Value;
                }

                if (Port.HasValue)
                {
                    MailClient.Port = Port.Value;
                }

                if (!string.IsNullOrEmpty(SmtpUser))
                {
                    MailClient.Credentials = new NetworkCredential(SmtpUser, SmtpPassword);
                }

                MailMessage message = new MailMessage(From, To)
                {
                    Subject = Subject,
                    Body    = Body
                };

                MailClient.Send(message);
            }
            catch (Exception mailException)
            {
                Errors.Add(mailException.Message);
                success = false;
            }
            finally
            {
                MailClient.Dispose();
            }

            return(success);
        }
        private void CloseMailClient(MailClient client, MailBox mailbox, ILogger log)
        {
            if (client == null)
            {
                return;
            }

            try
            {
                client.Authenticated -= ClientOnAuthenticated;
                client.GetMessage    -= ClientOnGetMessage;

                client.Cancel();
                client.Dispose();
            }
            catch (Exception ex)
            {
                log.Error(
                    "CloseMailClient(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex.Message);
            }
        }
        private MailBox ProcessMailbox(MailClient client, TasksConfig tasksConfig)
        {
            var mailbox = client.Account;

            Stopwatch watch = null;

            if (_tasksConfig.CollectStatistics)
            {
                watch = new Stopwatch();
                watch.Start();
            }

            var failed = false;

            var taskLogger = LoggerFactory.GetLogger(LoggerFactory.LoggerType.Log4Net,
                                                     string.Format("Mbox_{0} Task_{1}", mailbox.MailBoxId, Task.CurrentId));

            taskLogger.Info(
                "ProcessMailbox(Tenant = {0}, MailboxId = {1} Address = '{2}') Is {3}",
                mailbox.TenantId, mailbox.MailBoxId,
                mailbox.EMail, mailbox.Active ? "Active" : "Inactive");

            try
            {
                client.Log = taskLogger;

                client.GetMessage += ClientOnGetMessage;

                client.Aggregate(tasksConfig, tasksConfig.MaxMessagesPerSession);
            }
            catch (OperationCanceledException)
            {
                taskLogger.Info(
                    "[CANCEL] ProcessMailbox(Tenant = {0}, MailboxId = {1}, Address = '{2}')",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail);

                NotifySignalrIfNeed(mailbox, taskLogger);
            }
            catch (Exception ex)
            {
                taskLogger.Error(
                    "ProcessMailbox(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail,
                    ex is ImapProtocolException || ex is Pop3ProtocolException ? ex.Message : ex.ToString());

                failed = true;
            }
            finally
            {
                try
                {
                    client.GetMessage -= ClientOnGetMessage;

                    client.Dispose();
                }
                catch (Exception ex)
                {
                    _log.Error(
                        "[DISPOSE] ProcessMailbox->client.Dispose(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                        mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail,
                        ex is ImapProtocolException || ex is Pop3ProtocolException ? ex.Message : ex.ToString());
                }

                if (_tasksConfig.CollectStatistics && watch != null)
                {
                    watch.Stop();

                    LogStat(PROCESS_MAILBOX, mailbox, watch.Elapsed, failed);
                }
            }

            var manager = new MailBoxManager(taskLogger);

            var state = GetMailboxState(manager, mailbox, taskLogger);

            switch (state)
            {
            case MailboxState.NoChanges:
                taskLogger.Info("MailBox with id={0} not changed.", mailbox.MailBoxId);
                break;

            case MailboxState.Disabled:
                taskLogger.Info("MailBox with id={0} is deactivated.", mailbox.MailBoxId);
                break;

            case MailboxState.Deleted:
                taskLogger.Info("MailBox with id={0} is removed.", mailbox.MailBoxId);

                try
                {
                    taskLogger.Info("RemoveMailBox(id={0}) >> Try clear new data from removed mailbox", mailbox.MailBoxId);
                    manager.RemoveMailBox(mailbox);
                }
                catch (Exception exRem)
                {
                    taskLogger.Info(
                        "[REMOVE] ProcessMailbox->RemoveMailBox(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                        mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, exRem.Message);
                }
                break;

            case MailboxState.DateChanged:
                taskLogger.Info("MailBox with id={0}: beginDate was changed.", mailbox.MailBoxId);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            taskLogger.Info("Mailbox '{0}' has been processed.", mailbox.EMail);

            return(mailbox);
        }
Example #4
0
 public void Dispose()
 {
     _client.Dispose();
 }
        private MailClient CreateMailClient(MailBox mailbox, ILogger log, CancellationToken cancelToken)
        {
            MailClient client = null;

            var connectError = false;

            Stopwatch watch = null;

            if (_tasksConfig.CollectStatistics)
            {
                watch = new Stopwatch();
                watch.Start();
            }

            var manager = new MailBoxManager(log)
            {
                AuthErrorWarningTimeout = _tasksConfig.AuthErrorWarningTimeout,
                AuthErrorDisableTimeout = _tasksConfig.AuthErrorDisableMailboxTimeout
            };

            try
            {
                client = new MailClient(mailbox, cancelToken, _tasksConfig.TcpTimeout,
                                        _tasksConfig.SslCertificateErrorsPermit, _tasksConfig.ProtocolLogPath, log, true);

                log.Debug("MailClient.LoginImapPop(Tenant = {0}, MailboxId = {1} Address = '{2}')",
                          mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail);

                client.Authenticated += ClientOnAuthenticated;

                client.LoginImapPop();
            }
            catch (TimeoutException exTimeout)
            {
                log.Warn(
                    "[TIMEOUT] CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, exTimeout.ToStringDescriptive());

                connectError = true;
            }
            catch (OperationCanceledException)
            {
                log.Info(
                    "[CANCEL] CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail);
                connectError = true;
            }
            catch (AuthenticationException authEx)
            {
                log.Error(
                    "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, authEx.ToStringDescriptive());

                if (!mailbox.AuthErrorDate.HasValue)
                {
                    manager.SetMailboxAuthError(mailbox, true);
                }

                connectError = true;
            }
            catch (Exception ex)
            {
                log.Error(
                    "CreateTasks->client.LoginImapPop(Tenant = {0}, MailboxId = {1}, Address = '{2}')\r\nException: {3}\r\n",
                    mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail,
                    ex is ImapProtocolException || ex is Pop3ProtocolException ? ex.Message : ex.ToString());

                connectError = true;
            }
            finally
            {
                if (connectError)
                {
                    try
                    {
                        if (client != null)
                        {
                            client.Authenticated -= ClientOnAuthenticated;
                            client.Cancel();
                            client.Dispose();
                            client = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(
                            "CreateTasks->client.Dispose(Tenant = {0}, MailboxId = {1}, Address = '{2}') Exception: {3}",
                            mailbox.TenantId, mailbox.MailBoxId, mailbox.EMail, ex.Message);

                        client = null;
                    }
                }

                if (_tasksConfig.CollectStatistics && watch != null)
                {
                    watch.Stop();

                    LogStat(CONNECT_MAILBOX, mailbox, watch.Elapsed, connectError);
                }
            }

            return(client);
        }