Send Email to Gmail in ASP.Net Using C#

We took two TextBoxes, one for the To email address and the other for making comments or any information you want to send. We used one label too that shows that the email is sent to the desires location.

Initial chamber

Step 1

Open Your Visual Studio 2010 and create an Empty Website, provide a suitable name (Email_demo).

Step 2

In Solution Explorer you get your empty website, add a web form by going as in the following.

For Web Form:

Email_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it Email_demo.aspx.

Design chamber

Next we will create a design for our application where we drag some control from the toolbox. So open your Email.aspx page and write code like this:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             width: 106px;  
  12.         }  
  13.         .style2  
  14.         {  
  15.             text-decoration: underline;  
  16.             font-size: large;  
  17.             width: 257px;  
  18.         }  
  19.         .style3  
  20.         {  
  21.             width: 257px;  
  22.         }  
  23.     </style>  
  24. </head>  
  25. <body>  
  26.     <form id="form1" runat="server">  
  27.     <div>  
  28.       
  29.         <table style="width:100%;">  
  30.             <tr>  
  31.                 <td class="style1">  
  32.                      </td>  
  33.                 <td class="style2">  
  34.                     <strong>Email Sytem using asp.net C#</strong></td>  
  35.                 <td>  
  36.                      </td>  
  37.             </tr>  
  38.             <tr>  
  39.                 <td class="style1">  
  40.                      </td>  
  41.                 <td class="style3">  
  42.                      </td>  
  43.                 <td>  
  44.                      </td>  
  45.             </tr>  
  46.             <tr>  
  47.                 <td class="style1">  
  48.                     Email:</td>  
  49.                 <td class="style3">  
  50.                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  51.                 </td>  
  52.                 <td>  
  53.                     <asp:RequiredFieldValidator ID="RequiredFieldValidator1"   
  54.                         ControlToValidate="TextBox1" runat="server"   
  55.                         ErrorMessage="Please Provide Email ID" Font-Bold="True" ForeColor="Red"></asp:RequiredFieldValidator>  
  56.                   </td>  
  57.             </tr>  
  58.             <tr>  
  59.                 <td class="style1">  
  60.                     Comment:</td>  
  61.                 <td class="style3">  
  62.                     <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>  
  63.                 </td>  
  64.                 <td>  
  65.                      </td>  
  66.             </tr>  
  67.             <tr>  
  68.                 <td class="style1">  
  69.                      </td>  
  70.                 <td class="style3">  
  71.                      </td>  
  72.                 <td>  
  73.                      </td>  
  74.             </tr>  
  75.             <tr>  
  76.                 <td class="style1">  
  77.                     <asp:Label ID="lbmsg" runat="server"></asp:Label>  
  78.                 </td>  
  79.                 <td class="style3">  
  80.                     <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
  81.                         Text="Send Mail" />  
  82.                 </td>  
  83.                 <td>  
  84.                      </td>  
  85.             </tr>  
  86.         </table>  
  87.       
  88.     </div>  
  89.     </form>  
  90. </body>  
  91. </html>  
Your design will look as in the following:

design

This are the two TextBoxes wherein the first TextBox you need to enter the Email ID of the user whom you want to send the mail to and in the other TextBox you can put your information, then click on the button. That's it, you have sent the mail.

Code chamber

Finally open your Email_demo.aspx.cs page where we write our server-side code so that the mail is sent to the desired user. But before that, look at the namespaces that we will include.

namespace

You need to include these two namespaces in your application and anyhow if you can't find these namespaces in the intellisence then add them from the References.

add reference

Here is your code for Email_demo – Email_demo.aspx.cs.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. using System.Net;  
  9. using System.Net.Mail;  
  10.   
  11.   
  12. public partial class _Default : System.Web.UI.Page  
  13. {  
  14.     protected void Page_Load(object sender, EventArgs e)  
  15.     {  
  16.   
  17.     }    
  18.     protected void Button1_Click(object sender, EventArgs e)  
  19.     {  
  20.         MailMessage msg = new MailMessage();  
  21.         msg.From = new MailAddress("[email protected]");  
  22.         msg.To.Add(TextBox1.Text);  
  23.         msg.Subject = TextBox2.Text;  
  24.   
  25.         SmtpClient smt = new SmtpClient();  
  26.         smt.Host = "smtp.gmail.com";  
  27.         System.Net.NetworkCredential ntwd = new NetworkCredential();  
  28.         ntwd.UserName = "[email protected]"//Your Email ID  
  29.         ntwd.Password = ""// Your Password  
  30.         smt.UseDefaultCredentials = true;  
  31.         smt.Credentials = ntwd;  
  32.         smt.Port = 587;  
  33.         smt.EnableSsl = true;  
  34.         smt.Send(msg);  
  35.         lbmsg.Text = "Email Sent Successfully";  
  36.         lbmsg.ForeColor = System.Drawing.Color.ForestGreen;  
  37.   
  38.     }  
  39. }  
Output chamber

Output

There is a Required Field Validator in the first TextBox, so if you leave the first TextBox then an error will be shown as in the following image.

provide email

send mail

We will check for the mail. Look at the following image.

check in my mail

I haven't included any body part here, this is the only subject part so if you want to add any body part then for that just use one text box and make the text mode multiline. In the sever side you need to provide the following two lines of code:

code

I hope you like it. Thank you for reading. Have a good day.

Up Next
    Ebook Download
    View all
    Learn
    View all