在 C# 代码中跟踪电子邮件 activity

Track email activity in C# code

我正在使用 sendgrid 发送电子邮件。我可以在我的 sendgrid 门户中查看已发送邮件活动。现在我想在我的 c# 代码中跟踪这些活动。就像邮件发生了什么 - > 交付或退回或阻止。有什么解决办法吗?

使用 Sendgrid Events 是可行的,事实证明它相当简单。

查看https://sendgrid.com/docs/API_Reference/Webhooks/event.html

您可以决定接收事件的方式,在我的例子中我选择了 JSON。

在您的 C# 网络应用程序中,您可以创建一个 class 如:

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 的方式相匹配即可。