• Stars
    star
    1,030
  • Rank 42,936 (Top 0.9 %)
  • Language
    C#
  • License
    MIT License
  • Created over 12 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

SendGrid Logo

Test and Deploy NuGet MIT licensed Twitter Follow GitHub contributors Open Source Helpers

Announcements

  • Send SMS messages with Twilio.

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.

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

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • .NET Framework 4.0+
  • .NET Core 1.0+
  • .NET Standard 1.3+
  • 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 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 the 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. set SENDGRID_API_KEY="YOUR_API_KEY"

Here are a few examples to get and set API Keys programmatically:

# 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:

dotnet add package SendGrid

# use Twilio SendGrid with HttpClientFactory
dotnet add package SendGrid.Extensions.DependencyInjection

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

Dependencies

Please see the .csproj file.

Quick Start

Hello Email

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

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

class Program
{
    static async Task Main()
    {
        var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        var client = new SendGridClient(apiKey);
        var from = new EmailAddress("[email protected]", "Example User");
        var subject = "Sending with Twilio SendGrid is Fun";
        var to = new EmailAddress("[email protected]", "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).ConfigureAwait(false);
    }
}

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 System;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;

class Program
{
    static async Task Main()
    {
        var apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("[email protected]", "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("[email protected]", "Test User"));
        var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
    }
}

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

General v3 Web API Usage

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

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

HttpClientFactory + Microsoft.Extensions.DependencyInjection

SendGrid.Extensions.DependencyInjection is required

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using SendGrid;
using SendGrid.Extensions.DependencyInjection;
using SendGrid.Helpers.Mail;

class Program
{
    static async Task Main()
    {
        var services = ConfigureServices(new ServiceCollection()).BuildServiceProvider();
        var client = services.GetRequiredService<ISendGridClient>();
        var msg = new SendGridMessage()
        {
            From = new EmailAddress("[email protected]", "Example User"),
            Subject = "Sending with Twilio SendGrid is Fun"
        };
        msg.AddContent(MimeType.Text, "and easy to do anywhere, even with C#");
        msg.AddTo(new EmailAddress("[email protected]", "Example User"));
        var response = await client.SendEmailAsync(msg).ConfigureAwait(false);
    }

