Integrate send grid asp.net c# in 5 minutes

In this blog post I am going to explain about how to integrate send grid in asp.net mvc within just 5 minutes. Before integrate you should have to sign up at send grid. Once you sign up then you can use send grid username and password to send emails using send grid.

Before integrate the code you need to add send grid dll reference into your project. If you are using visual studio then you can simply add by using nudget command.

Install-Package Sendgrid

Once send grid reference is added now set some settings in you web.config file regarding the send grid details. See below details.

  <!--send grid email details-->
    <add key="sendGridSmtpServer" value="smtp.sendgrid.net" />
    <add key="EnableSsl" value="true" />
    <add key="sendGridPort" value="587" />
    <add key="sendGridFrom" value="your email" />
    <add key="sendGridUser" value="username" />
    <add key="sendGridPassword" value="password" />

Now you have to add send email code. See below :-

                        /*
                         * Send email section
                         */
                        //Send Email
                        var mail = new MailMessage();
                        var smtpServer = new SmtpClient(ConfigurationManager.AppSettings["sendGridSmtpServer"]);
                        mail.From = new MailAddress(ConfigurationManager.AppSettings["sendGridFrom"]);

                        mail.To.Add("to email here");                       
                        smtpServer.Port = Convert.ToInt32(ConfigurationManager.AppSettings["sendGridPort"]);
                        smtpServer.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["sendGridUser"], ConfigurationManager.AppSettings["sendGridPassword"]);
                        smtpServer.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);

                        mail.Subject = "Subject here";
                        mail.Body = "Body message here";
                        mail.IsBodyHtml = true;

                        smtpServer.Send(mail);
             

Hope it will save your time and efforts. Don’t forgot to share and comment if it save your time and efforts.

Posted by | View Post | View Group