Exemple #1
0
        public async Task<bool> SendEmailAsync<TModel>(
            ISystemEmail from,
            IEnumerable<ISystemEmail> to,
            string subject,
            TModel model,
            IEnumerable<ISystemEmail> cced = null,
            IEnumerable<ISystemEmail> bcced = null,
            IEnumerable<Attachment> attachments = null)
        {
            if (from == null)
                throw new ArgumentNullException(nameof(from));
            if (to == null)
                throw new ArgumentNullException(nameof(to));
            if (string.IsNullOrWhiteSpace(subject))
                throw new ArgumentException("Non-empty value expected", nameof(subject));
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            string modelName = typeof(TModel).Name;
            if (!modelName.EndsWith("Model"))
                throw new InvalidOperationException("Mail model name should ends with 'Model'");
            string templateName = modelName.Substring(0, modelName.Length - "Model".Length);
            var body = await _viewRenderService.TransformAsync(templateName, model);

            return await SendEmailAsync(from, to, subject, body, cced, bcced, attachments);
        }
Exemple #2
0
        public async Task <OperationResult> SendAsync([FromBody] EmailDto dto)
        {
            _logger.Info($"SendAsync TemplateToken = {dto.TemplateToken}; AdobeConnectUrl = {dto.AdobeConnectUrl}; RemoteIpAddress = {Request.HttpContext.Connection.RemoteIpAddress}");

            var recipients = _notificationsSettings.RecipientSettings.GetByToken(dto.TemplateToken);
            var from       = _notificationsSettings.GetFrom(recipients);

            var to = dto.ToEmails.Select(x => new SystemEmail {
                Email = x.Email, Name = x.Name
            }).ToList();

            var cced = dto.CcEmails != null && dto.CcEmails.Any()
                           ? dto.CcEmails.Select(x => new SystemEmail {
                Email = x.Email, Name = x.Name
            }).ToList()
                           : null;

            var bcced = dto.BccEmails != null && dto.BccEmails.Any()
                            ? dto.BccEmails.Select(x => new SystemEmail {
                Email = x.Email, Name = x.Name
            }).ToList()
                            : null;

            var model = new EmailModel {
                MailBody = dto.BodyHtml, MailSubject = dto.Subject
            };
            var mailBody = await _templateTransformer.TransformAsync(dto.TemplateToken, model);

            var sendResult = await _mail.SendEmailAsync(from, to, dto.Subject, mailBody, cced, bcced);

            return(new OperationResult {
                IsSuccess = sendResult
            });
        }