我正在使用我自己的 SendGrid API 自定义实现,因为它的 C# API 不支持 .NET Core。
这里是 API 模型和实现。您需要参考 JSON.NET 才能使 JSON 解析和序列化正常工作。
发送网格内容
public class SendGridContent
{
public SendGridContent()
{
}
public SendGridContent(string type, string content)
{
this.Type = type;
this.Value = content;
}
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
SendGridEmail
public class SendGridEmail
{
public SendGridEmail()
{
}
public SendGridEmail(string email, string name = null)
{
this.Email = email;
this.Name = name;
}
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
SendGridMessage
public class SendGridMessage
{
public const string TypeText = "text";
public const string TypeHtml = "text/html";
public SendGridMessage()
{
}
public SendGridMessage(SendGridEmail to, string subject, SendGridEmail from, string message, IEnumerable<SendGridEmail> bcc = null, string type = TypeHtml)
{
this.Personalizations = new List<SendGridPersonalization>
{
new SendGridPersonalization
{
To = new List<SendGridEmail> { to },
Bcc = bcc,
Subject = subject
}
};
this.From = from;
this.Content = new List<SendGridContent> { new SendGridContent(type, message) };
}
[JsonProperty("personalizations")]
public List<SendGridPersonalization> Personalizations { get; set; }
[JsonProperty("from")]
public SendGridEmail From { get; set; }
[JsonProperty("content")]
public List<SendGridContent> Content { get; set; }
}
SendGrid个性化
public class SendGridPersonalization
{
[JsonProperty("to")]
public List<SendGridEmail> To { get; set; }
[JsonProperty("bcc")]
public IEnumerable<SendGridEmail> Bcc { get; set; }
[JsonProperty("subject", NullValueHandling = NullValueHandling.Ignore)]
public string Subject { get; set; }
}
我的服务界面
public interface IEmailSender
{
Task Send(
string fromAddress,
string fromName,
string to,
string subject,
string message,
IEnumerable<string> bcc = null);
}
以及实施
// Documentation: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
public class SendGridEmailSender : IEmailSender
{
private readonly HttpClient httpClient;
public SendGridEmailSender(string apiKey)
{
this.httpClient = new HttpClient();
this.httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
this.httpClient.BaseAddress = new Uri("https://api.sendgrid.com/v3/");
}
// TODO: Delete custom code and use their library once the SendGrid library starts to support .NET Core
// Implemented using custom SendGrid API usage implementation due to lack of .NET Core support for their library
public async Task Send(
string fromAddress,
string fromName,
string to,
string subject,
string message,
IEnumerable<string> bcc = null)
{
var bccEmails = bcc?.Select(c => new SendGridEmail { Email = c, Name = fromName });
var msg = new SendGridMessage(
new SendGridEmail(to),
subject,
new SendGridEmail(fromAddress, fromName),
message,
bccEmails);
try
{
var json = JsonConvert.SerializeObject(msg);
var response = await this.httpClient.PostAsync(
"mail/send",
new StringContent(json, Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
// See if we can read the response for more information, then log the error
var errorJson = await response.Content.ReadAsStringAsync();
throw new Exception(
$"SendGrid indicated failure! Code: {response.StatusCode}, reason: {errorJson}");
}
}
catch (Exception)
{
// TODO: await this.logger.LogExceptionAsync(ex);
}
}
}