Hello World!
"; -myMessage.Text = "Hello World plain text!"; -``` - -#How to: Send an Email - -After creating an email message, you can send it using the Web API provided by SendGrid. - -Sending email requires that you supply your SendGrid account credentials (username and password) OR a SendGrid API Key. API Key is the preferred method. API Keys are in beta. To configure API keys, visit https://sendgrid.com/beta/settings/api_keys - -To send an email message, use the **DeliverAsync** method on the **Web** transport class, which calls the SendGrid Web API. The following example shows how to send a message. - -```csharp -// Create the email object first, then add the properties. -SendGridMessage myMessage = new SendGridMessage(); -myMessage.AddTo("anna@example.com"); -myMessage.From = new MailAddress("john@example.com", "John Smith"); -myMessage.Subject = "Testing the SendGrid Library"; -myMessage.Text = "Hello World!"; - -// Create a Web transport, using API Key -var transportWeb = new Web("This string is a SendGrid API key"); - -// Send the email. -transportWeb.DeliverAsync(myMessage); -// NOTE: If your developing a Console Application, use the following so that the API call has time to complete -// transportWeb.DeliverAsync(myMessage).Wait(); -``` - -#How to: Add an Attachment +## Dependencies -Attachments can be added to a message by calling the **AddAttachment** method and specifying the name and path of the file you want to attach, or by passing a stream. You can include multiple attachments by calling this method once for each file you wish to attach. The following example demonstrates adding an attachment to a message: +- The SendGrid Service, starting at the [free level](https://sendgrid.com/free?source=sendgrid-csharp)) +- [SendGrid.CSharp.HTTP.Client](https://github.com/sendgrid/csharp-http-client) -```csharp -SendGridMessage myMessage = new SendGridMessage(); -myMessage.AddTo("anna@example.com"); -myMessage.From = new MailAddress("john@example.com", "John Smith"); -myMessage.Subject = "Testing the SendGrid Library"; -myMessage.Text = "Hello World!"; +# Quick Start -myMessage.AddAttachment(@"C:\file1.txt"); -``` - -You can also add attachments from the data's **Stream**. It can be done by calling the same method as above, **AddAttachment**, but by passing in the Stream of the data, and the filename you want it to show as in the message. +## Hello Email ```csharp -SendGridMessage myMessage = new SendGridMessage(); -myMessage.AddTo("anna@example.com"); -myMessage.From = new MailAddress("john@example.com", "John Smith"); -myMessage.Subject = "Testing the SendGrid Library"; -myMessage.Text = "Hello World!"; +using System; +using SendGrid; -using (var attachmentFileStream = new FileStream(@"C:\file.txt", FileMode.Open)) +namespace Example { - message.AddAttachment(attachmentFileStream, "My Cool File.txt"); + internal class Example + { + private static void Main() + { + String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); + dynamic sg = new SendGrid.SendGridAPIClient(apiKey); + + Email from = new Email("test@example.com"); + String subject = "Hello World from the SendGrid CSharp Library"; + Email to = new Email("test@example.com"); + Content content = new Content("text/plain", "Textual content"); + Mail mail = new Mail(from, subject, to, content); + + dynamic response = sg.client.mail.send.post(requestBody: mail.Get()); + } + } } ``` -#How to: Use filters to enable footers, tracking, analytics and templates - -SendGrid provides additional email functionality through the use of filters. These are settings that can be added to an email message to enable specific functionality such as click tracking, Google analytics, subscription tracking, and so on. For a full list of filters, see [Filter Settings](https://sendgrid.com/docs/API_Reference/SMTP_API/apps.html). - -Filters can be applied to **SendGrid** email messages using methods implemented as part of the **SendGrid** class. Before you can enable filters on an email message, you must first initialize the list of available filters by calling the **InitializeFilters** method. - -The following examples demonstrate the footer and click tracking filters: - -##Footer -```csharp -// Create the email object first, then add the properties. -SendGridMessage myMessage = new SendGridMessage(); -myMessage.AddTo("anna@example.com"); -myMessage.From = new MailAddress("john@example.com", "John Smith"); -myMessage.Subject = "Testing the SendGrid Library"; -myMessage.Text = "Hello World!"; - -// Add a footer to the message. -myMessage.EnableFooter("PLAIN TEXT FOOTER", "HTML FOOTER
"); -``` - -##Click tracking -```csharp -// Create the email object first, then add the properties. -SendGridMessage myMessage = new SendGridMessage(); -myMessage.AddTo("anna@example.com"); -myMessage.From = new MailAddress("john@example.com", "John Smith"); -myMessage.Subject = "Testing the SendGrid Library"; -myMessage.Html = ""; -myMessage.Text = "Hello World!"; - -// true indicates that links in plain text portions of the email -// should also be overwritten for link tracking purposes. -myMessage.EnableClickTracking(true); -``` - -##Template -```csharp -// Create the email object first, then add the properties. -SendGridMessage myMessage = new SendGridMessage(); -myMessage.AddTo("anna@example.com"); -myMessage.From = new MailAddress("john@example.com", "John Smith"); -myMessage.Subject = "Testing the SendGrid Library"; -myMessage.Text = "Hello World!"; - -// Enable template engine, you must send the template id - myMessage.EnableTemplateEngine("template id"); -``` - -#How to: Use the [Web API v3](https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html) - -Note: We have just begun to implement support for these endpoints and therefore only the following endpoints are currently supported. This functionality is located in the "SendGrid" project. - -## API Keys ## - -Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html) for further details. - -List all API Keys belonging to the authenticated user [GET] - -```csharp -String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); -var client = new SendGrid.Client(apiKey); -// Leave off .Result for an asyncronous call -HttpResponseMessage responseGet = client.ApiKeys.Get().Result; -``` - -Generate a new API Key for the authenticated user [POST] - -```csharp -String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); -var client = new SendGrid.Client(apiKey); -var apiKeyName = "CSharpTestKey"; -// Leave off .Result for an asyncronous call -HttpResponseMessage responsePost = client.ApiKeys.Post(apiKeyName).Result; -``` - -Update the name of an existing API Key [PATCH] - -```csharp -String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User); -var client = new SendGrid.Client(apiKey); -var apiKeyName = "CSharpTestKey"; -var apiKeyId = "hello, <% name %>
"; - var escHtml = "hello, <% name %><\\/p><\\/body>";
-
- sendgrid.EnableFooter(text, html);
-
- var json = header.JsonString();
- Assert.AreEqual(
- "{\"filters\" : {\"footer\" : {\"settings\" : {\"enable\" : \"1\",\"text\\/plain\" : \"" + text +
- "\",\"text\\/html\" : \"" + escHtml + "\"}}}}", json);
- }
-
- [Test]
- public void EnableGoogleAnalytics()
- {
- var header = new Header();
- var sendgrid = new SendGridMessage(header);
-
- var source = "SomeDomain.com";
- var medium = "Email";
- var term = "keyword1, keyword2, keyword3";
- var content = "PG, PG13";
- var campaign = "my_campaign";
-
- sendgrid.EnableGoogleAnalytics(source, medium, term, content, campaign);
-
- var jsonSource = "\"utm_source\" : \"SomeDomain.com\"";
- var jsonMedium = "\"utm_medium\" : \"" + medium + "\"";
- var jsonTerm = "\"utm_term\" : \"" + term + "\"";
- var jsonContent = "\"utm_content\" : \"" + content + "\"";
- var jsonCampaign = "\"utm_campaign\" : \"" + campaign + "\"";
-
- var json = header.JsonString();
- Assert.AreEqual("{\"filters\" : {\"ganalytics\" : {\"settings\" : {\"enable\" : \"1\"," +
- jsonSource + "," + jsonMedium + "," + jsonTerm + "," + jsonContent + "," + jsonCampaign +
- "}}}}",
- json);
- }
-
- [Test]
- public void EnableGravatar()
- {
- var header = new Header();
- var sendgrid = new SendGridMessage(header);
-
- sendgrid.EnableGravatar();
-
- var json = header.JsonString();
- Assert.AreEqual("{\"filters\" : {\"gravatar\" : {\"settings\" : {\"enable\" : \"1\"}}}}", json);
- }
-
- [Test]
- public void EnableOpenTracking()
- {
- var header = new Header();
- var sendgrid = new SendGridMessage(header);
-
- sendgrid.EnableOpenTracking();
-
- var json = header.JsonString();
- Assert.AreEqual("{\"filters\" : {\"opentrack\" : {\"settings\" : {\"enable\" : \"1\"}}}}", json);
- }
-
- [Test]
- public void EnableSpamCheck()
- {
- var header = new Header();
- var sendgrid = new SendGridMessage(header);
-
- var score = 5;
- var url = "http://www.example.com";
- sendgrid.EnableSpamCheck(score, url);
-
- var json = header.JsonString();
- Assert.AreEqual(
- "{\"filters\" : {\"spamcheck\" : {\"settings\" : {\"enable\" : \"1\",\"maxscore\" : \"5\",\"url\" : \"http:\\/\\/www.example.com\"}}}}",
- json);
- }
-
- [Test]
- public void EnableTemplate()
- {
- var header = new Header();
- var sendgrid = new SendGridMessage(header);
- var html = "<% body %>";
-
- var escHtml = "<% body %>";
- sendgrid.EnableTemplate(html);
-
- var json = header.JsonString();
- Assert.AreEqual(
- "{\"filters\" : {\"template\" : {\"settings\" : {\"enable\" : \"1\",\"text\\/html\" : \"" + escHtml +
- "\"}}}}", json);
-
- escHtml = "bad";
- Assert.Throws Test HTML Body Check out our spring line! Check out our summer line!