Saturday, 10 September 2016

How to Send Email Using SendGrid with Azure

 Hello everyone, I am sharing  the steps and code for Send Email Using SendGrid with Azure using the ASP.Net C#.

Step-1 For the Azure customer first create Send Grid Account

Login to azure portal and then select In the Choose an Application and Service dialog, select SendGrid and click the right arrow and follow the defined step t create send grid account

Step-2 Add SendGrid into Application

  • In Visual Studio Solution Explorer, right-click References, then click Manage NuGet Packages.
  • Search for SendGrid and select the SendGrid item in the results list.
  • Click Install to complete the installation, and then close this dialog.
SendGrid's .NET class library is called SendGridMail. It contains the following namespaces:
  • SendGridMail for creating and working with email items.
  • SendGridMail.Transport for sending email using either the SMTP protocol, or the HTTP 1.1 protocol with Web/REST
Step-3  Send an Email

Use below Namespaces for sendgrid

using System;
using System.Net;
using System.Net.Mail;

using SendGrid;

//Azure Email  
public static Task SendMailAsync(string To, string Subject, string Body)
                var username = "your_sendgrid_username";
                var pswd = "your_sendgrid_password";
                var credentials = new NetworkCredential(username, pswd);
                SendGridMessage myMessage = new SendGridMessage();
                List<String> recipients = new List<String>();

                if (To != "")
                    if (To.Contains(","))
                        string[] EmailTo = To.Split(',');
                        for (int i = 0; i < EmailTo.Length; i++)
                            if (!string.IsNullOrEmpty(EmailTo[i]))
                myMessage.From = new MailAddress(MailSentFrom);
                myMessage.Subject = Subject;
                myMessage.Html = Body;
                // Create an Web transport for sending email.
                var transportWeb = new SendGrid.Web(credentials);
                // Send the email.
                // You can also use the **DeliverAsync** method, which returns an awaitable task.
                return transportWeb.DeliverAsync(myMessage);

No comments:

Post a Comment

Note: only a member of this blog may post a comment.