protected override string GetBody(UserAccountEvent <HierarchicalUserAccount> evt, IDictionary <string, string> values)
        {
            string msgBody = string.Empty;

            switch (_msgBodyType)
            {
            case AuthCentralSmtpMessageDelivery.MsgBodyTypes.MultipartAlternativeAsJson:
            {
                var multipartMsgBody = new MultipartMessageBody()
                {
                    PlainText = FormatValue(evt, LoadBodyTemplate(evt, PLAIN_TEXT_FILE_EXTENSION), values),
                    Html      = FormatValue(evt, LoadBodyTemplate(evt, HTML_FILE_EXTENSION), values),
                };

                msgBody = JsonConvert.SerializeObject(multipartMsgBody);
                break;
            }

            case AuthCentralSmtpMessageDelivery.MsgBodyTypes.Html:
            {
                msgBody = FormatValue(evt, LoadBodyTemplate(evt, HTML_FILE_EXTENSION), values);
                break;
            }

            default:
            {
                msgBody = FormatValue(evt, LoadBodyTemplate(evt, PLAIN_TEXT_FILE_EXTENSION), values);
                break;
            }
            }

            return(msgBody);
        }
Example #2
0
        public void Send(Message msg)
        {
            Tracing.Information("[SmtpMessageDelivery.Send] sending mail to " + msg.To);

            if (String.IsNullOrWhiteSpace(msg.From))
            {
                msg.From = Config.Smtp.From;
            }

            using (SmtpClient smtp = new SmtpClient())
            {
                smtp.Host    = Config.Smtp.Host;
                smtp.Timeout = SmtpTimeout;

                try
                {
                    MailMessage mailMessage;

                    switch (MsgBodyType)
                    {
                    case MsgBodyTypes.Html:
                    {
                        mailMessage            = new MailMessage(msg.From, msg.To, msg.Subject, msg.Body);
                        mailMessage.IsBodyHtml = true;
                        break;
                    }

                    case MsgBodyTypes.MultipartAlternativeAsJson:
                    {
                        MultipartMessageBody multipart = JsonConvert.DeserializeObject <MultipartMessageBody>(msg.Body);

                        mailMessage = new MailMessage(msg.From, msg.To, msg.Subject, multipart.PlainText);
                        mailMessage.BodyEncoding    = Encoding.UTF8;
                        mailMessage.SubjectEncoding = Encoding.UTF8;

                        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(multipart.Html, null, MediaTypeNames.Text.Html);
                        mailMessage.AlternateViews.Add(htmlView);
                        break;
                    }

                    default:
                    {
                        mailMessage = new MailMessage(msg.From, msg.To, msg.Subject, msg.Body);
                        break;
                    }
                    }

                    smtp.Send(mailMessage);
                }
                catch (SmtpException e)
                {
                    Tracing.Error("[SmtpMessageDelivery.Send] SmtpException: " + e.Message);
                }
                catch (Exception e)
                {
                    Tracing.Error("[SmtpMessageDelivery.Send] Exception: " + e.Message);
                }
            }
        }