Send email with sendgrid c#

Here is an example of how to send an email using SendGrid in C#:

Step 1: Install the SendGrid NuGet package

In your C# project, right-click on the project in Visual Studio and select "Manage NuGet Packages". Search for "SendGrid" and install the "SendGrid" package.

Step 2: Create a SendGrid API key

Go to the SendGrid dashboard and create a new API key. You can do this by clicking on the "Settings" icon (gear icon) in the top right corner, then selecting "API Keys" and clicking on "Create API Key".

Step 3: Set up the SendGrid API key in your C# code

Create a new class in your C# project to handle the email sending:

using SendGrid;
using SendGrid.Helpers.Mail;

public class SendGridEmail
{
    private readonly string _apiKey;

    public SendGridEmail(string apiKey)
    {
        _apiKey = apiKey;
    }

    public void SendEmail(string fromEmail, string fromName, string toEmail, string subject, string body)
    {
        var client = new SendGridClient(_apiKey);

        var msg = MailHelper.CreateSingleEmail(fromEmail, fromName, toEmail, subject, body);

        client.SendEmailAsync(msg);
    }
}

Step 4: Use the SendGridEmail class to send an email

Create an instance of the SendGridEmail class and call the SendEmail method:

string apiKey = "YOUR_API_KEY_HERE";
string fromEmail = "[email protected]";
string fromName = "Your Name";
string toEmail = "[email protected]";
string subject = "Test Email";
string body = "This is a test email sent using SendGrid";

var sendGridEmail = new SendGridEmail(apiKey);
sendGridEmail.SendEmail(fromEmail, fromName, toEmail, subject, body);

Note: Make sure to replace YOUR_API_KEY_HERE with your actual SendGrid API key.

That's it! This code should send an email using SendGrid. You can customize the email settings and content as needed.