コード例 #1
0
        public void Send(EmailMessage message)
        {
            try
            {
                SendInternal(message);
            }
            catch (Exception ex)
            {
                var sentFail = new SentFail();
                sentFail.Message    = ex.Message;
                sentFail.Stack      = ex.ToString();
                sentFail.MessageId  = message.MessageId;
                sentFail.Recipients = message.Recipients;
                sentFail.Subject    = message.Subject;

                var failQueueName = message.SentFailQueueName ?? GlobalConfiguration.Configuration.SentFailQueueName;
                Bus.Send(failQueueName, sentFail);
            }
        }
コード例 #2
0
        public void SentFail(PendingMessage pending, string status, string message = null)
        {
            var sentFail = new SentFail();

            sentFail.Stack      = status;
            sentFail.Message    = message;
            sentFail.MessageId  = pending.MessageId;
            sentFail.Recipients = pending.Recipients;
            sentFail.Subject    = pending.Subject;

            var contentString = Newtonsoft.Json.JsonConvert.SerializeObject(sentFail);

            var path = System.IO.Path.GetDirectoryName(this.GetType().Assembly.Location);

            path = System.IO.Path.Combine(path, $"fails/{status.Replace("sendinblue:", "")}-{Guid.NewGuid()}.json");
            System.IO.File.WriteAllText(path, contentString);

            var failQueueName = GlobalConfiguration.Configuration.SentFailQueueName;

            Bus.Send(failQueueName, sentFail);
        }
