我正在使用sendgrid发送电子邮件。我可以在sendgrid门户中看到已发送的邮件活动。现在我想在我的c#代码中跟踪这些活动。就像邮件发生的那样 - >交付或退回或阻止。有没有解决方案?
答案 0 :(得分:1)
可以使用Sendgrid Events,结果很简单。
查看https://sendgrid.com/docs/API_Reference/Webhooks/event.html
您可以决定接收事件的方式,在我的情况下,我选择了JSON。
在C#Web应用程序中,您可以创建一个类:
public class EmailEvent
{
[JsonProperty("sg_message_id")]
public string MessageId { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("timestamp")]
public long Timestamp { get; set; }
[JsonProperty("smtp-id")]
public string smtpid { get; set; }
[JsonProperty("event")]
public string Event { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("category")]
public string Category { get; set; }
[JsonProperty("userid")]
public string UserId { get; set; }
}
和控制器动作如:
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> IncomingNotification([FromBody] EmailEvent[] events)
{
// be prepared to handle an array of events as sendgrid can send batches
}
只要符合您配置SendGrid webhook的方式,您就可以按自己喜欢的方式命名动作。