    private static IServiceCollection ConfigureServices(IServiceCollection services)
    {
        services.AddSendGrid(options =>
        {
            options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
        });

        return services;
    }
}

Web Proxy

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

Or when using DependencyInjection

services.AddSendGrid(options =>
{
    options.ApiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
})
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
{
    Proxy = new WebProxy(new Uri("http://proxy:1337")),
    UseProxy = true
});

Usage

Use Cases

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

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 maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-csharp are trademarks of Twilio SendGrid, Inc.

Support

If you need help using SendGrid, please check the Twilio SendGrid Support Help Center.

License

The MIT License (MIT)

More Repositories

1

sendgrid-nodejs

The Official Twilio SendGrid Led, Community Driven Node.js API Library
JavaScript
2,885
star
2

sendgrid-python

The Official Twilio SendGrid Python API Library
Python
1,463
star
3

sendgrid-php

The Official Twilio SendGrid PHP API Library
PHP
1,445
star
4

sendgrid-go

The Official Twilio SendGrid Golang API Library
Go
924
star
5

email-templates

A repository of common email templates to use and modify to your heart's content.
HTML
802
star
6

sendgrid-ruby

The Official Twilio SendGrid Led, Community Driven Ruby API Library
Ruby
606
star
7

sendgrid-java

The Official Twilio SendGrid Led, Community Driven Java API Library
Java
475
star
8

docs

Repository of Twilio SendGrid's product documentation.
JavaScript
235
star
9

rest

SendGrid's Golang HTTP Client for calling APIs
Go
167
star
10

python-http-client

Twilio SendGrid's Python HTTP Client for calling APIs
Python
121
star
11

php-http-client

SendGrid's PHP HTTP Client for calling APIs
PHP
120
star
12

nodemailer-sendgrid-transport

SendGrid transport for Nodemailer
JavaScript
116
star
13

smtpapi-php

SendGrid's smtpapi library in PHP
PHP
67
star
14

krampus

The original AWS security enforcerβ„’
Python
58
star
15

eventkit-rails

An open source project for integrating with SendGrid's Event Webhook.
Ruby
52
star
16

wordpress

SendGrid plugin for WordPress
PHP
48
star
17

smtpapi-nodejs

SendGrid's smtpapi library in NodeJS
JavaScript
47
star
18

sendgrid-parse-api-example

Example application using the SendGrid Parse API.
JavaScript
45
star
19

sendgrid-subscription-widget

A new SendGrid subscription widget that works with existing infrastructure.
JavaScript
44
star
20

sendgrid-oai

SendGrid's Open API specification for the v3 API
Shell
42
star
21

go-solr

solr go client from sendgrid, zookeeper aware, incorporates retries
Go
39
star
22

sendgrid-apex

SendGrid (http://sendgrid.com) Apex helper library.
Apex
33
star
23

sendgrid-php-example

Example of using sendgrid-php library.
PHP
33
star
24

sendgrid-nodejs-example

Example of using sendgrid-nodejs library.
JavaScript
31
star
25

opensource

SendGrid Open Source Dashboard
31
star
26

smtpapi-go

SendGrids smtpapi library in Golang
Go
30
star
27

go-gmime

Go
27
star
28

smtpapi-csharp

SendGrid's smtpapi library in C#
C#
27
star
29

java-http-client

SendGrid's Java HTTP Client for calling APIs
Java
26
star
30

sendgrid-objc

SendGrid Objective-C helper library
Objective-C
25
star
31

csharp-http-client

Twilio SendGrid's C# HTTP Client for calling APIs
C#
23
star
32

ruby-http-client

SendGrid's Ruby HTTP Client for calling APIs
Ruby
23
star
33

ti.sendgrid

A SendGrid CommonJS module for Titanium
JavaScript
21
star
34

sendgrid-cli

The SendGrid CLI (Command Line Interface) is a tool that helps you to interact with SendGrid services from the command line.
JavaScript
20
star
35

nodejs-http-client

This repo is no longer maintained. Please use https://github.com/sendgrid/sendgrid-nodejs/tree/HEAD/packages/client instead
JavaScript
19
star
36

sendgrid-perl

Perl module for SendGrid's API
Perl
18
star
37

sendgrid-cobol

Official SendGrid library for Cobol
C
16
star
38

sendgridjs

Proxy so you can send email via JavaScript through SendGrid.
JavaScript
14
star
39

dx-automator

A tool for managing priorities across multiple GitHub repositories
HCL
14
star
40

sendgrid-parse-demo

A simple example of a parse webhook endpoint that receives an email, looks at the body for an integer, rolls that many dice, and replies to the requester.
PHP
13
star
41

open-source-library-data-collector

A tool for collecting open source download and github stats across multiple repositories
Python
12
star
42

filegetter

This package is an example implementation referenced in the blog post 'When Writing Unit Tests, Don't Use Mocks.'
Go
10
star
43

stopwatch

Benchmark your Go code with this utility for keeping track of timing information for different states.
Go
10
star
44

aws-env

PUBLIC REPO: Wrapper to export environment variables from AWS Parameter Store
Go
9
star
45

sendgrid-google-php

PHP
9
star
46

openshift-sendgrid-php

SendGrid integration for RedHat's OpenShift
PHP
8
star
47

google-python-sample-app

Python
8
star
48

sendgrid-google-java

Java
7
star
49

uptyped

Ruby
7
star
50

sendgrid-java-example

Example of using sendgrid-java library.
Java
6
star
51

sendgrid-engine-yard-ruby

Example Ruby app that shows how to use SendGrid on EngineYard
Ruby
5
star
52

dx-mobile

A Flutter powered iOS and Android app to help maintainers manage multiple repos on the go.
Dart
5
star
53

smtpapi-java

SendGrid's smtpapi library in Java
Java
5
star
54

sendgrid-java-sample-app

Java
4
star
55

smtpapi-objc

Simple Wrapper around SendGrid SMTPAPI Header
Objective-C
4
star
56

google-java-sample-app

Java
4
star
57

ui-developer-challenge

Ruby
3
star
58

tagtrics

Easy to use application metrics
Go
3
star
59

martini

Go
3
star
60

design-primitives

SendGrid design primitives
JavaScript
3
star
61

sendgrid-engine-yard-php

A sample app demonstrating how to integrate SendGrid with EngineYard through PHP.
PHP
3
star
62

sendgrid-azure-ios

iPhone app which lets a user enter a song into a request queue backed by a sample Azure app
Objective-C
3
star
63

google-php-sample-app

PHP
3
star
64

sendgrid-python-django-sample-app

Python
3
star
65

Gridium

A web automation framework for Selenium and NO Capybara
Ruby
2
star
66

box

Demo for BoxDev 2016
Python
2
star
67

sendgrid-rails-sample-app

Ruby
2
star
68

visual_studio_sdk_mail

2
star
69

Reputation

Python
2
star
70

visual_studio_sdk_sample_app

MSFT SDK for the sample app
C#
2
star
71

reseller-docs

1
star
72

sherlock

JavaScript
1
star
73

JusticeEngine

Python
1
star
74

library-automator

For automating v3 endpoints into our 7 libraries
C#
1
star
75

sendgrid-php-sample-app

sendgrid-php-sample-app
PHP
1
star
76

folly

A folly of a service that just chews up memory and CPU cycles
Go
1
star
77

lambda_cost

A Jupyter Notebook Comparing Lambda and EC2 Costs
Jupyter Notebook
1
star