Exemplo n.º 1
0
        /// <summary>
        /// Creates an SMTP mail message for a specified mail item.
        /// </summary>
        static async Task <MailMessage> CreateMailMessage(IEmailMessage mailItem)
        {
            if (mailItem.SendableDate > LocalTime.Now)
            {
                return(null);                                       // Not due yet
            }
            var mail = new MailMessage {
                Subject = mailItem.Subject.Or("[NO SUBJECT]").Remove("\r", "\n")
            };

            mailItem.GetEffectiveToAddresses().Do(x => mail.To.Add(x));
            mailItem.GetEffectiveCcAddresses().Do(x => mail.CC.Add(x));
            mailItem.GetEffectiveBccAddresses().Do(x => mail.Bcc.Add(x));

            if (mail.To.None() && mail.CC.None() && mail.Bcc.None())
            {
                Debug.WriteLine($"Mail message {mailItem.GetId()} will not be sent as there is no effective recipient.");
                return(null);
            }

            mail.AlternateViews.AddRange(mailItem.GetEffectiveBodyViews());

            mail.From = new MailAddress(mailItem.GetEffectiveFromAddress(), mailItem.GetEffectiveFromName());

            mail.ReplyToList.Add(new MailAddress(mailItem.GetEffectiveReplyToAddress(),
                                                 mailItem.GetEffectiveReplyToName()));

            mail.Attachments.AddRange(await mailItem.GetAttachments());

            return(mail);
        }
Exemplo n.º 2
0
        string GenerateEmailView()
        {
            var r = new StringBuilder();

            r.AppendLine("<a href='/?Web.Test.Command=testEmail&to=" + To + "'>&lt;&lt; Back</a>");
            r.AppendLine("<h2>Subject: <u>" + Email.Subject.Or("[NO SUBJECT]") + "</u></h2>");
            r.AppendLine("<table cellspacing='0'>");

            var body = GetBodyHtml(Email.Body.Or("[EMPTY BODY]"), Email.Html);

            var toShow = new Dictionary <string, object> {
                { "Date", Email.SendableDate.ToString("yyyy-MM-dd") + " at " + Email.SendableDate.ToString("HH:mm") },
                { "From", Email.GetEffectiveFromAddress() },
                { "ReplyTo", Email.GetEffectiveReplyToAddress() },
                { "To", Email.GetEffectiveToAddresses() },
                { "Bcc", Email.GetEffectiveBccAddresses().ToString(", ") },
                { "Cc", Email.GetEffectiveCcAddresses().ToString(", ") },
                { "Subject", Email.Subject.Or("[NO SUBJECT]").HtmlEncode().WithWrappers("<b>", "</b>") },
                { "Body", body.WithWrappers("<div class='body'>", "</div>") },
                { "Attachments", GetAttachmentLinks(Email) }
            };

            foreach (var item in toShow.Where(x => x.Value.ToStringOrEmpty().HasValue()))
            {
                r.AppendLine("<tr>");
                r.AddFormattedLine("<td class='label'>{0}:</td>", item.Key.HtmlEncode());
                r.AddFormattedLine("<td>{0}</td>", item.Value);

                r.AppendLine("</tr>");
            }

            r.AppendLine("</table>");

            return(r.ToString());
        }