コード例 #3
0
        public EmailMessage GetEmailMessage(EmailConfig emailConfig)
        {
            var sender = emailConfig.Sender;

            if (sender == null ||
                sender.Email == null)
            {
                sender = new Sender()
                {
                    Email       = GlobalConfiguration.Configuration.SenderEmail,
                    DisplayName = GlobalConfiguration.Configuration.SenderName,
                    Code        = GlobalConfiguration.Configuration.SenderCode,
                    JobTitle    = GlobalConfiguration.Configuration.SenderJobTitle
                };
            }

            // Deserialize object
            EmailModel em = null;

            try
            {
                em = ModelResolver.Convert(emailConfig);
            }
            catch (Exception ex)
            {
                GlobalConfiguration.Configuration.Logger.Error(ex);
                var failMessage = new SentFail();
                failMessage.FailDate   = DateTime.Now;
                failMessage.Message    = ex.Message;
                failMessage.MessageId  = emailConfig.MessageId;
                failMessage.Recipients = emailConfig.Recipients;
                failMessage.Stack      = string.Format("EmailConfig : {0}\r\n{1}", emailConfig, ex.ToString());
                failMessage.Subject    = "Fail to convert emailconfig model";

                var queueName = emailConfig.SentFailQueueName ?? GlobalConfiguration.Configuration.SentFailQueueName;
                Bus.Send(queueName, failMessage);
            }

            if (em == null)
            {
                em = new EmailModel();
            }

            // Create emailView
            dynamic emailView = CreateEmailView(emailConfig.EmailName, em.Model);

            if (emailConfig.Parameters != null)
            {
                foreach (var prm in emailConfig.Parameters)
                {
                    emailView.ViewData.Add(prm.Name, prm.Value);
                }
            }
            emailView.Sender    = sender;
            emailView.MessageId = emailConfig.MessageId;

            // Create emailMessage
            EmailMessage emailMessage = null;

            try
            {
                emailMessage = CreateEmailMessage(emailView);
            }
            catch (Exception ex)
            {
                GlobalConfiguration.Configuration.Logger.Error(ex);
                var failMessage = new SentFail();
                failMessage.FailDate   = DateTime.Now;
                failMessage.Message    = ex.Message;
                failMessage.MessageId  = emailConfig.MessageId;
                failMessage.Recipients = emailConfig.Recipients;
                failMessage.Stack      = ex.ToString();
                failMessage.Subject    = "Fail create email message";
                failMessage.ViewName   = emailConfig.EmailName;

                var queueName = emailConfig.SentFailQueueName ?? GlobalConfiguration.Configuration.SentFailQueueName;
                Bus.Send(queueName, failMessage);
            }

            if (emailMessage == null)
            {
                return(null);
            }

            emailMessage.Sender      = sender;
            emailMessage.SenderAlias = emailConfig.SenderAlias;

            var recipientList = emailMessage.Recipients.ToList();

            recipientList.AddRange(emailConfig.Recipients);
            emailMessage.Recipients.Clear();

            while (true)
            {
                var first = recipientList.FirstOrDefault();
                if (first == null)
                {
                    break;
                }
                recipientList.Remove(first);
                var existing = emailMessage.Recipients.SingleOrDefault(i => i.Address.Equals(first.Address, StringComparison.InvariantCultureIgnoreCase));
                if (existing != null)
                {
                    existing.DisplayName = existing.DisplayName ?? first.DisplayName;
                    existing.RecipientId = existing.RecipientId ?? first.RecipientId;
                }
                else
                {
                    emailMessage.Recipients.Add(first);
                }
            }

            emailMessage.Headers.Add(new EmailMessageHeader()
            {
                Name = "X-Mailer-GEN", Value = "QMailer"
            });
            emailMessage.Headers.Add(new EmailMessageHeader()
            {
                Name = "X-Mailer-MID", Value = emailConfig.MessageId
            });
            if (emailConfig.Headers != null)
            {
                foreach (var h in emailConfig.Headers)
                {
                    emailMessage.Headers.Add(new EmailMessageHeader()
                    {
                        Name = h.Name, Value = h.Value
                    });
                }
            }

            emailMessage.MessageId  = emailConfig.MessageId;
            emailMessage.DoNotTrack = emailConfig.DoNotTrack;
            emailMessage.EntityId   = em.EntityId ?? emailConfig.EntityId;
            emailMessage.EntityName = em.EntityName ?? emailConfig.EntityName;
            if (emailMessage.Attachments == null || emailMessage.Attachments.Count == 0)
            {
                emailMessage.Attachments = emailConfig.Attachments;
            }
            else if (emailConfig.Attachments != null)
            {
                emailMessage.Attachments.AddRange(emailConfig.Attachments);
            }
            emailMessage.EmailBodyRequestedQueueName = emailConfig.EmailBodyRequestedQueueName;
            emailMessage.SendEmailQueueName          = emailConfig.SendEmailQueueName;
            emailMessage.SentFailQueueName           = emailConfig.SentFailQueueName;
            emailMessage.SentMessageQueueName        = emailConfig.SentMessageQueueName;

            return(emailMessage);
        }
コード例 #4
0
        public void Send(EmailMessage message)
        {
            var client = new MailJetClient(PublicKey, PrivateKey);

            Models.SentMessageData result = null;

            try
            {
                result = client.SendMessage(message);
            }
            catch (Exception ex)
            {
                GlobalConfiguration.Configuration.Logger.Error(ex);

                var sentFail = new SentFail();
                sentFail.Message    = GetFirstGoodErrorMessage(ex);
                sentFail.Stack      = ex.ToString();
                sentFail.MessageId  = message.MessageId;
                sentFail.Recipients = message.Recipients;
                sentFail.Subject    = message.Subject;

                var failQueueName = message.SentFailQueueName ?? GlobalConfiguration.Configuration.SentFailQueueName;
                Bus.Send(failQueueName, sentFail);
            }

            if (result == null)
            {
                return;
            }

            if (!message.DoNotTrack)
            {
                var sentMessage = new SentMessage();
                sentMessage.Body       = message.Body;
                sentMessage.MessageId  = message.MessageId;
                sentMessage.Recipients = message.Recipients;
                sentMessage.Subject    = message.Subject;
                sentMessage.SmtpInfo   = "mailjetapi";
                sentMessage.Sender     = message.Sender;
                sentMessage.EntityId   = message.EntityId;
                sentMessage.EntityName = message.EntityName;

                if (message.SenderAlias != null)
                {
                    sentMessage.Sender = message.SenderAlias;
                }

                var queueName = message.SentMessageQueueName ?? GlobalConfiguration.Configuration.SentMessageQueueName;
                if (message.SentMessage == null)
                {
                    Bus.Send(queueName, sentMessage);
                }
                else
                {
                    Bus.Send(queueName, message.SentMessage);
                }
            }

            //foreach (var item in result.Sent)
            //{
            //	var pending = new PendingMessage();
            //	pending.MessageId = message.MessageId;
            //	pending.MailJetMessageId = item.MessageID;
            //	pending.Email = item.Email;
            //	pending.SendDate = DateTime.Now;
            //	pending.Subject = message.Subject;
            //	pending.Recipients = message.Recipients;

            //	StatusChecker.Current.Add(pending);
            //}
        }
