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
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.
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. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <link href="Content/bootstrap.min.css" rel="stylesheet" />
  7. </head>
  8. <body>
  9. <br /><br /><br />
  10. <asp:Label ID="lblmsg" runat="server" Text=""></asp:Label>
  11. <form id="form1" runat="server">
  12. <div class="card col-lg-6 col-sm-12 col-md-6">
  13. <div class="row">
  14. <div class="col-sm-12 col-md-6 col-lg-6">
  15. <asp:Label ID="Label1" runat="server" Text="Enter Receiver Email"></asp:Label>
  16. </div>
  17. <div class="col-sm-12 col-md-6 col-lg-6">
  18. <asp:TextBox ID="txtto" CssClass="form-control" runat="server"></asp:TextBox>
  19. </div>
  20. </div>
  21. <div class="row">
  22. <div class="col-sm-12 col-md-6 col-lg-6">
  23. <asp:Label ID="Label2" runat="server" Text="Enter Subject"></asp:Label>
  24. </div>
  25. <div class="col-sm-12 col-md-6 col-lg-6">
  26. <asp:TextBox ID="txtsub" CssClass="form-control" runat="server"></asp:TextBox>
  27. </div>
  28. </div>
  29. <div class="row">
  30. <div class="col-sm-12 col-md-6 col-lg-6">
  31. <asp:Label ID="Label3" runat="server" Text="Enter Message"></asp:Label>
  32. </div>
  33. <div class="col-sm-12 col-md-6 col-lg-6">
  34. <textarea id="txtmsg" class="form-control" cols="20" rows="5" name="txtmsg"></textarea>
  35. </div>
  36. </div>
  37. <div class="row text-center">
  38. <asp:Button ID="Button1" runat="server" CssClass="btn btn-success" Text="Send Mail" OnClick="Button1_Click" />
  39. </div>
  40. </div>
  41. </form>
  42. <script src="Scripts/bootstrap.min.js"></script>
  43. </body>
  44. </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. public partial class SendMail : System.Web.UI.Page
  9. {
  10. protected void Button1_Click(object sender, EventArgs e)
  11. {
  12. try
  13. {
  14. MailMessage mail = new MailMessage();
  15. mail.To.Add(txtto.Text);
  16. mail.From = new MailAddress("Your Gmail Address");
  17. mail.Subject = txtsub.Text;
  18. mail.Body = Request.Form["txtmsg"];
  19. SmtpClient smtp = new SmtpClient();
  20. smtp.Host = "smtp.gmail.com";
  21. smtp.Port = 587;
  22. smtp.UseDefaultCredentials = false;
  23. smtp.Credentials = new System.Net.NetworkCredential("Your Gmail Address", "Your Password");
  24. smtp.EnableSsl = true;
  25. smtp.Send(mail);
  26. lblmsg.Text = "Mail Send .......";
  27. }
  28. catch (Exception ex)
  29. {
  30. }
  31. }
  32. }
Thank you for reading this article. If you like the article, share with your friends.