diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs
index 4d2f484a2..d3a6de1c4 100644
--- a/SendGrid/SendGridMail/Transport/Web.cs
+++ b/SendGrid/SendGridMail/Transport/Web.cs
@@ -23,27 +23,48 @@ public class Web : ITransport
private readonly NetworkCredential _credentials;
private readonly HttpClient _client;
+ private readonly string _apiKey;
#endregion
+ ///
+ /// Creates a new Web interface for sending mail
+ ///
+ /// The API Key with which to send
+ public Web(string apiKey)
+ : this(apiKey, null, TimeSpan.FromSeconds(100)) { }
+
+ /// Creates a new Web interface for sending mail
+ ///
+ /// The API Key with which to send
+ /// HTTP request timeout
+ public Web(string apiKey, TimeSpan httpTimeout)
+ : this(apiKey, null, httpTimeout) { }
+
///
/// Creates a new Web interface for sending mail
///
/// SendGridMessage user parameters
public Web(NetworkCredential credentials)
- : this(credentials, TimeSpan.FromSeconds(100)) { }
+ : this(null, credentials, TimeSpan.FromSeconds(100)) { }
///
/// Creates a new Web interface for sending mail.
///
+ /// The API Key with which to send
/// SendGridMessage user parameters
/// HTTP request timeout
- public Web(NetworkCredential credentials, TimeSpan httpTimeout)
+ public Web(string apiKey, NetworkCredential credentials, TimeSpan httpTimeout)
{
_credentials = credentials;
_client = new HttpClient();
-
+ _apiKey = apiKey;
+
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
+ if (credentials == null)
+ {
+ _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);
+ }
_client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "sendgrid/" + version + ";csharp");
_client.Timeout = httpTimeout;
}
@@ -157,8 +178,7 @@ internal List> FetchFormParams(ISendGrid message)
{
var result = new List>
{
- new KeyValuePair("api_user", _credentials.UserName),
- new KeyValuePair("api_key", _credentials.Password),
+
new KeyValuePair("headers",
message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)),
new KeyValuePair("replyto",
@@ -170,6 +190,18 @@ internal List> FetchFormParams(ISendGrid message)
new KeyValuePair("html", message.Html),
new KeyValuePair("x-smtpapi", message.Header.JsonString() ?? "")
};
+
+ //if the api key is not specified, use the username and password
+ if (_credentials != null)
+ {
+ var creds = new List>
+ {
+ new KeyValuePair("api_user", _credentials.UserName),
+ new KeyValuePair("api_key", _credentials.Password)
+ };
+ result.AddRange(creds);
+ }
+
if (message.To != null)
{
result = result.Concat(message.To.ToList().Select(a => new KeyValuePair("to[]", a.Address)))