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

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.

Resolving 'Setup Account Privileges' error while installing SQL Server

A new installation of Microsoft SQL Server 2012 or Microsoft SQL Server 2008 R2 fails You see the following error message when you try to install a new instance of SQL Server 2012 or SQL Server 2008 R2: Rule "Setup account privileges" failed.

Creating Oracle stored Procedures using TOAD for Oracle

In a database management system, a  stored procedure  is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs. The use of  stored procedures  can be helpful in controlling  access to data, preserving  data integrity  and  improving  productivity.