sendgrid/sendgrid-csharp


The Official Twilio SendGrid Led, Community Driven C#, .NetStandard, .NetCore API Library

https://sendgrid.com

License: MIT

Language: C#

Keywords: c-sharp, dotnet, dotnet-core, dotnet-standard, dotnetcore, email, sendgrid, transactional-emails


SendGrid Logo

BuildStatus NuGet Email Notifications Badge MIT licensed Twitter Follow GitHub contributors Open Source Helpers

Announcements

  • Subscribe to email notifications for releases and breaking changes.
  • Send SMS messages with Twilio.
  • If you're a software engineer who is passionate about #DeveloperExperience and/or #OpenSource, this is an incredible opportunity to join our #DX team as a Developer Experience Engineer and work with @thinkingserious and @aroach! Tell your friends :)

Overview

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via C# with .NET.

Version 9.X.X+ of this library provides full support for all Twilio SendGrid Web API v3 endpoints, including the new v3 /mail/send.

We want this library to be community driven, and Twilio SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.

For updates to this library, see our CHANGELOG and releases.

Subscribe to email release notifications to receive emails about releases and breaking changes.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • .NET version 4.5.2 and higher
  • .NET Core 1.0 and higher
  • .NET Standard 1.3 support
  • A Twilio SendGrid account, sign up for free to send up to 40,000 emails for the first 30 days, then send 100 emails/day free forever or check out our pricing.

Obtain an API Key

Grab your API Key from the Twilio SendGrid UI.

Setup Environment Variables to Manage Your API Key

Manage your Twilio SendGrid API Keys by storing them in Environment Variables or in Web.config. It is a good practice to keep your data and configuration settings separate. This way to you can change your Twilio SendGrid API key without changing your code. Also, we strongly advise against storing sensitive data directly in your code.

Setup Environment Variables using the UI:

  1. Press Win+R and run SystemPropertiesAdvanced
  2. Click on Environment Variables
  3. Click New in user variables section
  4. Type SENDGRID_API_KEY in the name. (Make sure this name matches the name of key in your code)
  5. Type actual API Key in the value
  6. Restart the IDE and you're done!

Setup Environment Variables using CMD:

  1. Run CMD as administrator
  2. setx SENDGRID_API_KEY "YOUR_API_KEY"

Here are few example to get and set API Keys programatically:

Get Environment Variable

var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");

Set Environment Variable

var setKey = Environment.SetEnvironmentVariable("SENDGRID_API_KEY", "YOUR_API_KEY");

Install Package

To use Twilio SendGrid in your C# project, you can either download the Twilio SendGrid C# .NET libraries directly from our Github repository or if you have the NuGet package manager installed, you can grab them automatically:

PM> Install-Package SendGrid

Once you have the Twilio SendGrid library installed, you can include calls to them in your code. For sample implementations, see the .NET Core Example and the .NET 4.5.2 Example folders.

Dependencies

Quick Start

Hello Email

The following is the minimum needed code to send an simple email. Use this example, and modify the apiKey, from and to variables:

using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

        static async Task Execute()
        {
            var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
            var client = new SendGridClient(apiKey);
            var from = new EmailAddress("test@example.com", "Example User");
            var subject = "Sending with Twilio SendGrid is Fun";
            var to = new EmailAddress("test@example.com", "Example User");
            var plainTextContent = "and easy to do anywhere, even with C#";
            var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
            var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response = await client.SendEmailAsync(msg);
        }
    }
}

After executing the above code, response.StatusCode should be 202 and you should have an email in the inbox of the to recipient. You can check the status of your email in the UI. Alternatively, we can post events to a URL of your choice using our Event Webhook. This gives you data about the events that occur as Twilio SendGrid processes your email.

For more advanced cases, you can build the SendGridMessage object yourself with these minimum required settings:

using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

	    static async Task Execute()
        {
            var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
            var client = new SendGridClient(apiKey);
            var msg = new SendGridMessage()
            {
                From = new EmailAddress("test@example.com", "DX Team"),
                Subject = "Sending with Twilio SendGrid is Fun",
                PlainTextContent = "and easy to do anywhere, even with C#",
                HtmlContent = "<strong>and easy to do anywhere, even with C#</strong>"
            };
            msg.AddTo(new EmailAddress("test@example.com", "Test User"));
            var response = await client.SendEmailAsync(msg);
        }
    }
}

