Skip to content

Commit 9c18374

Browse files
Merge pull request #161 from sendgrid/v3_beta
V3 beta
2 parents 8894887 + 61aa4f3 commit 9c18374

File tree

24 files changed

+875
-68
lines changed

24 files changed

+875
-68
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ SendGrid/SendGrid.userprefs
1818
SendGrid/packages/
1919
SendGrid/TestResult.xml
2020
SendGrid/SendGrid.sln.VisualState.xml
21+
22+
*.snk

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
language: csharp
22
solution: SendGrid/SendGrid.sln
3+
env:
4+
global:
5+
- secure: "S6rHgh05/IRe/UjhObIK/IX4O2B9dXgvpKln2bM1trJcZsO1uRN74UjQ22ExKQBpAfKwxG9yNOdomxoIn1/WhwQfmwSKrnegN86l09aNzKSSdoK7wt1pfJz952/FO/a4jV/e1p3E5g2hEpopG+KLmaipwE+qmlQrEaZUgZLx/mI="
36
install:
47
- nuget restore SendGrid/SendGrid.sln
58
- nuget install NUnit.Runners -Version 2.6.4 -OutputDirectory testrunner
69
script:
710
- xbuild /p:Configuration=BuildNet45 SendGrid/SendGrid.sln
811
- mono ./testrunner/NUnit.Runners.2.6.4/tools/nunit-console.exe SendGrid/Tests/bin/BuildNet45/Tests.dll
12+
- mono ./testrunner/NUnit.Runners.2.6.4/tools/nunit-console.exe SendGrid/UnitTest/bin/Release/UnitTest.dll
913
notifications:
1014
hipchat:
1115
rooms:

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,62 @@ myMessage.Text = "Hello World!";
159159
// should also be overwritten for link tracking purposes.
160160
myMessage.EnableClickTracking(true);
161161
```
162+
163+
#How to: Use the [Web API v3](https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html)
164+
165+
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.
166+
167+
## API Keys ##
168+
169+
Please refer to [our documentation](https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/index.html) for further details.
170+
171+
List all API Keys belonging to the authenticated user [GET]
172+
173+
```csharp
174+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
175+
var client = new SendGrid.Client(apiKey);
176+
// Leave off .Result for an asyncronous call
177+
HttpResponseMessage responseGet = client.ApiKeys.Get().Result;
178+
```
179+
180+
Generate a new API Key for the authenticated user [POST]
181+
182+
```csharp
183+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
184+
var client = new SendGrid.Client(apiKey);
185+
var apiKeyName = "CSharpTestKey";
186+
// Leave off .Result for an asyncronous call
187+
HttpResponseMessage responsePost = client.ApiKeys.Post(apiKeyName).Result;
188+
```
189+
190+
Update the name of an existing API Key [PATCH]
191+
192+
```csharp
193+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
194+
var client = new SendGrid.Client(apiKey);
195+
var apiKeyName = "CSharpTestKey";
196+
ver apiKeyId = "<API Key ID>";
197+
// Leave off .Result for an asyncronous call
198+
HttpResponseMessage responsePatch = client.ApiKeys.Patch(apiKeyId, apiKeyName).Result;
199+
```
200+
201+
Revoke an existing API Key [DELETE]
202+
203+
```csharp
204+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
205+
var client = new SendGrid.Client(apiKey);
206+
ver apiKeyId = "<API Key ID>";
207+
// Leave off .Result for an asyncronous call
208+
HttpResponseMessage responseDelete = client.ApiKeys.Delete(apiKeyId).Result;
209+
```
210+
211+
#How to: Testing
212+
213+
* Load the solution (We have tested using the Visual Studio Community Edition)
214+
* In the Test Explorer, click "Run All". Tests for the Mail Send v2 endpoint are in the "Tests" project, while the tests for the v3 endpoints are in the "UnitTests" project. Selecting "Run All" from the Test Explorer will run the tests in both projects.
215+
216+
You can also test the code by building and running our "Example" project. It will run through the examples using an interactive console. You will need your API key to run the examples against your account.
217+
162218
[SendGrid Documentation](http://www.sendgrid.com/docs)
163219

164220
This readme adapted from [How to Send Email Using SendGrid with Windows Azure](http://www.windowsazure.com/en-us/develop/net/how-to-guides/sendgrid-email-service/)

SendGrid/Example/Example.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,20 @@
4545
</DefineConstants>
4646
</PropertyGroup>
4747
<ItemGroup>
48+
<Reference Include="Microsoft.CSharp" />
49+
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
51+
<Private>True</Private>
52+
</Reference>
4853
<Reference Include="SendGrid.SmtpApi, Version=1.3.1.0, Culture=neutral, PublicKeyToken=2ae73662c35d80e4, processorArchitecture=MSIL">
4954
<SpecificVersion>False</SpecificVersion>
5055
<HintPath>..\packages\SendGrid.SmtpApi.1.3.1\lib\net40\SendGrid.SmtpApi.dll</HintPath>
5156
</Reference>
5257
<Reference Include="System" />
58+
<Reference Include="System.Data.DataSetExtensions" />
5359
<Reference Include="System.Net" />
60+
<Reference Include="System.Net.Http" />
61+
<Reference Include="System.Web.Extensions" />
5462
</ItemGroup>
5563
<ItemGroup>
5664
<Compile Include="Program.cs" />
@@ -71,6 +79,11 @@
7179
<Project>{3c687bef-ff50-44ad-8315-2d4237281af8}</Project>
7280
<Name>Mail</Name>
7381
</ProjectReference>
82+
<ProjectReference Include="..\SendGrid\SendGrid.csproj">
83+
<Project>{1c318867-440b-4eb9-99e3-c0cc2c556962}</Project>
84+
<Name>SendGrid</Name>
85+
<Aliases>global</Aliases>
86+
</ProjectReference>
7487
</ItemGroup>
7588
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7689
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

SendGrid/Example/Program.cs

Lines changed: 89 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,107 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net;
4+
using System.Net.Http;
45
using System.Net.Mail;
5-
using SendGrid;
6+
using Newtonsoft.Json.Linq;
67

78
namespace Example
89
{
9-
internal class Program
10-
{
11-
// this code is used for the SMTPAPI examples
12-
private static void Main()
13-
{
14-
// Create the email object first, then add the properties.
15-
var myMessage = new SendGridMessage();
16-
myMessage.AddTo("[email protected]");
17-
myMessage.From = new MailAddress("[email protected]", "John Smith");
18-
myMessage.Subject = "Testing the SendGrid Library";
19-
myMessage.Text = "Hello World! %tag%";
20-
21-
var subs = new List<String> { "私は%type%ラーメンが大好き" };
22-
myMessage.AddSubstitution("%tag%",subs);
23-
myMessage.AddSection("%type%", "とんこつ");
24-
25-
SendAsync(myMessage);
26-
27-
Console.ReadLine();
28-
}
29-
30-
private static async void SendAsync(SendGridMessage message)
31-
{
32-
// Create credentials, specifying your user name and password.
33-
var credentials = new NetworkCredential("username", "password");
10+
internal class Program
11+
{
12+
private static void Main()
13+
{
14+
// Test sending email
15+
var to = "[email protected]";
16+
var from = "[email protected]";
17+
var fromName = "Jane Doe";
18+
SendEmail(to, from, fromName);
19+
// Test viewing, creating, modifying and deleting API keys through our v3 Web API
20+
ApiKeys();
21+
}
22+
23+
private static void SendAsync(SendGrid.SendGridMessage message)
24+
{
25+
// Create credentials, specifying your user Name and password.
26+
var username = Environment.GetEnvironmentVariable("SENDGRID_USERNAME");
27+
var password = Environment.GetEnvironmentVariable("SENDGRID_PASSWORD");
28+
//string apikey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY");
29+
var credentials = new NetworkCredential(username, password);
3430

3531
// Create a Web transport for sending email.
36-
var transportWeb = new Web(credentials);
32+
var transportWeb = new SendGrid.Web(credentials);
33+
//var transportWeb2 = new SendGrid.Web(apikey);
3734

3835
// Send the email.
3936
try
4037
{
41-
await transportWeb.DeliverAsync(message);
42-
Console.WriteLine("Sent!");
38+
transportWeb.DeliverAsync(message).Wait();
39+
Console.WriteLine("Email sent to " + message.To.GetValue(0));
40+
Console.WriteLine("Press any key to continue.");
41+
Console.ReadKey();
4342
}
4443
catch (Exception ex)
4544
{
4645
Console.WriteLine(ex.Message);
46+
Console.WriteLine("Press any key to continue.");
47+
Console.ReadKey();
4748
}
48-
}
49-
}
50-
}
49+
}
50+
51+
private static void SendEmail(string to, string from, string fromName)
52+
{
53+
// Create the email object first, then add the properties.
54+
var myMessage = new SendGrid.SendGridMessage();
55+
myMessage.AddTo(to);
56+
myMessage.From = new MailAddress(from, fromName);
57+
myMessage.Subject = "Testing the SendGrid Library";
58+
myMessage.Text = "Hello World! %tag%";
59+
60+
var subs = new List<String> { "私は%type%ラーメンが大好き" };
61+
myMessage.AddSubstitution("%tag%", subs);
62+
myMessage.AddSection("%type%", "とんこつ");
63+
64+
SendAsync(myMessage);
65+
}
66+
67+
private static void ApiKeys()
68+
{
69+
String apiKey = Environment.GetEnvironmentVariable("SENDGRID_APIKEY", EnvironmentVariableTarget.User);
70+
var client = new SendGrid.Client(apiKey);
71+
72+
// GET API KEYS
73+
HttpResponseMessage responseGet = client.ApiKeys.Get().Result;
74+
Console.WriteLine(responseGet.StatusCode);
75+
Console.WriteLine(responseGet.Content.ReadAsStringAsync().Result);
76+
Console.WriteLine("These are your current API Keys. Press any key to continue.");
77+
Console.ReadKey();
78+
79+
// POST API KEYS
80+
HttpResponseMessage responsePost = client.ApiKeys.Post("CSharpTestKey").Result;
81+
var rawString = responsePost.Content.ReadAsStringAsync().Result;
82+
dynamic jsonObject = JObject.Parse(rawString);
83+
var apiKeyId = jsonObject.api_key_id.ToString();
84+
Console.WriteLine(responsePost.StatusCode);
85+
Console.WriteLine(responsePost.Content.ReadAsStringAsync().Result);
86+
Console.WriteLine("API Key created. Press any key to continue.");
87+
Console.ReadKey();
88+
89+
// PATCH API KEYS
90+
HttpResponseMessage responsePatch = client.ApiKeys.Patch(apiKeyId, "CSharpTestKeyPatched").Result;
91+
Console.WriteLine(responsePatch.StatusCode);
92+
Console.WriteLine(responsePatch.Content.ReadAsStringAsync().Result);
93+
Console.WriteLine("API Key patched. Press any key to continue.");
94+
Console.ReadKey();
95+
96+
// DELETE API KEYS
97+
Console.WriteLine("Deleting API Key, please wait.");
98+
HttpResponseMessage responseDelete = client.ApiKeys.Delete(apiKeyId).Result;
99+
Console.WriteLine(responseDelete.StatusCode);
100+
HttpResponseMessage responseFinal = client.ApiKeys.Get().Result;
101+
Console.WriteLine(responseFinal.StatusCode);
102+
Console.WriteLine(responseFinal.Content.ReadAsStringAsync().Result);
103+
Console.WriteLine("API Key Deleted, press any key to end");
104+
Console.ReadKey();
105+
}
106+
}
107+
}

SendGrid/Example/app.config

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
1+
<?xml version="1.0" encoding="utf-8"?>
32
<configuration>
43
<startup>
5-
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
65
</startup>
76
<runtime>
87
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
98
<dependentAssembly>
10-
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
11-
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
9+
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
10+
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0"/>
1211
</dependentAssembly>
1312
<dependentAssembly>
14-
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
15-
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0" />
13+
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
14+
<bindingRedirect oldVersion="0.0.0.0-2.6.9.0" newVersion="2.6.9.0"/>
1615
</dependentAssembly>
1716
<dependentAssembly>
18-
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
19-
<bindingRedirect oldVersion="0.0.0.0-2.2.18.0" newVersion="2.2.18.0" />
17+
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
18+
<bindingRedirect oldVersion="0.0.0.0-2.2.18.0" newVersion="2.2.18.0"/>
19+
</dependentAssembly>
20+
<dependentAssembly>
21+
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
22+
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0"/>
2023
</dependentAssembly>
2124
</assemblyBinding>
2225
</runtime>
23-
</configuration>
26+
</configuration>

SendGrid/Example/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
34
<package id="SendGrid.SmtpApi" version="1.3.1" targetFramework="net45" />
45
</packages>

0 commit comments

Comments
 (0)