/// <summary>
 /// Defines the body of the message.
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="body">The body of the message.</param>
 /// <returns></returns>
 public static EmailMessageBuilder WithBody(this EmailMessageBuilder builder, string body)
 {
     if (string.IsNullOrEmpty(body))
     {
         throw new ArgumentException("A body for the message cannot be null or empty", nameof(body));
     }
     builder.Body = body;
     return(builder);
 }
 /// <summary>
 /// Adds one or more attachments to the message. Attachments length cannot exceed 20 MB.
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="attachments">Optional attachments contained in the message.</param>
 /// <returns></returns>
 public static EmailMessageBuilder WithAttachments(this EmailMessageBuilder builder, params FileAttachment[] attachments)
 {
     if (attachments?.Length == 0)
     {
         throw new ArgumentException("One or more attachments must be declared for the message.", nameof(attachments));
     }
     builder.Attachments = attachments;
     return(builder);
 }
 /// <summary>
 /// Defines the subject of the message.
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="subject">The subject of the message.</param>
 /// <returns></returns>
 public static EmailMessageBuilder WithSubject(this EmailMessageBuilder builder, string subject)
 {
     if (string.IsNullOrEmpty(subject))
     {
         throw new ArgumentException("A subject for the message cannot be null or empty", nameof(subject));
     }
     builder.Subject = subject;
     return(builder);
 }
 /// <summary>
 /// Defines the template used to render the email. Defaults to 'Email'. It has to be a Razor view, discoverable by the Razor Engine. (ex. Located in Views -> Shared folder).
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="template">The template used to render the email. Defaults to 'Email'.</param>
 /// <returns></returns>
 public static EmailMessageBuilder UsingTemplate(this EmailMessageBuilder builder, string template)
 {
     if (string.IsNullOrEmpty(template))
     {
         throw new ArgumentException("A template name cannot be null or empty", nameof(template));
     }
     builder.Template = template;
     return(builder);
 }
 /// <summary>
 /// Adds one or more recipients to the message.
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="recipients">The email addresses of the recipients.</param>
 /// <returns></returns>
 public static EmailMessageBuilder To(this EmailMessageBuilder builder, params string[] recipients)
 {
     if (recipients?.Length == 0)
     {
         throw new ArgumentException("One or more recipients must be declared for the message.", nameof(recipients));
     }
     foreach (var recipient in recipients.Distinct())
     {
         builder.Recipients.Add(recipient);
     }
     return(builder);
 }
Esempio n. 6
0
        /// <summary>
        /// Sends an email, along with template data, by using a fluent configuration.
        /// </summary>
        /// <typeparam name="TModel">The type of the data that will be applied to the template.</typeparam>
        /// <param name="emailService">Abstraction for sending email through different providers and implementations. SMTP, SparkPost, Mailchimp etc.</param>
        /// <param name="configureMessage">The delegate that will be used to build the message.</param>
        public static async Task SendAsync <TModel>(this IEmailService emailService, Action <EmailMessageBuilder <TModel> > configureMessage) where TModel : class
        {
            if (configureMessage == null)
            {
                throw new ArgumentNullException(nameof(configureMessage));
            }
            var messageBuilder = new EmailMessageBuilder <TModel>();

            configureMessage(messageBuilder);
            var message = messageBuilder.Build();
            await emailService.SendAsync(message.Recipients.ToArray(), message.Subject, message.Body, message.Template, message.Data, message.Attachments.ToArray());
        }
 /// <summary>
 /// Returns the <see cref="EmailMessage"/> instance made by the builder.
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <returns></returns>
 public static EmailMessage Build(this EmailMessageBuilder builder) =>
 new EmailMessage(builder.Recipients, builder.Subject, builder.Body, builder.Template, builder.Data, builder.Attachments);
 /// <summary>
 /// Adds a model that is passed to the email template.
 /// </summary>
 /// <param name="builder">The builder</param>
 /// <param name="data">Data that are passed to the email template.</param>
 /// <returns></returns>
 public static EmailMessageBuilder WithData <TModel>(this EmailMessageBuilder builder, TModel data) where TModel : class
 {
     builder.Data = data;
     return(builder);
 }