Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions SendGrid/SendGridMail/Transport/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,48 @@ public class Web : ITransport

private readonly NetworkCredential _credentials;
private readonly HttpClient _client;
private readonly string _apiKey;

#endregion

/// <summary>
/// Creates a new Web interface for sending mail
/// </summary>
/// <param name="apiKey">The API Key with which to send</param>
public Web(string apiKey)
: this(apiKey, null, TimeSpan.FromSeconds(100)) { }

/// Creates a new Web interface for sending mail
/// </summary>
/// <param name="apiKey">The API Key with which to send</param>
/// <param name="httpTimeout">HTTP request timeout</param>
public Web(string apiKey, TimeSpan httpTimeout)
: this(apiKey, null, httpTimeout) { }

/// <summary>
/// Creates a new Web interface for sending mail
/// </summary>
/// <param name="credentials">SendGridMessage user parameters</param>
public Web(NetworkCredential credentials)
: this(credentials, TimeSpan.FromSeconds(100)) { }
: this(null, credentials, TimeSpan.FromSeconds(100)) { }

/// <summary>
/// Creates a new Web interface for sending mail.
/// </summary>
/// <param name="apiKey">The API Key with which to send</param>
/// <param name="credentials">SendGridMessage user parameters</param>
/// <param name="httpTimeout">HTTP request timeout</param>
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;
}
Expand Down Expand Up @@ -157,8 +178,7 @@ internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
{
var result = new List<KeyValuePair<string, string>>
{
new KeyValuePair<String, String>("api_user", _credentials.UserName),
new KeyValuePair<String, String>("api_key", _credentials.Password),

new KeyValuePair<String, String>("headers",
message.Headers.Count == 0 ? null : Utils.SerializeDictionary(message.Headers)),
new KeyValuePair<String, String>("replyto",
Expand All @@ -170,6 +190,18 @@ internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
new KeyValuePair<String, String>("html", message.Html),
new KeyValuePair<String, String>("x-smtpapi", message.Header.JsonString() ?? "")
};

//if the api key is not specified, use the username and password
if (_credentials != null)
{
var creds = new List<KeyValuePair<string, string>>
{
new KeyValuePair<String, String>("api_user", _credentials.UserName),
new KeyValuePair<String, String>("api_key", _credentials.Password)
};
result.AddRange(creds);
}

if (message.To != null)
{
result = result.Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("to[]", a.Address)))
Expand Down