Overview
This guide demonstrates how to perform common programming tasks with theSendGrid email service on Azure. The samples are written in C#and supports .NET Standard 1.3. The scenarios covered include constructingemail, sending email, adding attachments, and enabling various mail andtracking settings. For more information on SendGrid and sending email, seethe Next steps section.
What is the SendGrid Email Service?
SendGrid is a cloud-based email service that provides reliabletransactional email delivery, scalability, and real-time analytics along with flexible APIsthat make custom integration easy. Common SendGrid use cases include:
SendGrid API Integrate in minutes with our email API and trust your emails reach the inbox. SendGrid Marketing Campaigns Build meaningful connections with smart email marketing. Over 80,000 paying customers trust SendGrid to send more than 90 billion emails every month. Delivering your transactional and marketing email through one reliable platform.
- Automatically sending receipts or purchase confirmations to customers.
- Administering distribution lists for sending customers monthly fliers and promotions.
- Collecting real-time metrics for things like blocked email and customer engagement.
- Forwarding customer inquiries.
- Processing incoming emails.
For more information, visit https://sendgrid.com orSendGrid's C# library GitHub repo.
Create a SendGrid Account
To sign up for a SendGrid account
Sign in to the Azure portal.
In the Azure portal menu or the home page, select Create a resource.
Search for and select SendGrid.
Complete the signup form and select Create.
Enter a Name to identify your SendGrid service in your Azure settings. Names must be between 1 and 100 characters in length and contain only alphanumeric characters, dashes, dots, and underscores. The name must be unique in your list of subscribed Azure Store Items.
Enter and confirm your Password.
Choose your Subscription.
Create a new Resource group or use an existing one.
In the Pricing tier section select the SendGrid plan you want to sign up for.
Enter a Promotion Code if you have one.
Enter your Contact Information.
Review and accept the Legal terms.
After confirming your purchase you will see a Deployment Succeeded pop-up and you will see your account listed.
After you have completed your purchase and clicked the Manage button to initiate the email verification process, you will receive an email from SendGrid asking you to verify your account. If you do not receive this email, or have problems verifying your account, please see our FAQ.
You can only send up to 100 emails/day until you have verified your account.
To modify your subscription plan or see the SendGrid contact settings, click the name of your SendGrid service to open the SendGrid Marketplace dashboard.
To send an email using SendGrid, you must supply your API Key.
To find your SendGrid API Key
Click Manage.
In your SendGrid dashboard, select Settings and then API Keys in the menu on the left.
Click the Create API Key.
At a minimum, provide the Name of this key and provide full access to Mail Send and select Save.
Your API will be displayed at this point one time. Please be sure to store it safely.
To find your SendGrid credentials
Click the key icon to find your Username.
The password is the one you chose at setup. You can select Change password or Reset password to make any changes.
To manage your email deliverability settings, click the Manage button. This will redirect to your SendGrid dashboard.
For more information on sending email through SendGrid, visit the Email API Overview.
Reference the SendGrid .NET Class Library
The SendGrid NuGet package is the easiest way to get the SendGrid API and to configure your application with all dependencies. NuGet is a Visual Studio extension included with Microsoft Visual Studio 2015 and above that makes it easy to install and update libraries and tools.
Note
To install NuGet if you are running a version of Visual Studio earlier than Visual Studio 2015, visit https://www.nuget.org, and click the Install NuGet button.
To install the SendGrid NuGet package in your application, do the following:
Click on New Project and select a Template.
In Solution Explorer, right-click References, then clickManage NuGet Packages.
Search for SendGrid and select the SendGrid item in theresults list.
Select the latest stable version of the Nuget package from the version dropdown to be able to work with the object model and APIs demonstrated in this article.
Click Install to complete the installation, and then close thisdialog.
SendGrid's .NET class library is called SendGrid. It contains the following namespaces:
- SendGrid for communicating with SendGrid's API.
- SendGrid.Helpers.Mail for helper methods to easily create SendGridMessage objects that specify how to send emails.
Add the following code namespace declarations to the top of any C# file in which you want to programmatically access the SendGrid email service.
How to: Create an Email
Use the SendGridMessage object to create an email message. Once the message object is created, you can set properties and methods, including the email sender, the email recipient, and the subject and body of the email.
The following example demonstrates how to create a fully populated email object:
For more information on all properties and methods supported by theSendGrid type, see sendgrid-csharp on GitHub.
How to: Send an Email
After creating an email message, you can send it using SendGrid's API. Alternatively, you may use .NET's built in library.
Sending email requires that you supply your SendGrid API Key. If you need details about how to configure API Keys, please visit SendGrid's API Keys documentation.
You may store these credentials via your Azure portal by clicking Application settings and adding the key/value pairs under App settings.
Then, you may access them as follows:
The following examples show how to send an email message using the SendGrid Web API with a console application.
How to: Send email from ASP .NET Core API using MailHelper class
The below example can be used to send a single email to multiple persons from the ASP .NET Core API using the MailHelper
class of SendGrid.Helpers.Mail
namespace. For this example we are using ASP .NET Core 1.0.
In this example, the API key has been stored in the appsettings.json
file which can be overridden from the Azure portal as shown in the above examples.
The contents of appsettings.json
file should look similar to:
First, we need to add the below code in the Startup.cs
file of the .NET Core API project. This is required so that we can access the SENDGRID_API_KEY
from the appsettings.json
file by using dependency injection in the API controller. The IConfiguration
interface can be injected at the constructor of the controller after adding it in the ConfigureServices
method below. The content of Startup.cs
file looks like the following after adding the required code:
At the controller, after injecting the IConfiguration
interface, we can use the CreateSingleEmailToMultipleRecipients
method of the MailHelper
class to send a single email to multiple recipients. The method accepts one additional boolean parameter named showAllRecipients
. This parameter can be used to control whether email recipients will be able to see each others email address in the To section of email header. The sample code for controller should be like below
How to: Add an attachment
Attachments can be added to a message by calling the AddAttachment method and minimally specifying the file name and Base64 encoded content you want to attach. You can include multiple attachments by calling this method once for each file you wish to attach or by using the AddAttachments method. The following example demonstrates adding an attachment to a message:
How to: Use mail settings to enable footers, tracking, and analytics
SendGrid provides additional email functionality through the use of mail settings and tracking settings. These settings can be added to an email message to enable specific functionality such as click tracking, Google analytics, subscription tracking, and so on. For a full list of apps, see the Settings Documentation.
Apps can be applied to SendGrid email messages using methods implemented as part of the SendGridMessage class. The following examples demonstrate the footer and click tracking filters:
The following examples demonstrate the footer and click trackingfilters:
Click tracking
How to: Use Additional SendGrid Services
SendGrid offers several APIs and webhooks that you can use to leverage additional functionality within your Azure application. For more details, see the SendGrid API Reference.
Next steps
Now that you've learned the basics of the SendGrid Email service, followthese links to learn more.
- SendGrid C# library repo: sendgrid-csharp
- SendGrid API documentation: https://sendgrid.com/docs
10,000 emails /month | 125,000 emails /month | 700,000 emails /month | |
---|---|---|---|
Annual Savings | $59 /year | - | - |
PostmarkIncludes great deliverability and support as standard | $10 /mo. | $100 /mo. | $400 /mo. |
SendGridEssentials Plan | $14.95 /mo. | - | - |
Pro Plan | - | $79.95 /mo. | $399.95 /mo. |
Do you offer a free trial?
All new accounts start off on our free developer plan with 100 emails per month. Use it for however long you need, it doesn’t expire.
Do you offer dedicated IPs?
Dedicated IPs are available to high-volume senders. Read about our policy for issuing dedicated IPs.
What happens if I go over my plan’s monthly email limit?
Extra emails on our paid plans are calculated at the end of your billing cycle and included with your plan cost for the upcoming month.
What payment types do you accept?
We accept payments via Visa, MasterCard, American Express, Diners, and JCB. We do not accept PayPal or Purchase Orders.
Can I send bulk marketing email through Postmark?
Yes you can! To comply with industry best-practices and to maintain our high deliverability, it's important though that transactional and marketing emails never mix. That's why we created Message Streams, a parallel but completely separate sending infrastructures for your transactional and broadcast emails.
Do you offer high-volume plans?
Sendgrid 2fa 1password Code
Yes! Contact us to learn more about our high-volume pricing.