public void SendMergeMessageWithUnlimitedRecipientsUsingHelper(System.Net.Mail.MailAddress[] addresses) { int totalAllowedRecipientsPerMessage = 50; // Construct the object used to generate JSON for the POST request. // The you can add any merge field you like if you are using API templates. var postBody = new PostBody { ServerId = _serverId, ApiKey = _apiKey, Messages = new[] { new EmailMessage { Subject = "%%Subject%%", To = new[] { new Address { EmailAddress = "%%DeliveryAddress%%", FriendlyName = "%%FriendlyName%%" } }, From = new Address { EmailAddress = "%%FromEmail%%", FriendlyName = "%%FromName%%" }, TextBody = "%%TextBody%%", HtmlBody = "%%HtmlBody%%", } } }; var bulkRecipientData = new BulkRecipientHelper(); bulkRecipientData.AddGlobalMergeField("Subject", "Email subject line for SDK example."); bulkRecipientData.AddGlobalMergeField("TextBody", "The text portion of the message. Using Merge %%CustomField%%."); bulkRecipientData.AddGlobalMergeField("HtmlBody", "<h1>The HTML portion of the message</h1><br/><p>A paragraph using Merge %%CustomField%%..</p>"); bulkRecipientData.AddGlobalMergeField("FromName", "Example Name"); bulkRecipientData.AddGlobalMergeField("FromEmail", "*****@*****.**"); int reciepientsForThisMessage = 0; foreach (var recipient in addresses) { bulkRecipientData.AddRecipient(recipient.Address, recipient.DisplayName); reciepientsForThisMessage++; if (reciepientsForThisMessage == totalAllowedRecipientsPerMessage) { postBody.Messages[0].MergeData = bulkRecipientData.GetMergeData(); SendMEssage(postBody); reciepientsForThisMessage = 0; bulkRecipientData.ClearRecipients(); } } //Recipients that didn't send yet? if (bulkRecipientData.RecipientCount > 0) { postBody.Messages[0].MergeData = bulkRecipientData.GetMergeData(); SendMEssage(postBody); } }
public void SendMergeMessageWithMultipleRecipientsUsingHelper() { // Construct the object used to generate JSON for the POST request. // The you can add any merge field you like if you are using API templates. var postBody = new PostBody { ServerId = _serverId, ApiKey = _apiKey, Messages = new[] { new EmailMessage { Subject = "%%Subject%%", To = new[] { new Address { EmailAddress = "%%DeliveryAddress%%", FriendlyName = "%%FriendlyName%%" } }, From = new Address { EmailAddress = "%%FromEmail%%", FriendlyName = "%%FromName%%" }, TextBody = "%%TextBody%%", HtmlBody = "%%HtmlBody%%", } } }; var bulkRecipientData = new BulkRecipientHelper(); bulkRecipientData.AddGlobalMergeField("Subject", "Email subject line for SDK example."); bulkRecipientData.AddGlobalMergeField("TextBody", "The text portion of the message. Using Merge %%CustomField%%."); bulkRecipientData.AddGlobalMergeField("HtmlBody", "<h1>The HTML portion of the message</h1><br/><p>A paragraph using Merge %%CustomField%%..</p>"); bulkRecipientData.AddGlobalMergeField("FromName", "Example Name"); bulkRecipientData.AddGlobalMergeField("FromEmail", "*****@*****.**"); bulkRecipientData.AddRecipient("*****@*****.**", "recipient 1"); bulkRecipientData.AddCustomFieldToRecipient("*****@*****.**", "CustomField", "Example 1"); bulkRecipientData.AddRecipient("*****@*****.**", "recipient 2"); bulkRecipientData.AddCustomFieldToRecipient("*****@*****.**", "CustomField", "Example 2"); bulkRecipientData.AddRecipient("*****@*****.**", "recipient 3"); bulkRecipientData.AddCustomFieldToRecipient("*****@*****.**", "CustomField", "Example 3"); postBody.Messages[0].MergeData = bulkRecipientData.GetMergeData(); try { var httpPostServer = new HttpPostService(); var response = httpPostServer.PostAndGetResponse <PostResponse>(postBody, "https://inject.socketlabs.com/api/v1/email", null); // Display the results. if (response.ErrorCode.Equals("Success")) { Console.WriteLine("Successful injection!"); } else if (response.ErrorCode.Equals("Warning")) { Console.WriteLine("Warning, not all recipients/messages were sent."); foreach (var result in response.MessageResults) { Console.WriteLine($"Message #{result.Index} {result.ErrorCode}"); foreach (var address in result.AddressResults) { Console.WriteLine($"{address.EmailAddress} {address.ErrorCode} Sent: {address.Accepted}"); } } } else { Console.WriteLine("Failed injection!"); Console.WriteLine(response.ErrorCode); } } catch (Exception ex) { Console.WriteLine("Error, something bad happened: " + ex.Message); } }