예제 #1
0
        private void Send(MailMessage message, int retryCount)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            try
            {
                using (var smtpClient = new SmtpClient())
                {
                    // in development & qa, deliver mail to test mail server folder
                    if (smtpClient.DeliveryMethod == SmtpDeliveryMethod.SpecifiedPickupDirectory)
                    {
                        var path      = Path.Combine(HttpRuntime.AppDomainAppPath, _configurationManager.TestMailServer);
                        var directory = Directory.CreateDirectory(path);
                        smtpClient.PickupDirectoryLocation = directory.FullName;
                    }

                    // rename recipients when not deployed to prevent sending to unwanted recipients
                    if (!_configurationManager.IsDeployedToCloud)
                    {
                        var toAddress = message.To.First().Address;
                        message.To.Clear();
                        message.CC.Clear();
                        message.Bcc.Clear();
                        foreach (var interceptAddress in _configurationManager.EmailInterceptAddresses.Explode(";"))
                        {
                            message.To.Add(new MailAddress(interceptAddress, string.Format(
                                                               "Intended for {0} (UCosmic Mail Intercept)", toAddress)));
                        }
                    }

                    // send the message
                    smtpClient.Send(message);
                }
            }
            catch (Exception ex)
            {
                // log the exception
                _exceptionLogger.LogException(ex);

                // give up after trying 3 times
                if (++retryCount > 2)
                {
                    throw;
                }

                // wait 3 seconds and try to send the message again
                Thread.Sleep(3000);
                Send(message, retryCount);
            }
        }
예제 #2
0
        public void Handle(SendEmailMessageCommand command)
        {
            // get a fresh email address from the database
            EmailMessage emailMessage = null;

            while (emailMessage == null && ++_retryCount < RetryLimit)
            {
                if (_retryCount > 1)
                {
                    Thread.Sleep(300);
                }

                var person = _entities.Get <Person>()
                             .EagerLoad(_entities, new Expression <Func <Person, object> >[]
                {
                    p => p.Messages,
                })
                             .By(command.PersonId);
                emailMessage = person != null?person.GetMessage(command.MessageNumber) : null;
            }
            if (emailMessage == null)
            {
                var exception = new OperationCanceledException(string.Format(
                                                                   "Unable to locate EmailMessage number '{0}' for person '{1}'. The message send operation was canceled after {2} retries.",
                                                                   command.MessageNumber, command.PersonId, _retryCount));
                _exceptionLogger.LogException(exception);
                throw exception;
            }

            // convert email message to mail message
            var mail = _queryProcessor.Execute(
                new ComposeMailMessageQuery(emailMessage)
                );

            // send the mail message
            _mailSender.Send(mail);

            // log when the message was sent
            emailMessage.SentOnUtc = DateTime.UtcNow;
            _entities.Update(emailMessage);
        }
예제 #3
0
 public virtual JsonResult LogAjaxError(JQueryAjaxException model)
 {
     _exceptionLogger.LogException(model);
     return(Json(null, JsonRequestBehavior.AllowGet));
 }