I am using SendGrid C# code to send the emails, i was successfully sent the emails but in response i am not getting delivery status. If i send mail to wrong email address also, it is giving success response i am not getting bounce status response. Then i did some R&D how to get the delivery status, then i came to know about Webhook. I am new to Webhook i created a webhook URL in keen webiste and pasted in SendGrid webook events.
my normal Sendgrid email C# code
Environment.SetEnvironmentVariable("SENDGRID_API_KEY", "xxxxx");
var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("[email protected]", "CAP");
var subject = "Testing email from Sendgrid";
var to = new EmailAddress("[email protected]", "Good User");
var plainTextContent = "Hello, ";
var htmlContent = "Congratulations! you have got the email";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
msg.SetBypassBounceManagement(true);
msg.TrackingSettings = new TrackingSettings()
{
ClickTracking = new ClickTracking()
{
Enable = true,
EnableText = false
},
OpenTracking = new OpenTracking()
{
Enable = true,
SubstitutionTag = "%open-track%"
},
SubscriptionTracking = new SubscriptionTracking()
{
Enable = false
}
};
var response = await client.SendEmailAsync(msg).ConfigureAwait(false);But i don't know how to get the delivery status in response after sending email. Any one please help on this.


Gowtham CpRank #1,552 · 1K reputationPosted Aug 13, 2024, 9:36 AM EDT
You’re not receiving email delivery statuses because SendGrid’s
SendEmailAsynconly confirms that the email was accepted, not its final status. To track delivery, bounces, and other events, set up a webhook in SendGrid that sends event notifications to your C# endpoint. Create a simple web service (e.g., using ASP.NET Core) to handle these notifications, process the data, and log or act on the email statuses.Gowtham CpRank #1,552 · 1K reputationPosted Aug 15, 2024, 5:04 AM EDT
Hi Kranthi,
Here’s a streamlined example of a web service to handle SendGrid webhook notifications:
1. Project Setup:
2. Webhook Controller:
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; // For JSON deserialization using System.Threading.Tasks; namespace EmailWebhookReceiver.Controllers { [Route("api/[controller]")] [ApiController] public class WebhookController : ControllerBase { private readonly ILogger _logger; public WebhookController(ILogger logger) { _logger = logger; } [HttpPost] public async Task Post([FromBody] SendGridEvent[] events) // Deserialize to SendGridEvent array { if (events == null || events.Length == 0) { _logger.LogWarning("Received null or empty events."); return BadRequest("No events received."); } foreach (var eventData in events) { var eventType = eventData.event; var email = eventData.email; var timestamp = eventData.timestamp; var status = eventData.status; // Get delivery status _logger.LogInformation($"Event: {eventType}, Email: {email}, Timestamp: {timestamp}, Status: {status}"); // Custom processing based on event type switch (eventType) { case "delivered": // Handle delivery success break; case "bounce": // Handle bounce (email not deliverable) break; case "dropped": // Handle dropped (email rejected) break; case "click": // Handle click event break; case "open": // Handle open event break; default: _logger.LogWarning($"Unhandled event type: {eventType}"); break; } } return Ok(); } } public class SendGridEvent { public string event { get; set; } public string email { get; set; } public long timestamp { get; set; } public string status { get; set; } } }3. Configure SendGrid Webhook:
- Set the webhook URL in SendGrid (e.g., `https://yourdomain.com/api/webhook`).
- Choose the events to track (e.g., delivered, bounced).
Kranthi Vardhan KolluruRank #3,416 · 423 reputationPosted Aug 14, 2024, 6:20 AM EDT
Hi Goutham,
Thanks for the response.
I created all the webhook setup in sendgrid but i am failing to create the service i have no idea on this how to access response after email sent
Could you please provide that web service code to handle these notifications, i tried a lot i was not found any suitable code.
Kranthi Vardhan KolluruRank #3,416 · 423 reputationPosted Aug 16, 2024, 6:18 AM EDT
Thank you Goutham,
How should i call this response in front end immedietly after sending the email to the customer. I mean i want the status of email delivery immedetly after sending the mail. Could you please give some code in C# to get this response
Gowtham CpRank #1,552 · 1K reputationPosted Aug 16, 2024, 12:07 PM EDT
To get real-time email delivery updates on your front-end, you can use SignalR. First, connect to your SignalR hub by specifying its URL. Next, listen for status updates using connection.on("ReceiveStatusUpdate", ...), which lets you update your UI with the latest email status as soon as it’s available. Start the connection with connection.start() and handle any errors that come up.