Sending an email with SendGrid API from ASP.Net Framework 3.5

2.1k Views Asked by At

I'm trying to send an email using SendGrid API with an API Key I already have.

The problem is: I have to do this from an old .net application, whose asp.net framework version is 3.5 (and changing framework version is not an option)

I'm failing to find useful information on how to achieve it. The only code I found makes use of SendGrid csharp libraries, and these do not support asp.net framework 3.5

This is the code sample I found here, but I cannot make this work from my .net 3.5 web app:

// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp
using System.Net.Http;
using System.Net.Mail;

var myMessage = new SendGrid.SendGridMessage();
myMessage.AddTo("[email protected]");
myMessage.From = new MailAddress("[email protected]", "First Last");
myMessage.Subject = "Sending with SendGrid is Fun";
myMessage.Text = "and easy to do anywhere, even with C#";

var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");
transportWeb.DeliverAsync(myMessage);
// NOTE: If you're developing a Console Application, use the following so that the API call has time to complete
// transportWeb.DeliverAsync(myMessage).Wait();

Any ideas?

1

There are 1 best solutions below

1
On

You have a couple of options. The SendGrid API uses the HttpClient class to make the requests and this requires the .NET 4+ dependency.

  1. You could try implementing your own implementation using RestSharp, this is compatible with .NET 3.5 and the SendGrid API uses an interface you can implement. It would just need to be adjusted from the source code on github.

    1. Use the .Net SMTP classes to send your emails and configure as per the guidelines in the SendGrid documentation.

    2. Proxy the requests via another WebAPI that is running .NET 4+ and simplify what is required to make these calls by implementing your own API. Use something like the WebClient class or RestSharp to make the calls.

** Scrap that, option 1 is harder than it looked ** That the ISendGridClient is a bit of a weird example. It used the implementation inside of the interface for some parameters. Looks like 2 and 3 are good options!