Search code examples
c#sendgrid

Add filter to SMTP Client


I am using the following code to send emails using SendGrid:

        string userName = ConfigurationManager.AppSettings["SendGridUser"];
        string passWord = ConfigurationManager.AppSettings["SendGridKey"];

        SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
        NetworkCredential credentials = new NetworkCredential(userName, passWord);
        smtpClient.Credentials = credentials;

        smtpClient.Send(mailMsg);

And now I need to add an additional filter setting and now sure how to add it. The filter that I need to include is to included the enabling of "bypass list management". How do I add that?


Solution

  • Twilio SendGrid developer evangelist here.

    To send filters, such as "bypass list management", you should send them as a JSON string as part of the X-SMTPAPI header. You can read more about building an X-SMTPAPI header here.

    An example X-SMTPAPI header that bypasses list management might look like:

    {
      "filters": {
        "bypass_list_management": true
      }
    }
    

    You can add this to the headers of the mailMsg like so:

    mailMsg.Headers.Add("X-SMTPAPI", jsonString);