Menu

Auto email sender in ASP.NET with C#

In this blog we will learn: How to trigger email notification in ASP.NET with C# or automatically send email notification using gmail SMTP.
In this electronic era everyone wants a contact us page in their website and on that page people want a adaptive form where their users fill that form with required details and submit. Which may later use those details by admin of that website to contact that user. To create a adaptive form and on click on submit button admin will receive an email. 

first we will design our adaptive form in html, below is the sample.
<html>
<head>
    <title>Adaptive Form/Contact Us</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 247px; height: 105px">
            <tr>
                <td>
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    Email:</td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                    Message</td>
                <td>
                    <asp:TextBox ID="txtmes" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnsend" runat="server" Text="Submit" Width="89px" OnClick="btnsend_Click" /></td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Now on the click event of submit button we will write the following C# code.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
using System.Text;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnsend_Click(object sender, EventArgs e)
    {
       
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        try
        {
            // Gmail Address from where you send the mail
            string fromAddress = "from@gmail.com";
            // any address where the email will be sending
            string toAddress = "to@admin.com";
            //Password of your gmail address
            const string fromPassword = "Put your password here";
            // Passing the values and make a email formate to display
            string subject = "Request";
            string body = "From: " + txtname.Text + "\n";
            body += "Email: " + txtemail.Text + "\n";
            body += "Subject: " + txtmes.Text + "\n";
                        // smtp settings
            SmtpClient smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(fromAddress, toAddress, subject, body);
            Label1.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            Label1.Text = "Send Email Failed." + ex.Message;
        }
    }
}

Now debug the code and test the functionality 

No comments:

Post a Comment