Ejemplo 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);
        }