From 60f466c2b4e9c6920b9962add16021a3bfc49b52 Mon Sep 17 00:00:00 2001 From: Jericho Date: Wed, 28 Apr 2021 18:36:12 -0400 Subject: [PATCH] Simplify IMail interface Resolves #387 --- Source/StrongGrid/Extensions/Public.cs | 602 +++++++++++++++++++++++++ Source/StrongGrid/Resources/IMail.cs | 458 ------------------- Source/StrongGrid/Resources/Mail.cs | 588 +----------------------- 3 files changed, 603 insertions(+), 1045 deletions(-) diff --git a/Source/StrongGrid/Extensions/Public.cs b/Source/StrongGrid/Extensions/Public.cs index ba81e0e0..dd86bfa2 100644 --- a/Source/StrongGrid/Extensions/Public.cs +++ b/Source/StrongGrid/Extensions/Public.cs @@ -15,6 +15,608 @@ namespace StrongGrid /// public static class Public { + /// + /// Send an email to a single recipient without using a template (which means you must provide the subject, html content and text content). + /// + /// The mail resource. + /// To. + /// From. + /// The subject. + /// Content of the HTML. + /// Content of the text. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This overload is ideal when sending an email without using a template. + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendToSingleRecipientAsync( + this IMail mailResource, + MailAddress to, + MailAddress from, + string subject, + string htmlContent, + string textContent, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var recipients = new[] { to }; + return mailResource.SendToMultipleRecipientsAsync(recipients, from, subject, htmlContent, textContent, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); + } + + /// + /// Send an email to a single recipient using a legacy template. + /// + /// The mail resource. + /// To. + /// From. + /// The subject. + /// Content of the HTML. + /// Content of the text. + /// The template identifier. + /// Data to be merged in the content. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendToSingleRecipientAsync( + this IMail mailResource, + MailAddress to, + MailAddress from, + string subject, + string htmlContent, + string textContent, + string templateId, + IEnumerable> substitutions = null, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var recipients = new[] { to }; + return mailResource.SendToMultipleRecipientsAsync(recipients, from, subject, htmlContent, textContent, templateId, substitutions, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); + } + + /// + /// Send an email to a single recipient using a dynamic template. + /// + /// The mail resource. + /// To. + /// From. + /// The identifier of the template. + /// The data to be merged in the content. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendToSingleRecipientAsync( + this IMail mailResource, + MailAddress to, + MailAddress from, + string dynamicTemplateId, + object dynamicData = null, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var recipients = new[] { to }; + return mailResource.SendToMultipleRecipientsAsync(recipients, from, dynamicTemplateId, dynamicData, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); + } + + /// + /// Send an AMP email to a single recipient without using a template (which means you must provide the subject, html content and text content). + /// + /// The mail resource. + /// To. + /// From. + /// The subject. + /// Content of the AMP. + /// Content of the HTML. + /// Content of the text. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This overload is ideal when sending an email without using a template. + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendAmpEmailToSingleRecipientAsync( + this IMail mailResource, + MailAddress to, + MailAddress from, + string subject, + string ampContent, + string htmlContent, + string textContent, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var recipients = new[] { to }; + return mailResource.SendAmpEmailToMultipleRecipientsAsync(recipients, from, subject, ampContent, htmlContent, textContent, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); + } + + /// + /// Send the same email to multiple recipients without using a template (which means you must provide the subject, html content and text content). + /// + /// The mail resource. + /// The recipients. + /// From. + /// The subject. + /// Content of the HTML. + /// Content of the text. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendToMultipleRecipientsAsync( + this IMail mailResource, + IEnumerable recipients, + MailAddress from, + string subject, + string htmlContent, + string textContent, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var personalizations = new[] + { + new MailPersonalization + { + To = recipients.ToArray() + } + }; + + var contents = new List(); + if (!string.IsNullOrEmpty(textContent)) contents.Add(new MailContent("text/plain", textContent)); + if (!string.IsNullOrEmpty(htmlContent)) contents.Add(new MailContent("text/html", htmlContent)); + + var trackingSettings = new TrackingSettings + { + ClickTracking = new ClickTrackingSettings + { + EnabledInHtmlContent = trackClicks, + EnabledInTextContent = trackClicks + }, + OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, + GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, + SubscriptionTracking = subscriptionTracking + }; + + return mailResource.SendAsync(personalizations, subject, contents, from, replyTo, attachments, null, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); + } + + /// + /// Send the same email to multiple recipients using a legacy template. + /// + /// The mail resource. + /// The recipients. + /// From. + /// The subject. + /// Content of the HTML. + /// Content of the text. + /// The template identifier. + /// Data to be merged in the content. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendToMultipleRecipientsAsync( + this IMail mailResource, + IEnumerable recipients, + MailAddress from, + string subject, + string htmlContent, + string textContent, + string templateId, + IEnumerable> substitutions = null, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var personalizations = new[] + { + new MailPersonalization + { + To = recipients.ToArray(), + Substitutions = substitutions?.ToArray() + } + }; + + var contents = new List(); + if (!string.IsNullOrEmpty(textContent)) contents.Add(new MailContent("text/plain", textContent)); + if (!string.IsNullOrEmpty(htmlContent)) contents.Add(new MailContent("text/html", htmlContent)); + + var trackingSettings = new TrackingSettings + { + ClickTracking = new ClickTrackingSettings + { + EnabledInHtmlContent = trackClicks, + EnabledInTextContent = trackClicks + }, + OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, + GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, + SubscriptionTracking = subscriptionTracking + }; + + return mailResource.SendAsync(personalizations, subject, contents, from, replyTo, attachments, templateId, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); + } + + /// + /// Send the same email to multiple recipients. + /// + /// The mail resource. + /// The recipients. + /// From. + /// The identifier of the template. + /// The data to be merged in the content. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendToMultipleRecipientsAsync( + this IMail mailResource, + IEnumerable recipients, + MailAddress from, + string dynamicTemplateId, + object dynamicData = null, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + if (!Template.IsDynamic(dynamicTemplateId)) + { + throw new ArgumentException($"{dynamicTemplateId} is not a valid dynamic template identifier.", nameof(dynamicTemplateId)); + } + + var personalizations = new[] + { + new MailPersonalization + { + To = recipients.ToArray(), + DynamicData = dynamicData + } + }; + + var trackingSettings = new TrackingSettings + { + ClickTracking = new ClickTrackingSettings + { + EnabledInHtmlContent = trackClicks, + EnabledInTextContent = trackClicks + }, + OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, + GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, + SubscriptionTracking = subscriptionTracking + }; + + return mailResource.SendAsync(personalizations, null, null, from, replyTo, attachments, dynamicTemplateId, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); + } + + /// + /// Send the same AMP email to multiple recipients. + /// + /// The mail resource. + /// The recipients. + /// From. + /// The subject. + /// Content of the AMP. + /// Content of the HTML. + /// Content of the text. + /// if set to true [track opens]. + /// if set to true [track clicks]. + /// The subscription tracking. + /// The reply to. + /// The attachments. + /// The sections. + /// The headers. + /// The categories. + /// The custom arguments. + /// The send at. + /// The batch identifier. + /// The unsubscribe options. + /// Name of the ip pool. + /// The mail settings. + /// The priority. + /// The cancellation token. + /// + /// The message id. + /// + /// + /// This is a convenience method with simplified parameters. + /// If you need more options, use the method. + /// + /// Too many recipients. + /// Email exceeds the size limit. + public static Task SendAmpEmailToMultipleRecipientsAsync( + this IMail mailResource, + IEnumerable recipients, + MailAddress from, + string subject, + string ampContent, + string htmlContent, + string textContent, + bool trackOpens = true, + bool trackClicks = true, + SubscriptionTrackingSettings subscriptionTracking = null, + MailAddress replyTo = null, + IEnumerable attachments = null, + IEnumerable> sections = null, + IEnumerable> headers = null, + IEnumerable categories = null, + IEnumerable> customArgs = null, + DateTime? sendAt = null, + string batchId = null, + UnsubscribeOptions unsubscribeOptions = null, + string ipPoolName = null, + MailSettings mailSettings = null, + MailPriority priority = MailPriority.Normal, + CancellationToken cancellationToken = default) + { + var personalizations = new[] + { + new MailPersonalization + { + To = recipients.ToArray() + } + }; + + var contents = new List(); + if (!string.IsNullOrEmpty(textContent)) contents.Add(new MailContent("text/plain", textContent)); + if (!string.IsNullOrEmpty(ampContent)) contents.Add(new MailContent("text/x-amp-html", ampContent)); + if (!string.IsNullOrEmpty(htmlContent)) contents.Add(new MailContent("text/html", htmlContent)); + + var trackingSettings = new TrackingSettings + { + ClickTracking = new ClickTrackingSettings + { + EnabledInHtmlContent = trackClicks, + EnabledInTextContent = trackClicks + }, + OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, + GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, + SubscriptionTracking = subscriptionTracking + }; + + return mailResource.SendAsync(personalizations, subject, contents, from, replyTo, attachments, null, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); + } + /// /// Get all of the details about the messages matching the criteria. /// diff --git a/Source/StrongGrid/Resources/IMail.cs b/Source/StrongGrid/Resources/IMail.cs index fc93766c..3d111918 100644 --- a/Source/StrongGrid/Resources/IMail.cs +++ b/Source/StrongGrid/Resources/IMail.cs @@ -14,464 +14,6 @@ namespace StrongGrid.Resources /// public interface IMail { - /// - /// Send an email to a single recipient without using a template (which means you must provide the subject, html content and text content). - /// - /// To. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This overload is ideal when sending an email without using a template. - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string subject, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send an email to a single recipient using a legacy template. - /// - /// To. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// The template identifier. - /// Data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string subject, - string htmlContent, - string textContent, - string templateId, - IEnumerable> substitutions = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send an email to a single recipient using a dynamic template. - /// - /// To. - /// From. - /// The identifier of the template. - /// The data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string dynamicTemplateId, - object dynamicData = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send an AMP email to a single recipient without using a template (which means you must provide the subject, html content and text content). - /// - /// To. - /// From. - /// The subject. - /// Content of the AMP. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This overload is ideal when sending an email without using a template. - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendAmpEmailToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string subject, - string ampContent, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send the same email to multiple recipients without using a template (which means you must provide the subject, html content and text content). - /// - /// The recipients. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string subject, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send the same email to multiple recipients using a legacy template. - /// - /// The recipients. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// The template identifier. - /// Data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string subject, - string htmlContent, - string textContent, - string templateId, - IEnumerable> substitutions = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send the same email to multiple recipients. - /// - /// The recipients. - /// From. - /// The identifier of the template. - /// The data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string dynamicTemplateId, - object dynamicData = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - - /// - /// Send the same AMP email to multiple recipients without using a template (which means you must provide the subject, html content and text content). - /// - /// The recipients. - /// From. - /// The subject. - /// Content of the AMP. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - Task SendAmpEmailToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string subject, - string ampContent, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default); - /// /// Send email(s) over SendGrid’s v3 Web API. /// diff --git a/Source/StrongGrid/Resources/Mail.cs b/Source/StrongGrid/Resources/Mail.cs index b833fbc6..82feacf7 100644 --- a/Source/StrongGrid/Resources/Mail.cs +++ b/Source/StrongGrid/Resources/Mail.cs @@ -35,592 +35,6 @@ internal Mail(Pathoschild.Http.Client.IClient client) _client = client; } - /// - /// Send an email to a single recipient without using a template (which means you must provide the subject, html content and text content). - /// - /// To. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This overload is ideal when sending an email without using a template. - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string subject, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var recipients = new[] { to }; - return SendToMultipleRecipientsAsync(recipients, from, subject, htmlContent, textContent, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); - } - - /// - /// Send an email to a single recipient using a legacy template. - /// - /// To. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// The template identifier. - /// Data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string subject, - string htmlContent, - string textContent, - string templateId, - IEnumerable> substitutions = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var recipients = new[] { to }; - return SendToMultipleRecipientsAsync(recipients, from, subject, htmlContent, textContent, templateId, substitutions, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); - } - - /// - /// Send an email to a single recipient using a dynamic template. - /// - /// To. - /// From. - /// The identifier of the template. - /// The data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string dynamicTemplateId, - object dynamicData = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var recipients = new[] { to }; - return SendToMultipleRecipientsAsync(recipients, from, dynamicTemplateId, dynamicData, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); - } - - /// - /// Send an AMP email to a single recipient without using a template (which means you must provide the subject, html content and text content). - /// - /// To. - /// From. - /// The subject. - /// Content of the AMP. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This overload is ideal when sending an email without using a template. - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendAmpEmailToSingleRecipientAsync( - MailAddress to, - MailAddress from, - string subject, - string ampContent, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var recipients = new[] { to }; - return SendAmpEmailToMultipleRecipientsAsync(recipients, from, subject, ampContent, htmlContent, textContent, trackOpens, trackClicks, subscriptionTracking, replyTo, attachments, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, priority, cancellationToken); - } - - /// - /// Send the same email to multiple recipients without using a template (which means you must provide the subject, html content and text content). - /// - /// The recipients. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string subject, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var personalizations = new[] - { - new MailPersonalization - { - To = recipients.ToArray() - } - }; - - var contents = new List(); - if (!string.IsNullOrEmpty(textContent)) contents.Add(new MailContent("text/plain", textContent)); - if (!string.IsNullOrEmpty(htmlContent)) contents.Add(new MailContent("text/html", htmlContent)); - - var trackingSettings = new TrackingSettings - { - ClickTracking = new ClickTrackingSettings - { - EnabledInHtmlContent = trackClicks, - EnabledInTextContent = trackClicks - }, - OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, - GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, - SubscriptionTracking = subscriptionTracking - }; - - return SendAsync(personalizations, subject, contents, from, replyTo, attachments, null, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); - } - - /// - /// Send the same email to multiple recipients using a legacy template. - /// - /// The recipients. - /// From. - /// The subject. - /// Content of the HTML. - /// Content of the text. - /// The template identifier. - /// Data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string subject, - string htmlContent, - string textContent, - string templateId, - IEnumerable> substitutions = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var personalizations = new[] - { - new MailPersonalization - { - To = recipients.ToArray(), - Substitutions = substitutions?.ToArray() - } - }; - - var contents = new List(); - if (!string.IsNullOrEmpty(textContent)) contents.Add(new MailContent("text/plain", textContent)); - if (!string.IsNullOrEmpty(htmlContent)) contents.Add(new MailContent("text/html", htmlContent)); - - var trackingSettings = new TrackingSettings - { - ClickTracking = new ClickTrackingSettings - { - EnabledInHtmlContent = trackClicks, - EnabledInTextContent = trackClicks - }, - OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, - GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, - SubscriptionTracking = subscriptionTracking - }; - - return SendAsync(personalizations, subject, contents, from, replyTo, attachments, templateId, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); - } - - /// - /// Send the same email to multiple recipients. - /// - /// The recipients. - /// From. - /// The identifier of the template. - /// The data to be merged in the content. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string dynamicTemplateId, - object dynamicData = null, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - if (!Template.IsDynamic(dynamicTemplateId)) - { - throw new ArgumentException($"{dynamicTemplateId} is not a valid dynamic template identifier.", nameof(dynamicTemplateId)); - } - - var personalizations = new[] - { - new MailPersonalization - { - To = recipients.ToArray(), - DynamicData = dynamicData - } - }; - - var trackingSettings = new TrackingSettings - { - ClickTracking = new ClickTrackingSettings - { - EnabledInHtmlContent = trackClicks, - EnabledInTextContent = trackClicks - }, - OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, - GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, - SubscriptionTracking = subscriptionTracking - }; - - return SendAsync(personalizations, null, null, from, replyTo, attachments, dynamicTemplateId, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); - } - - /// - /// Send the same AMP email to multiple recipients. - /// - /// The recipients. - /// From. - /// The subject. - /// Content of the AMP. - /// Content of the HTML. - /// Content of the text. - /// if set to true [track opens]. - /// if set to true [track clicks]. - /// The subscription tracking. - /// The reply to. - /// The attachments. - /// The sections. - /// The headers. - /// The categories. - /// The custom arguments. - /// The send at. - /// The batch identifier. - /// The unsubscribe options. - /// Name of the ip pool. - /// The mail settings. - /// The priority. - /// The cancellation token. - /// - /// The message id. - /// - /// - /// This is a convenience method with simplified parameters. - /// If you need more options, use the method. - /// - /// Too many recipients. - /// Email exceeds the size limit. - public Task SendAmpEmailToMultipleRecipientsAsync( - IEnumerable recipients, - MailAddress from, - string subject, - string ampContent, - string htmlContent, - string textContent, - bool trackOpens = true, - bool trackClicks = true, - SubscriptionTrackingSettings subscriptionTracking = null, - MailAddress replyTo = null, - IEnumerable attachments = null, - IEnumerable> sections = null, - IEnumerable> headers = null, - IEnumerable categories = null, - IEnumerable> customArgs = null, - DateTime? sendAt = null, - string batchId = null, - UnsubscribeOptions unsubscribeOptions = null, - string ipPoolName = null, - MailSettings mailSettings = null, - MailPriority priority = MailPriority.Normal, - CancellationToken cancellationToken = default) - { - var personalizations = new[] - { - new MailPersonalization - { - To = recipients.ToArray() - } - }; - - var contents = new List(); - if (!string.IsNullOrEmpty(textContent)) contents.Add(new MailContent("text/plain", textContent)); - if (!string.IsNullOrEmpty(ampContent)) contents.Add(new MailContent("text/x-amp-html", ampContent)); - if (!string.IsNullOrEmpty(htmlContent)) contents.Add(new MailContent("text/html", htmlContent)); - - var trackingSettings = new TrackingSettings - { - ClickTracking = new ClickTrackingSettings - { - EnabledInHtmlContent = trackClicks, - EnabledInTextContent = trackClicks - }, - OpenTracking = new OpenTrackingSettings { Enabled = trackOpens }, - GoogleAnalytics = new GoogleAnalyticsSettings { Enabled = false }, - SubscriptionTracking = subscriptionTracking - }; - - return SendAsync(personalizations, subject, contents, from, replyTo, attachments, null, sections, headers, categories, customArgs, sendAt, batchId, unsubscribeOptions, ipPoolName, mailSettings, trackingSettings, priority, cancellationToken); - } - /// /// Send email(s) over SendGrid’s v3 Web API. /// @@ -724,7 +138,7 @@ public async Task SendAsync( var numberOfRecipients = personalizationsCopy.Sum(p => p.To?.Count(r => r != null) ?? 0); numberOfRecipients += personalizationsCopy.Sum(p => p.Cc?.Count(r => r != null) ?? 0); numberOfRecipients += personalizationsCopy.Sum(p => p.Bcc?.Count(r => r != null) ?? 0); - if (numberOfRecipients >= 1000) throw new ArgumentOutOfRangeException("The total number of recipients must be less than 1000"); + if (numberOfRecipients >= 1000) throw new ArgumentOutOfRangeException(nameof(numberOfRecipients), numberOfRecipients, "The total number of recipients must be less than 1000"); // SendGrid throws an unhelpful error when the Bcc email address is an empty string if (mailSettings?.Bcc != null && string.IsNullOrWhiteSpace(mailSettings.Bcc.EmailAddress))