What is SendGrid?
Site Link - https://sendgrid.com
SendGrid API helps in delivering your important emails over SMPT or HTTP. It does have its own server which is on the cloud and helps you integrate their durable service with less effort.
Requirements
- Visual Studio 2015.
- <packages>
- <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
- <package id="Sendgrid" version="9.9.0" targetFramework="net452" />
- <package id="System.Net.Http" version="4.3.3" targetFramework="net45" />
- </packages>
- Azure subscription to create SendGrid.
SendGrid Benefits
It provides 24/7 customer support. It also has click tracking mechanism, Spam Reports, and if your mail gets bounced back, SenGrid tracks that as well. You can set daily/monthly mailing functionality over SendGrid API just to track how many emails went well or bounced back. If ever SendGrid API is down, it puts those emails in queues and sends it again once it is up.
So, it works on customer satisfaction with huge support.
Working with SendGrid
We need to create a SendGrid account.
Log in to your Azure account and add a new SendGrid Email Delivery Resource.
- Click "New" to add a new resource.
- Perform the configuration like in the below image.
- Fill contact information.
- Accept the licence under the "Legal Terms" tab.
- Once you have successfully added SendGrid Resources, you will see a screen like the below one. Click on the "Manage" tab. It will redirect to the SendGrid portal.
- SendGrid Portal
- Here, you can configure/create an API key.
- Please save your key or take a screenshot of the same, otherwise, you will unable to find the key on a later stage.
- Let's create our template for the mail.
- Edit your template like below using different modules. These templates are compatible with your mobile devices as well.
- Save this Template ID.
And at last, you can add the below logic in your .NET application.
- static async Task sendEmailUsingSendGrid(string apiKey) {
- var client = new SendGridClient(apiKey);
-
- var msgs = new SendGridMessage() {
- From = new EmailAddress("[email protected]", "Pankaj Sapkal"),
- Subject = "Subject line",
- TemplateId = "fb09a5fb-8bc3-4183-b648-dc6d48axxxxx",
-
-
-
- };
-
- msgs.AddTo(new EmailAddress("[email protected]", "Pankaj Sapkal"));
-
- var attach = new Attachment();
-
- attach.Type = "image/png";
- attach.Filename = "hulk.png";
- attach.Disposition = "inline";
- attach.ContentId = "hulk2";
-
-
- msgs.SetFooterSetting(true, "<strong>Regards,</strong><b> Pankaj Sapkal", "Pankaj");
-
-
- var responses = await client.SendEmailAsync(msgs);
- }
Now, run the application and check for the sent mail.
Thanks a lot for giving your valuable time reading this article.