.net核心中的SendGrid入站解析Webhook

时间:2019-12-16 16:28:44

标签: c# .net-core sendgrid

我正在尝试从SendGrid docs实现入站解析的代码片段,该片段分析入站Sendgrid电子邮件,但似乎在.net-core上不起作用。

// POST api/inbound
[HttpPost]
public async Task<HttpResponseMessage> Post()
{
    var root = HttpContext.Current.Server.MapPath("~/App_Data");
    var provider = new MultipartFormDataStreamProvider(root);
    await Request.Content.ReadAsMultipartAsync(provider);

    var email = new Email
    {
        Dkim = provider.FormData.GetValues("dkim").FirstOrDefault(),
        To = provider.FormData.GetValues("to").FirstOrDefault(),
        Html = provider.FormData.GetValues("html").FirstOrDefault(),
        From = provider.FormData.GetValues("from").FirstOrDefault(),
        Text = provider.FormData.GetValues("text").FirstOrDefault(),
        SenderIp = provider.FormData.GetValues("sender_ip").FirstOrDefault(),
        Envelope = provider.FormData.GetValues("envelope").FirstOrDefault(),
        Attachments = int.Parse(provider.FormData.GetValues("attachments").FirstOrDefault()),
        Subject = provider.FormData.GetValues("subject").FirstOrDefault(),
        Charsets = provider.FormData.GetValues("charsets").FirstOrDefault(),
        Spf = provider.FormData.GetValues("spf").FirstOrDefault()
    };

    // The email is now stored in the email variable

    return new HttpResponseMessage(HttpStatusCode.OK);
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

这是我解析.net核心中的入站SendGrid电子邮件的方法。

鉴于以下环境:

代码:

public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log)
{

var parser = new WebhookParser();
var inboundMail = await parser.ParseInboundEmailWebhookAsync(req.Body);

inboundMail.Subject etc
...