コード例 #5
0
        private void SendInternal(EmailMessage message)
        {
            var mailMessage = (System.Net.Mail.MailMessage)message;

            if (message.Attachments == null ||
                message.Attachments.Count == 0)
            {
                AddDkimHeader(mailMessage);
            }

            var htmlView = AlternateView.CreateAlternateViewFromString(message.Body, null, "text/html");

            htmlView.TransferEncoding = System.Net.Mime.TransferEncoding.QuotedPrintable;
            mailMessage.AlternateViews.Add(htmlView);

            Exception sentFailException = null;
            bool      isCanceled        = false;
            string    senderHost        = null;

            using (var sender = GlobalConfiguration.Configuration.SmtpClientFactory.Create(message.Recipients))
            {
                senderHost = sender.Host;
                using (var mailSentEvent = new System.Threading.ManualResetEvent(false))
                {
                    sender.SendCompleted += (s, arg) =>
                    {
                        if (arg.Error != null)
                        {
                            sentFailException = arg.Error;
                        }

                        if (arg.Cancelled)
                        {
                            isCanceled = true;
                        }

                        try
                        {
                            if (!mailSentEvent.SafeWaitHandle.IsClosed)
                            {
                                mailSentEvent.Set();
                            }
                        }
                        catch (Exception ex)
                        {
                            GlobalConfiguration.Configuration.Logger.Error(ex);
                        }
                    };

                    sender.SendAsync(mailMessage, message);

                    var isSent = mailSentEvent.WaitOne(30 * 1000);
                    if (!isSent)
                    {
                        sender.SendAsyncCancel();
                        isCanceled = true;
                    }
                    else
                    {
                        GlobalConfiguration.Configuration.Logger.Debug("mail for {0} sent", mailMessage.To.First().Address);
                    }
                }
            }

            var sentFailQueueName = message.SentFailQueueName;

            if (string.IsNullOrWhiteSpace(sentFailQueueName))
            {
                sentFailQueueName = GlobalConfiguration.Configuration.SentFailQueueName;
            }

            if (isCanceled)
            {
                var sentFail = new SentFail();
                sentFail.Message    = "Canceled";
                sentFail.Stack      = "Canceled";
                sentFail.MessageId  = message.MessageId;
                sentFail.Recipients = message.Recipients;
                sentFail.Subject    = message.Subject;

                Bus.Send(sentFailQueueName, sentFail);

                GlobalConfiguration.Configuration.Logger.Warn("Email '{0}' sending was canceled", message.Subject);
            }
            else if (sentFailException != null)
            {
                var sentFail = new SentFail();
                sentFail.Message    = sentFailException.Message;
                sentFail.Stack      = sentFailException.ToString();
                sentFail.MessageId  = message.MessageId;
                sentFail.Recipients = message.Recipients;
                sentFail.Subject    = message.Subject;

                GlobalConfiguration.Configuration.Logger.Error(sentFailException);

                Bus.Send(sentFailQueueName, sentFail);
            }
            else if (!message.DoNotTrack)
            {
                var sentMessage = new SentMessage();
                sentMessage.Body       = message.Body;
                sentMessage.MessageId  = message.MessageId;
                sentMessage.Recipients = message.Recipients;
                sentMessage.Subject    = message.Subject;
                sentMessage.SmtpInfo   = senderHost;
                sentMessage.Sender     = message.Sender;
                sentMessage.EntityId   = message.EntityId;
                sentMessage.EntityName = message.EntityName;

                if (message.SenderAlias != null)
                {
                    sentMessage.Sender = message.SenderAlias;
                }

                var queueName = message.SentMessageQueueName ?? GlobalConfiguration.Configuration.SentMessageQueueName;
                Bus.Send(queueName, sentMessage);
            }
        }
