Beispiel #1
0
        /// <summary>
        ///   Send email
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="recipients"></param>
        /// <param name="subject"></param>
        /// <param name="templatePlain"></param>
        /// <param name="templateHtml"></param>
        /// <param name="model"></param>
        /// <param name="templateKey"></param>
        /// <param name="attachments"></param>
        /// <param name="customHeaders"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task SendEmailAsync <T>(MailAddress sender, MailAddress[] recipients, string subject,
                                             string templatePlain,
                                             string templateHtml, T model, string templateKey, Attachment[] attachments = null,
                                             IDictionary <string, string> customHeaders = null)
        {
            if (string.IsNullOrEmpty(sender?.Address))
            {
                throw new ArgumentNullException(nameof(sender));
            }

            if (recipients == null || !recipients.Any())
            {
                throw new ArgumentNullException(nameof(recipients));
            }

            if (string.IsNullOrEmpty(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (string.IsNullOrEmpty(templatePlain))
            {
                throw new ArgumentNullException(nameof(templatePlain));
            }

            var modelType = typeof(T);

            var templateContentPlain = await _messageProvider.GetCompiledMessageAsync(templateKey + "Plain", templatePlain, model);

            var subjectProperty = modelType.GetProperties().SingleOrDefault(p => p.Name.ToLower() == "subject");

            if (subjectProperty != null && subjectProperty.PropertyType == typeof(string))
            {
                if (subjectProperty.GetValue(model) is string sVal)
                {
                    subject = sVal;
                }
            }

            var message = new MailMessage
            {
                SubjectEncoding = Encoding.UTF8,
                BodyEncoding    = Encoding.UTF8,
                From            = sender,
                Subject         = subject,
                Body            = templateContentPlain,
                IsBodyHtml      = false
            };

            if (customHeaders != null)
            {
                foreach (var ch in customHeaders.Where(ch => !message.Headers.AllKeys.Contains(ch.Key)))
                {
                    message.Headers.Add(ch.Key, ch.Value);
                }
            }

            if (!string.IsNullOrEmpty(templateHtml))
            {
                var templateContentHtml = await _messageProvider.GetCompiledMessageAsync(templateKey + "Html", templateHtml, model);

                templateContentHtml = EmbedImages(templateContentHtml, out var embeddedImages);

                var htmlView = AlternateView.CreateAlternateViewFromString(templateContentHtml);
                htmlView.ContentType = new ContentType("text/html")
                {
                    CharSet = "utf8"
                };

                foreach (var lr in embeddedImages)
                {
                    htmlView.LinkedResources.Add(lr);
                }

                message.AlternateViews.Add(htmlView);
            }

            if (_emailSettings.TestMode)
            {
                message.To.Add(_emailSettings.TestRecipient);
            }
            else
            {
                foreach (var r in recipients)
                {
                    message.To.Add(r);
                }
            }

            if (attachments != null)
            {
                foreach (var att in attachments)
                {
                    message.Attachments.Add(att);
                }
            }

            if (_emailSettings.SaveCopy &&
                !string.IsNullOrEmpty(_emailSettings.CopyLocation) &&
                Directory.Exists(_emailSettings.CopyLocation))
            {
                var client = new SmtpClient
                {
                    DeliveryMethod          = SmtpDeliveryMethod.SpecifiedPickupDirectory,
                    PickupDirectoryLocation = _emailSettings.CopyLocation
                };
                client.Send(message);
            }

            if (_emailSettings.Enabled)
            {
                await _mailProvider.SendAsync(message);
            }
        }