Send Mail from an ASP.NET Web Form Website

Introduction


Hello guys, in this article, I will tell you how you can send an email from your ASP.NET web form to any other user using Gmail. I will also tell you how you can use the Gmail API to send mail. I hope you like this article.
 
Step 1
 
First of all, open your visual studio and create a new project by clicking on File > New > Project
 
Send Mail From ASP.NET Web Form Website
 
Step 2
 
Now select ASP.NET webform application from the various options
 
Send Mail From ASP.NET Web Form Website
 
Step 3
 
Now you will get a screen where you need to enter your project name, project location, solution name, and framework for your project. Select the framework as per your need.
 
Send Mail From ASP.NET Web Form Website
 
Step 4
 
Now add a new web form in your project by right-clicking on your project name in Solution Explorer > Add > Add New Item
 
Send Mail From ASP.NET Web Form Website
 
Step 5
 
Select web form from a various list of items, give your form name and click on the add button
 
Send Mail From ASP.NET Web Form Website
 
Step 6
 
Create a form as per your requirement. As you can see here, I created a basic form which contains only three text fields which receive an email address, mail subject, and mail message.
 
Send Mail From ASP.NET Web Form Website
 
Step 7
 
Add a namespace using System.Net.Mail
 
Step 8
 
Now add an event on a button click and add the following code as shown in the below image:
 
Send Mail From ASP.NET Web Form Website
 
Under Standing Code
  • MailMessage mail = new MailMessage() is create object of class which is In System.Net.Mail namespace which use for send mails
  • Mail.to is contain email address of receiver (here you can add multiple email addresses)
  • Mail.from is contain sender email address
  • Mail.body is for mail body which contain your message for mail
  • SmtpClient SMTP = new SmtpClient() is create object of SMTP services
  • smtp.host attribute tells the server that you use this as your mail host. Here, the user Gmail as a host you can define other as well
  • smtp.port is define port for you host 587 is port number of Gmail
  • smtp.UseDefaultCredentials: If this is set as true, is uses default Credentials which define in your host server. But if you need to use your email then you need to set it to false
  • smtp.Credentials is use to define your username and password to enable your server to login into your account
  • smtp.EnableSsl: if this is set false mail send with HTTP only and if it set with true mail send with HTTPS
  • smtp.Send(mail): it sends the mail  containing the above information
Step 9
 
Now run your website and try to send mail.
 
However, you will get an error as seen in the below image.
 
Send Mail From ASP.NET Web Form Website
 
This error comes when the server does not log in to your account. There are two main reasons for this.
  • The first reason is that your username or password is incorrect 
  • The second reason is your account does not allow access to other apps. for change this setting go to this https://myaccount.google.com/lesssecureapps?pli=1 and turn on the button to allow less secure apps, as seen in the below image.
Send Mail From ASP.NET Web Form Website 
Step 10
 
Now re-run your website and enter details and click on the send button.
Send Mail From ASP.NET Web Form Website 
 
Step 11
 
Now check your email, as you can see in the below image, the mail has been received successfully.
 
Send Mail From ASP.NET Web Form Website
 
Below is the source code for designing the file (.aspx file)
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendMail.aspx.cs" Inherits="SendMail" %>    
  2.     
  3. <!DOCTYPE html>    
  4.     
  5. <html xmlns="http://www.w3.org/1999/xhtml">    
  6. <head runat="server">    
  7.     <title></title>    
  8.     <link href="Content/bootstrap.min.css" rel="stylesheet" />    
  9.         
  10. </head>    
  11. <body>    
  12.     <br /><br /><br />    
  13.     
  14.                     <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>    
  15.     
  16.     <form id="form1" runat="server">    
  17.         <div class="card col-lg-6 col-sm-12 col-md-6">    
  18.             <div class="row">    
  19.                 <div class="col-sm-12 col-md-6 col-lg-6">    
  20.                     <asp:Label ID="Label1" runat="server" Text="Enter Receiver Email"></asp:Label>    
  21.                 </div>    
  22.                 <div class="col-sm-12 col-md-6 col-lg-6">    
  23.                     <asp:TextBox ID="txtto" CssClass="form-control" runat="server"></asp:TextBox>    
  24.                 </div>    
  25.             </div>    
  26.     
  27.             <div class="row">    
  28.                 <div class="col-sm-12 col-md-6 col-lg-6">    
  29.                     <asp:Label ID="Label2" runat="server" Text="Enter Subject"></asp:Label>    
  30.                 </div>    
  31.                 <div class="col-sm-12 col-md-6 col-lg-6">    
  32.                     <asp:TextBox ID="txtsub" CssClass="form-control" runat="server"></asp:TextBox>    
  33.                 </div>    
  34.             </div>    
  35.     
  36.             <div class="row">    
  37.                 <div class="col-sm-12 col-md-6 col-lg-6">    
  38.                     <asp:Label ID="Label3" runat="server" Text="Enter Message"></asp:Label>    
  39.                 </div>    
  40.                 <div class="col-sm-12 col-md-6 col-lg-6">    
  41.                     <textarea id="txtmsg" class="form-control" cols="20" rows="5" name="txtmsg"></textarea>    
  42.                 </div>    
  43.             </div>    
  44.             <div class="row text-center">    
  45.                 <asp:Button ID="Button1" runat="server" CssClass="btn btn-success" Text="Send Mail" OnClick="Button1_Click" />    
  46.             </div>    
  47.         </div>    
  48.     </form>    
  49.     
  50. <script src="Scripts/bootstrap.min.js"></script>    
  51. </body>    
  52. </html>   
Below is the source code for the back end file, (.aspx.cs file)
  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. using System.Net.Mail;  
  8.   
  9. public partial class SendMail : System.Web.UI.Page  
  10. {   
  11.   
  12.     protected void Button1_Click(object sender, EventArgs e)  
  13.     {  
  14.         try  
  15.         {  
  16.             MailMessage mail = new MailMessage();  
  17.             mail.To.Add(txtto.Text);  
  18.             mail.From = new MailAddress("Your Gmail Address");  
  19.             mail.Subject = txtsub.Text;  
  20.             mail.Body = Request.Form["txtmsg"];  
  21.             SmtpClient smtp = new SmtpClient();  
  22.             smtp.Host = "smtp.gmail.com";  
  23.             smtp.Port = 587;  
  24.             smtp.UseDefaultCredentials = false;  
  25.             smtp.Credentials = new System.Net.NetworkCredential("Your Gmail Address""Your Password");  
  26.             smtp.EnableSsl = true;  
  27.               
  28.             smtp.Send(mail);  
  29.             lblmsg.Text = "Mail Send .......";  
  30.         }  
  31.         catch (Exception ex)  
  32.         {  
  33.               
  34.         }  
  35.     }  
  36. }  
Thank you for reading this article. If you like the article, share with your friends.


Similar Articles