Sending Email with Attachment using SendGrid Email Service

 Introduction
 
This article will walk you through the creation of an ASP.NET Core MVC web application with C# language. It will also cover sending an email using SendGrid Email Service.
 
Send Email Using SendGrid Email Service With An Attachment
Photo by Austin Distel on Unsplash
 
In a modern web application, email notification or verification has now become a major and important part of the application. Using email service, we can ensure application user registration, password reset, mobile number verification, etc. This helps the verification process go smoothly, quickly and securely. Newsletters, Promotion and Shipping Notifications tasks can also be completed with email service as well. Therefore in any application, we can make sure to use email service.
 
In this article, I will explain how to send an email from ASP.NET Core MVC web application using Sendgrid email service. I will write a Sendgrid email service as well to show how to send an email using C# language and the Sendgrid nuget package manager.
 
Prerequisites
 
Good knowledge of C# language, ASP.NET Core MVC application and .NET CLI.
 

Background

 
Let’s move to this demonstration and create an ASP.NET Core MVC web application. Before moving to the practical demonstration, let’s understand the objective of this demonstration which tells what exactly will be covered in this article. Here is the objective as follows,
  1. Create a SendGrid account
  2. Get an API key from the created SendGrid account 
  3. Create a project
  4. Code implementation
  5. Conclusion

Create a SendGrid Account

 
Sendgrid is a cloud-based email service that ensures reliable email delivery, scalability, and real-time service analysis using the SendGrid application dashboard. SendGrid API helps in delivering your important emails over SMPT or HTTP.

You can create a free account from Sendgrid website.
 
Send Email Using SendGrid Email Service With An Attachment 
 
After successfully creating the SendGrid account, you can create the SendGrid email service API key which is required in the application implementation.
 
SendGrid Dashboard: Create or get API Key from here.
 
Send Email Using SendGrid Email Service With An Attachment 
 

Plan and Pricing

 
You can send 40,000 emails for the first 30 days, then 100/day forever. For more details, please visit the Sendgrid website, here.
 
Send Email Using SendGrid Email Service With An Attachment 
 

Advantage of SendGrid

  1. 24-hour customer support
  2. Using dashboard, you can track Spam Reports, mail bounced back and other features as well.
  3. You can set daily/monthly mailing functionality.
  4. If the server down, you can put email service in queues.

Implementation

 
Let’s first create an ASP.NET Core MVC web Application from .NET Core CLI.
  • $ dotnet new sln -n SendGridDotNetCore
  • $ dotnet new mvc -n SendGridDotNetCore
  • $ dotnet sln SendGridDotNetCore.sln add SendGridDotNetCore/SendGridDotNetCore.csproj

API Key and Email Info

 I put the API key and send email related all info to the ASP.NET resource file so that it can access and edit in real-time execution.
 
Send Email Using SendGrid Email Service With An Attachment 
 
Build and Run
  • $ dotnet build
  • $ dotnet run
  • $ dotnet watch run
Publish and run
  • $ dotnet publish -o ./publish
  • $ dotnet AutoMapperDemo.dll
Here the application UI is showing the email delivery details status.
 
Send Email Using SendGrid Email Service With An Attachment 
 

Email Service Interface

 
Startup
 
services.AddTransient<ISendGridEmailSender, SendGridEmailSender>();
 

Conclusion

 
Using SendGrid email service, C# nuget package manager and ASP.NET Core MVC web application, we can now simply send an email from the application. Additionally, you can add an attachment to the email and the SendGrid email service will send it by attaching in the email body as well!
 
Cheers!
 
Github Repo
 
https://github.com/csharplang/Sendgrid
 
Reference
  • https://sendgrid.com/solutions/email-api/
  • https://github.com/sendgrid/sendgrid-csharp
  • https://www.nuget.org/packages/Sendgrid/