@@ -30,7 +30,7 @@ We appreciate your continued support, thank you!
30
30
31
31
## Prerequisites
32
32
33
- - .NET version 4.5.2
33
+ - .NET version 4.5 and above
34
34
- The SendGrid service, starting at the [ free level] ( https://sendgrid.com/free?source=sendgrid-csharp )
35
35
36
36
## Setup Environment Variables
@@ -51,14 +51,13 @@ For a sample implementation, check the [Example](https://github.com/sendgrid/sen
51
51
Add the following namespaces to use the library:
52
52
``` csharp
53
53
using System ;
54
- using System .Web .Script .Serialization ;
55
54
using SendGrid ;
56
- using SendGrid .Helpers .Mail ; // Include if you want to use the Mail Helper
55
+ using SendGrid .Helpers .Mail ; // If you are using the Mail Helper
56
+ using Newtonsoft .Json ; // You can generate your JSON string yourelf or with another library if you prefer
57
57
```
58
58
59
59
## Dependencies
60
60
61
- - [ SendGrid.CSharp.HTTP.Client] ( https://github.com/sendgrid/csharp-http-client )
62
61
- [ Newtonsoft.Json] ( http://www.newtonsoft.com/json )
63
62
64
63
<a name =" quick_start " ></a >
@@ -72,31 +71,35 @@ The following is the minimum needed code to send an email with the [/mail/send H
72
71
73
72
``` csharp
74
73
using System ;
74
+ using System .Collections .Generic ;
75
+ using System .Threading .Tasks ;
76
+ using Newtonsoft .Json ; // You can generate your JSON string yourelf or with another library if you prefer
75
77
using SendGrid ;
76
78
using SendGrid .Helpers .Mail ;
77
- using System .Threading .Tasks ;
78
79
79
80
namespace Example
80
81
{
81
82
internal class Example
82
83
{
83
84
private static void Main ()
84
85
{
85
- Execute ().Wait ();
86
+ Execute ().Wait ();
86
87
}
87
88
88
89
static async Task Execute ()
89
90
{
90
91
string apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" , EnvironmentVariableTarget .User );
91
- dynamic sg = new SendGridAPIClient (apiKey );
92
+ Client client = new Client (apiKey );
92
93
93
94
Email from = new Email (
" [email protected] " );
94
95
string subject = " Hello World from the SendGrid CSharp Library!" ;
95
96
Email to = new Email (
" [email protected] " );
96
97
Content content = new Content (" text/plain" , " Hello, Email!" );
97
98
Mail mail = new Mail (from , subject , to , content );
98
99
99
- dynamic response = await sg .client .mail .send .post (requestBody : mail .Get ());
100
+ Response response = await client .RequestAsync (method : Client .Methods .POST ,
101
+ requestBody : mail .Get (),
102
+ urlPath : " mail/send" );
100
103
}
101
104
}
102
105
}
@@ -110,23 +113,24 @@ The following is the minimum needed code to send an email without the /mail/send
110
113
111
114
``` csharp
112
115
using System ;
113
- using SendGrid ;
114
- using Newtonsoft .Json ; // You can generate your JSON string yourelf or with another library if you prefer
116
+ using System .Collections .Generic ;
115
117
using System .Threading .Tasks ;
118
+ using Newtonsoft .Json ; // You can generate your JSON string yourelf or with another library if you prefer
119
+ using SendGrid ;
116
120
117
121
namespace Example
118
122
{
119
123
internal class Example
120
124
{
121
125
private static void Main ()
122
126
{
123
- Execute ().Wait ();
127
+ Execute ().Wait ();
124
128
}
125
129
126
130
static async Task Execute ()
127
131
{
128
- String apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" , EnvironmentVariableTarget .User );
129
- dynamic sg = new SendGridAPIClient (apiKey );
132
+ string apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" , EnvironmentVariableTarget .User );
133
+ Client client = new Client (apiKey );
130
134
131
135
string data = @" {
132
136
'personalizations': [
@@ -150,39 +154,15 @@ namespace Example
150
154
]
151
155
}" ;
152
156
Object json = JsonConvert .DeserializeObject <Object >(data );
153
- dynamic response = await sg .client .mail .send .post (requestBody : json .ToString ());
154
- }
155
- }
156
- }
157
- ```
158
-
159
- ## General v3 Web API Usage (With Fluent Interface)
160
-
161
- ``` csharp
162
- using System ;
163
- using SendGrid ;
164
- using System .Threading .Tasks ;
165
-
166
- namespace Example
167
- {
168
- internal class Example
169
- {
170
- private static void Main ()
171
- {
172
- Execute ().Wait ();
157
+ Response response = await client .RequestAsync (method : Client .Methods .POST ,
158
+ requestBody : json .ToString (),
159
+ urlPath : " mail/send" );
173
160
}
174
-
175
- static async Task Execute ()
176
- {
177
- string apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" , EnvironmentVariableTarget .User );
178
- dynamic sg = new SendGrid .SendGridAPIClient (apiKey );
179
- dynamic response = await sg .client .suppression .bounces .get ();
180
- }
181
161
}
182
162
}
183
163
```
184
164
185
- ## General v3 Web API Usage (Without Fluent Interface)
165
+ ## General v3 Web API Usage
186
166
187
167
``` csharp
188
168
using System ;
@@ -200,10 +180,12 @@ namespace Example
200
180
201
181
static async Task Execute ()
202
182
{
203
- string apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" , EnvironmentVariableTarget .User );
204
- dynamic sg = new SendGrid .SendGridAPIClient (apiKey );
205
- dynamic response = await sg .client ._ (" suppression/bounces" ).get ();
206
- }
183
+ string apiKey = Environment .GetEnvironmentVariable (" NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY" , EnvironmentVariableTarget .User );
184
+ Client client = new Client (apiKey );
185
+ Response response = await client .RequestAsync (method : Client .Methods .GET ,
186
+ requestBody : json .ToString (),
187
+ urlPath : " suppression/bounces" );
188
+ }
207
189
}
208
190
}
209
191
```
0 commit comments