1 / 4
2 / 4
3 / 4
4 / 4

Sunday 7 June 2020

Send Email using SendGrid in Asp.Net C# (.Net Framework)


As we know we can send Emails from Google SMTP server for free, but we can use other SMTP service providers like Sendinblue, Amazon SES, GSuit, SendGrid, Mailgun, etc. In this blog, we are going to discuss Sending Email using the SendGrid SMTP service provider.



First of all, we have to register in SendGrid.

  1. Open website https://sendgrid.com/
  2. Click on the "Try for free"/"Start for free" button.
  3. using a valid email address register with any password(this will be your login mail id and password for SendGrid)
  4. Then you have to give your and company details. after filling up the form it will take you to the dashboard.
  5. Sender Authentication; for now, we are using SendGrid for beginners(not for professionals), so we are going to test the service provider that how and what settings we have to do to use SendGrid SMTP.  So for authentication follow these steps:- Settings ->Sender Authentication->fill up the form with a valid Email Id* (sender Email id)-> Create
* you must have to give the valid email id so that you can verify it when the website will ask while filling up about email id I will tell "Attempting to send from a free email address domain like gmail.com is not recommended"; i.e. it Gmail Smtp is free, we don't need to use any other SMTP for Gmail.its only an alert for us, we can use Gmail for the testing process.


Now open Visual Studio

  1. Now create a new ASP .Net Framework project named "MailHere".
  2. Add a webform "MailPage".

 In "MailPage.aspx" code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mailpage.aspx.cs" Inherits="MailHere.Mailpage" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Mail Here</title>
    <!--bootstrap CDN-->
    <link crossorigin="anonymous" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" rel="stylesheet"></link>
    <style>
        form{
            padding:0 550px;
            border:1px solid orange;
        }
        a{
            color:orange;
            padding:14px 0;
        }
        h2{
            color:#72b02c;
            padding:14px 0;
           text-align:center;
        }
         p{
            padding:14px 0;
           text-align:center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
     <h2 style="align-content: center;">Mail Here</h2>
     powered by <a href="https://www.blogger.com/www.mypalmbook.com"> My Palm Book</a>
     <div class="container">
      <asp:textbox cssclass="form-control" id="txtTo" placeholder="To" runat="server"></asp:textbox>
      <asp:textbox cssclass="form-control" id="txtSubject" placeholder="Subject" runat="server"></asp:textbox>
      <asp:textbox cssclass="form-control" height="200px" id="txtContent" placeholder="Your Message" runat="server" textmode="MultiLine" width="300px"></asp:textbox>
      <asp:button cssclass="btn btn-block btn-success" id="btnSendGrid" onclick="btnSendGrid_Click" runat="server" text="Send"></asp:button>
     </div>
   </form>
</body>
</html>


 In "MailPage.aspx.cs" code

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

    protected void btnSendGrid_Click(object sender, EventArgs e)
        {
            //Mail Settings
            MailMessage mailMsg = new MailMessage("mypalmbook@gmail.com", txtTo.Text);//the verified sender email that you have registered in my account
            
            //Message Settings
            mailMsg.Subject = txtSubject.Text;    //any default text or data from our textbox
            mailMsg.Body = txtContent.Text;      //any default text or data from our textbox

            //SMTP Settings
            SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net",Convert.ToInt32(587));
            NetworkCredential credentials = new NetworkCredential("registeremail@gmail.com","yourpassword"); //the mail id and password that we have register to log in the sendgrid 
            smtpClient.Credentials = credentials;
            if (mailMsg != null)
            {
                smtpClient.Send(mailMsg);
                Response.Write("<script>alert('Mail Sent!')</script>");
            }
        }

 In "web.config" code


<system.net>
    <mailSettings>
      <smtp from="anyvalidemailid@gmail.com">
        <network host="smtp.sendgrid.net" port="587" />
      </smtp>
    </mailSettings>
  </system.net>


Now run the code and see the output and check the mail inbox.

Video tutorial







No comments:

Post a Comment

If you have any doubts please let me know