You can find an example of all of the email features here.

General v3 Web API Usage

using System;
using System.Threading.Tasks;
using SendGrid;

namespace Example
{
    internal class Example
    {
        private static void Main()
        {
            Execute().Wait();
        }

        static async Task Execute()
        {
            var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
            var client = new SendGridClient(apiKey);
            var queryParams = @"{
                'limit': 100
            }";
            var response = await client.RequestAsync(method: SendGridClient.Method.GET,
                                                     urlPath: "suppression/bounces",
                                                     queryParams: queryParams);
	    }
    }
}

Web Proxy

var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var proxy = new WebProxy("http://proxy:1337");
var client = new SendGridClient(proxy, apiKey);

Usage

Use Cases

Here are some examples of common API use cases, such as how to send an email with a transactional template.

Roadmap

If you are interested in the future direction of this project, please take a look at our open issues and pull requests. We would love to hear your feedback!

How to Contribute

We encourage contribution to our library (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-csharp is guided and supported by the Twilio Developer Experience Team.

Email the Developer Experience Team here in case of any assistance or queries.

sendgrid-csharp is maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-csharp are trademarks of Twilio SendGrid, Inc.

License

The MIT License (MIT)

Project Statistics

Sourcerank 15
Repository Size 9.05 MB
Stars 608
Forks 425
Watchers 258
Open issues 178
Dependencies 54
Contributors 81
Tags 45
Created
Last updated
Last pushed

Top Contributors See all

Elmer Thomas Brandon West Tyler Bischel Dylan Morley Matt Bernier Dan Jagnow Brian Surowiec Alex Forbes-Reed Niladri Dutta Graham Mueller Bob McLaren Pontus Öwre Xavier Hahn Roel de Vries Jef Statham paritosh baghel thepriefy Arpit Jain saluce65 Jonny Bekkum

Packages Referencing this Repo

Sendgrid
C# client library and examples for using Twilio SendGrid API's to send mail and access Web API v3...
Latest release 9.11.0 - Updated - 608 stars
SendGrid
C# client library and examples for using SendGrid API's to send mail and access Web API v3 endpoi...
Latest release 9.10.0 - Updated - 608 stars
SendgridTestNuget
C# client library and examples for using SendGrid API's to send mail. Github repo located at : ht...
This package is no longer available on NuGet
SendGridTestNuget
C# client library and examples for using SendGrid API's to send mail. Github repo located at : ht...
This package is no longer available on NuGet
SendGrid.v9beta.Port
v9beta .NET Core and .NET 4.5.X and up support.Not an official sendgrid release but a premature p...
Latest release 1.0.0 - Published - 608 stars
SendGridv9beta
C# client library and examples for using SendGrid API's to send mail and access Web API v3 endpoi...
This package is no longer available on NuGet

Recent Tags See all

v9.11.0 April 18, 2019
v9.10.0 September 12, 2018
v9.9.0 August 22, 2017
v9.8.0 August 15, 2017
v9.6.1 August 03, 2017
v9.6.0 August 02, 2017
v9.5.2 July 24, 2017
v9.5.1 July 20, 2017
v9.5.0 June 19, 2017
v9.4.1 May 17, 2017
v9.4.0 May 17, 2017
v9.3.0 May 16, 2017
v9.2.1 May 16, 2017
v9.2.0 May 06, 2017
v9.1.1 April 13, 2017

Interesting Forks See all

advancedrei/sendgridplus-csharp
C# - MIT - Last pushed - 13 stars - 7 forks
pandeysoni/sendgrid-csharp
ssl
C# - MIT - Updated - 2 stars - 1 forks
Codablo/sendgrid-csharp
Easily send emails from .NET
C# - Last pushed - 1 stars
lizhi5753186/sendgrid-csharp
Easily send emails from .NET
C# - Updated - 1 stars

Something wrong with this page? Make a suggestion

Last synced: 2019-04-18 17:19:09 UTC

Login to resync this repository