/// <summary> /// Renders an email view. /// </summary> /// <param name="email">The email to render.</param> /// <param name="viewName">Optional email view name override. If null then the email's ViewName property is used instead.</param> /// <returns>The rendered email view output.</returns> public string Render(EmailNotificationRequest email, string viewName = null) { viewName = viewName ?? email.ViewName; var controllerContext = CreateControllerContext(); var view = CreateView(viewName, controllerContext); var viewOutput = RenderView(view, email.ViewData, controllerContext); return(viewOutput); }
private static EmailNotificationRequest Clone(EmailNotificationRequest request) { // Do not clone with .WithViewModel(...) as it is cloned already with .WithViewData(...) return(new EmailNotificationRequestBuilder() .WithAttachments(request.Attachments) .WithBcc(request.Bcc) .WithCc(request.Cc) .WithTo(request.To) .WithFrom(request.From.Address, request.From.DisplayName) .WithSubject(request.Subject) .WithViewName(request.ViewName) .WithViewData(request.ViewData) .Build()); }
private string CreateBody(EmailNotificationRequest request, out bool isHtml) { isHtml = true; if (!string.IsNullOrWhiteSpace(request.ViewName)) { return(_renderer.Render(request)); } if (!string.IsNullOrEmpty(request.HtmlBody?.ToString())) { return(request.HtmlBody.ToString()); } isHtml = false; return(request.Body); }
public EmailNotificationResponse Send(EmailNotificationRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request), "EmailNotificationRequest cannot be null"); } if (!_whitelistConfiguration.HasWhitelist) { return(_emailClient.Send(request)); } request.To = WhiteList(request.To); request.Cc = WhiteList(request.Cc); request.Bcc = WhiteList(request.Bcc); return(_emailClient.Send(request)); }
public MailMessage Create(EmailNotificationRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request), "EmailNotificationRequest cannot be null"); } if (request.To == null) { throw new ArgumentNullException( $"{nameof(request)}.{nameof(request.To)}", "To email address cannot be null"); } if (request.From == null) { throw new ArgumentNullException( $"{nameof(request)}.{nameof(request.From)}", "From email address cannot be null"); } var mail = new MailMessage { Subject = request.Subject, From = request.From }; CopyAddress(request.To, mail.To); CopyAddress(request.Cc, mail.CC); CopyAddress(request.Bcc, mail.Bcc); CopyAddress(request.ReplyTo, mail.ReplyToList); CopyAttachments(request.Attachments, mail.Attachments); bool isHtml; mail.Body = CreateBody(request, out isHtml); mail.IsBodyHtml = isHtml; mail.BodyEncoding = Encoding.UTF8; return(mail); }
public async Task <EmailNotificationResponse> SendAsync(EmailNotificationRequest request) { var response = new EmailNotificationResponse(); try { using (var mail = _mailMessageFactory.Create(request)) using (var client = new SmtpClient()) { await client.SendMailAsync(mail).ConfigureAwait(false); } response.IsSent = true; } catch (Exception e) { response.Message = e.Message; Log.Error($"Email failed to: {request.To}. Subject: {request.Subject}", e); } return(response); }
public EmailNotificationResponse Send(EmailNotificationRequest request) { var response = new EmailNotificationResponse(); try { using (var mail = _mailMessageFactory.Create(request)) using (var client = new SmtpClient()) { client.Send(mail); } response.IsSent = true; } catch (Exception e) { response.Message = e.Message; Log.Error($"Email failed to: {request.To}. Subject: {request.Subject}", e); } return(response); }
/// <summary> /// Creates new instance of EmailNotificationRequestBuilder. /// </summary> /// <param name="request">Existing EmailNotificationRequest from which to copy values. Creates empty EmailNotificationRequest if null passed.</param> public EmailNotificationRequestBuilder(EmailNotificationRequest request = null) { _request = request != null?Clone(request) : new EmailNotificationRequest(); }