Home > Blockchain >  How can I get the message-Id after sending an email in Sengrid?
How can I get the message-Id after sending an email in Sengrid?

Time:11-04

I am using sendgrid with .net, when I send and email with the API the response contains an x-message-id, but I need the message-id that is different

CodePudding user response:

As you mentioned, the response includes x-message-id. When you use the API to send messages, you can send multiple using a single API call and thus the x-message-id identifies multiple messages.

To get the individual messages and their ID's, you can retrieve messages using the API by filtering on that x-message-id:

var queryParams = JsonSerializer.Serialize(new
{
  query = $"msg_id LIKE '{messageId}%'",
  limit = 10
});

var response = await client.RequestAsync(
  method: SendGridClient.Method.GET,
  urlPath: "messages",
  queryParams: queryParams
);

Console.WriteLine(await response.Body.ReadAsStringAsync());

The JSON returned looks like this:

{
  "messages": [
    {
      "from_email": "[email protected]",
      "msg_id": "5QSczogTRHqFtiIkLxMtWA.filterdrecv-5645d9c87f-6r2ch-1-62847C63-2D.0",
      "subject": "Sending with Twilio SendGrid is Fun",
      "to_email": "[email protected]",
      "status": "delivered",
      "opens_count": 0,
      "clicks_count": 0,
      "last_event_time": "2022-05-18T05: 01: 05Z"
    },
    {
      "from_email": "[email protected]",
      "msg_id": "5QSczogTRHqFtiIkLxMtWA.filterdrecv-5645d9c87f-6r2ch-1-62847C63-2D.1",
      "subject": "Sending with Twilio SendGrid is Fun",
      "to_email": "[email protected]",
      "status": "delivered",
      "opens_count": 0,
      "clicks_count": 0,
      "last_event_time": "2022-05-18T05: 01: 05Z"
    },
        ...
  ]
}

Note 1: You must purchase additional email activity history to gain access to the Email Activity Feed API.

Note 2: To retrieve message via the Email Activity Feed API, your API key must have the email_activity.read permission.


I'm not sure what your use case is, but you can use the SendGrid Event Webhook to get information about your email delivery and more in real-time, which may be a better way to go.

  • Related