Beispiel #1
0
        public async Task SendMailAsync(IEnumerable <string> toAddress,
                                        TemplateEngine templateEngine = null, string ccAddress = null)
        {
            var enumerable = toAddress as string[] ?? toAddress.ToArray();

            if (!enumerable.Any())
            {
                throw new ArgumentNullException(nameof(toAddress));
            }

            switch (CurrentEngine)
            {
            case null when templateEngine == null:
                throw new Exception("TemplateEngine must be specified.");

            case null when true:
                CurrentEngine = templateEngine;
                break;
            }

            // create mail message
            var messageToSend = new MimeMessage
            {
                Sender  = new MailboxAddress(Settings.SenderName, Settings.SmtpUserName),
                Subject = CurrentEngine.Format(() => new StringBuilder(CurrentEngine.TextProvider.Subject)).Trim(),
            };

            messageToSend.From.Add(new MailboxAddress(Settings.EmailDisplayName, Settings.SmtpUserName));
            var bodyText = CurrentEngine.Format(() => new StringBuilder(CurrentEngine.TextProvider.Text)).Trim();

            if (CurrentEngine.TextProvider is TemplateMailMessage templateMailMessage && templateMailMessage.IsHtml)
            {
                messageToSend.Body = new TextPart(TextFormat.Html)
                {
                    Text = bodyText
                };
            }