Can my ASP.Net Code get confirmation from sendgrid that an email has been sent?

无人久伴 提交于 2019-12-04 02:45:45

Emails sent via the SendGrid web API are asynchronous, so to get a confirmation, you need to implement a webhook. The Event Webhook will post events of your choice to a URL that you define. In this case you are interested in the "delivered" event.

You'll need some code on your server to handle the incoming webhook and do any logic based on the results, such as logging delivered events. There are a few community-contributed libraries out there that let you easily create a webhook handler. I suggest sendgrid-webhooks, which is available on nuget.

Then take the incoming POST and hand it to the parser to get an object back.

Since you are using ASP.NET MVC, then you can use an [HttpPost] method inside of a controller to receive the POST data from SendGrid. Then you can parse it using sendgrid-webhooks.

From the sendgrid-webhooks readme:

var parser = new WebhookParser();
var events = parser.ParseEvents(json);

var webhookEvent = events[0];

//shared base properties
webhookEvent.EventType; //Enum - type of the event as enum
webhookEvent.Categories; //IList<string> - list of categories assigned ot the event
webhookEvent.TimeStamp; //DateTime - datetime of the event converted from unix time
webhookEvent.UniqueParameters; //IDictionary<string, string> - map of key-value unique parameters

//event specific properties
var clickEvent = webhookEvent as ClickEvent; //cast to the parent based on EventType
clickEvent.Url; //string - url on what the user has clicked

I work at SendGrid so please let me know if there's anything I can help with.

You will want to use Event Webhooks to get confirmation sent back to you to confirm the message has been delivered to the recipient.

You would need to setup a page to accept events from Sendgrid, such as:

https://yourdomain.com/email/hook which would accept JSON which you would then deal with however you want. The Json.NET documentation would be able to guide you with how to accept JSON and then turn it into an object you can use.

Example JSON you'd be POSTed:

{
    "sg_message_id":"sendgrid_internal_message_id",
    "email": "john.doe@sendgrid.com",
    "timestamp": 1337197600,
    "smtp-id": "<4FB4041F.6080505@sendgrid.com>",
    "event": "delivered"
  },

The events you can receive from SendGrid are: Processed, Dropped, Delivered, Deferred, Bounce, Open, Click, Spam Report, Unsubscribe, Group Unsubscribe, Group Resubscribe.

With all these options you could have a webhook to deal with Bounces such as get someone to find out the correct e-mail address for the user you tried to email.

I dont think SendGrid is set up to give a response. However, as a hack, you could BCC yourself (and therefore know at least email has gone out) by adding the following code to your configSendGridasync class

    .
    .
    .
    //this is your old code...
    myMessage.Text=message.Body;
    myMessage.Html = message.Body;

    //Add this...
    myMessage.AddBcc("yourEmail@domain.com");

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!