Created
February 4, 2016 21:51
-
-
Save thinkingserious/90ae60a837ace83cfe57 to your computer and use it in GitHub Desktop.
Testing BCC and unique args
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net.Mail; | |
using System.Linq; | |
using SendGrid; | |
namespace Example | |
{ | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
try | |
{ | |
/* MAIL SETTINGS | |
* Fill in the relevant information below. | |
* ===================================================*/ | |
// From | |
string fromAddress = "[email protected]"; | |
// To | |
string recipients = "[email protected]"; | |
// Subject | |
string subject = "Test Arg"; | |
// Text Body | |
string text = "Hello,\n\nThis is a test message from SendGrid. We have sent this to you because you requested a test message be sent from your account.\n\nThis is a link to google.com: http://www.google.com\nThis is a link to apple.com: http://www.apple.com\nThis is a link to sendgrid.com: http://www.sendgrid.com\n\nThank you for reading this test message.\n\nLove,\nYour friends at SendGrid"; | |
// HTML Body | |
string html = "<table style=\"border: solid 1px #000; background-color: #666; font-family: verdana, tahoma, sans-serif; color: #fff;\"> <tr> <td> <h2>Hello,</h2> <p>This is a test message from SendGrid. We have sent this to you because you requested a test message be sent from your account.</p> <a href=\"http://www.google.com\" target=\"_blank\">This is a link to google.com</a> <p> <a href=\"http://www.apple.com\" target=\"_blank\">This is a link to apple.com</a> <p> <a href=\"http://www.sendgrid.com\" target=\"_blank\">This is a link to sendgrid.com</a> </p> <p>Thank you for reading this test message.</p> Love,<br/> Your friends at SendGrid</p> <p> <img src=\"http://cdn1.sendgrid.com/images/sendgrid-logo.png\" alt=\"SendGrid!\" /> </td> </tr> </table>"; | |
/* CREATE THE MAIL MESSAGE | |
* ===================================================*/ | |
var myMessage = new SendGridMessage(); | |
myMessage.AddTo(recipients); | |
myMessage.From = new MailAddress(fromAddress); | |
myMessage.Subject = subject; | |
myMessage.Text = text; | |
myMessage.Html = html; | |
/* SMTP API | |
* ===================================================*/ | |
// Recipients | |
myMessage.Header.SetTo(new List<String> | |
{ | |
"[email protected]" | |
}); | |
// Unique Arguments | |
Dictionary<string, dynamic> values = new Dictionary<string, dynamic>() | |
{ | |
{ | |
"ABC", "123" | |
}, | |
{ | |
"XYZ", "789" | |
} | |
}; | |
Dictionary<string, string> args = values.ToDictionary(k => (string)k.Key, k => (string)Convert.ToString(k.Value)); | |
myMessage.AddUniqueArgs(args); | |
// App Filters | |
// **NOTE** There are convenience methods built in | |
// to the library (i.e. EnableClickTracking()). For | |
// the sake of quickly generating this example, | |
// we're just going to manually add each app filter. | |
// For more info about the available methods, see the | |
// GitHub page. | |
var filters = new Dictionary<string, dynamic>() | |
{ | |
{ | |
"bcc", new Dictionary<string, dynamic>() | |
{ | |
{ | |
"settings", new Dictionary<string, dynamic>() | |
{ | |
{ | |
"enable", "1" | |
}, | |
{ | |
"email", "[email protected]" | |
} | |
} | |
} | |
} | |
} | |
}; | |
foreach (var filter in filters.Keys) | |
{ | |
var settings = filters[filter]["settings"]; | |
foreach (var setting in settings.Keys) | |
{ | |
myMessage.Header.AddFilterSetting(filter, new List<string> { setting }, Convert.ToString(settings[setting])); | |
} | |
} | |
/* SEND THE MESSAGE | |
* ===================================================*/ | |
string apikey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY"); | |
// Create a Web transport for sending email. | |
var transportWeb = new SendGrid.Web(apikey); | |
// Send the email. | |
transportWeb.DeliverAsync(myMessage).Wait(); | |
Console.WriteLine("Email sent to " + myMessage.To.GetValue(0)); | |
Console.WriteLine("\n\nPress any key to continue."); | |
Console.ReadKey(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment