Testing Sendgrid SMTP without sending emails using sandbox mode

Here’s how to test out a Sendgrid integration using SMTP without actually sending emails.  Obviously make sure you have Sendgrid SMTP set up in your config first, and then just add the X-SMTPAPI header (docs link) with some JSON like the below.


MailMessage msg = new MailMessage();
var json = @"{
““mail_settings”": {
““sandbox_mode”": {
““enable”": true
}
}
}";

msg.Headers.Add(“X-SMTPAPI”, json);
// all the usual email stuff

SmtpClient smtpclient = new SmtpClient();
smtpclient.Send(msg);