コード例 #6
0
        public void Send(EmailMessage message)
        {
            var client = new SendInBlueClient(ApiKey);

            Models.CreateSmtpEmail result = null;
            var content = client.CreateSendinBlueMessage(message);

            try
            {
                result = client.SendMessage(content);
            }
            catch (Exception ex)
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(message);
                ex.Data.Add("MessageContent", json);

                var sendinBlueMessageJson = Newtonsoft.Json.JsonConvert.SerializeObject(content);
                ex.Data.Add("SendinBlueMessageContent", sendinBlueMessageJson);

                ex.Data.Add("Subject", message.Subject);
                try
                {
                    if (message.Recipients != null)
                    {
                        var recipient = message.Recipients.FirstOrDefault(i => i.SendingType == EmailSendingType.To);
                        if (recipient == null)
                        {
                            recipient = message.Recipients.FirstOrDefault();
                        }
                        if (recipient != null)
                        {
                            ex.Data.Add("Email", recipient.Address);
                        }
                    }
                }
                catch (Exception subEx)
                {
                    GlobalConfiguration.Configuration.Logger.Error(subEx);
                }

                GlobalConfiguration.Configuration.Logger.Error(ex);

                var sentFail = new SentFail();
                sentFail.Message    = GetFirstGoodErrorMessage(ex);
                sentFail.Stack      = ex.ToString();
                sentFail.MessageId  = message.MessageId;
                sentFail.Recipients = message.Recipients;
                sentFail.Subject    = message.Subject;

                var failQueueName = message.SentFailQueueName ?? GlobalConfiguration.Configuration.SentFailQueueName;
                Bus.Send(failQueueName, sentFail);
            }

            if (result == null)
            {
                return;
            }

            if (!message.DoNotTrack)
            {
                var sentMessage = new SentMessage();
                sentMessage.Body       = message.Body;
                sentMessage.MessageId  = message.MessageId;
                sentMessage.Recipients = message.Recipients;
                sentMessage.Subject    = message.Subject;
                sentMessage.SmtpInfo   = "sendinblueapi";
                sentMessage.Sender     = message.Sender;
                sentMessage.EntityId   = message.EntityId;
                sentMessage.EntityName = message.EntityName;

                if (message.SenderAlias != null)
                {
                    sentMessage.Sender = message.SenderAlias;
                }

                var queueName = message.SentMessageQueueName ?? GlobalConfiguration.Configuration.SentMessageQueueName;
                if (message.SentMessage == null)
                {
                    Bus.Send(queueName, sentMessage);
                }
                else
                {
                    Bus.Send(queueName, message.SentMessage);
                }
            }

            var pending = new Models.PendingMessage();

            pending.MessageId           = message.MessageId;
            pending.SendInBlueMessageId = result.MessageId;
            pending.SendDate            = DateTime.Now;
            pending.Subject             = message.Subject;
            pending.Recipients          = message.Recipients;

            StatusChecker.Current.Add(pending);
        }