Skip to main content

send mail through gmail in ASP.NET

Here is code

1) Create one Visual studio project name as SendmailApplication .
2) open you default.aspx (designer page). and write down below code

Default.aspx page:
 ================================================================
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID ="Sender" runat="server" AssociatedControlID="txtSender" 
Text="From :" Width="600"></asp:Label>
      <asp:TextBox ID="txtSender" runat ="server" Width="200px" 
            style="position: relative; top: 0px; left: 90px" ></asp:TextBox><br />
      <br />
      <asp:Label ID="receiver" runat="server" AssociatedControlID="txtreceiver" 
Text="To :"></asp:Label>
      <asp:TextBox ID ="txtreceiver" runat="server" 
            style="position: relative; top: 0px; left: 110px; width: 214px">
</asp:TextBox><br />
       <br />
      <asp:Label ID="subject" runat="server" AssociatedControlID="txtsubject"
 Text="Subject :"></asp:Label>
                  style="position: relative; top: 0px; left: 70px; width: 484px;">
</asp:TextBox><br />
       <br />
       <asp:Label ID="msgbody" runat="server" AssociatedControlID="txtmsgbody" 
            Text="Body :" style="position: relative"></asp:Label>
      <asp:TextBox ID ="txtmsgbody" runat="server" Height="93px" 
TextMode="MultiLine" 
            style="position: relative; top: 0px; left: 80px; width: 484px">
</asp:TextBox><br />
             <br />
      <asp:Button ID="btnsend" runat="server" Text="Send" 
onclick="btnsend_Click" 
            style="position: relative; top: 0px; left: 450px" />
     
    </div>
    </form>
</body>
</html>

=========================================================================
Use following Namespaces  to send mail 
         Using System.Net;
         Using System.Net.Mail;

create message format and body.
                       paramenter  - Sender and receiver ( from,To)
Assign body and subject to mailmessage instance .
Create instance of SmtpClient assign host - smtp.gmail.com
    and port - 587 
Provide credential of your domain and send mail .
& paste below code on click event of button.
Default.aspx.cs page : 
===========================================================================
protected void btnsend_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage(new 
MailAddress(txtSender.Text), new MailAddress(txtreceiver.Text));
                mail.Subject = txtsubject.Text;
                mail.Body = txtmsgbody.Text;
                SmtpClient client = new SmtpClient();
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.Credentials = new 
NetworkCredential("abc@gmail.com", "pwd***");
                client.EnableSsl = true;
                client.Send(mail);
            }
            catch (Exception ex)
            {

            }

        }
====================================================================================== 
Hope this will help you........

Regards,
Gajanan

Comments

Popular posts from this blog

Asp.Net Web API

What is ASP.NET Web API? ASP.NET Web API is a framework provided by the Microsoft with which we can easily build HTTP services that can reach a broad of clients, including browsers, mobile, IoT devices, etc. ASP.NET Web API provides an ideal platform for building RESTful applications on the .NET Framework.   Difference between ASP.NET Web API and WCF Web AP I is a Framework to build HTTP Services that can reach a board of clients, including browsers, mobile, IoT Devices, etc. and provided an ideal platform for building RESTful applications. It is limited to HTTP based services. ASP.NET framework ships out with the .NET framework and is Open Source. WCF i.e. Windows Communication Foundation is a framework used for building Service Oriented applications (SOA) and supports multiple transport protocol like HTTP, TCP, MSMQ, etc. It supports multiple protocols like HTTP, TCP, Named Pipes, MSMQ, etc. WCF ships out with the .NET Framework. Both Web API and WCF can be self-hosted or can be...

Creating package in Oracle Database using Toad For Oracle

What are Packages in Oracle Database A package is  a group   of procedures, functions,  variables   and  SQL statements   created as a single unit. It is used to store together related objects. A package has two parts, Package  Specification  and Package Body.