How to send mail from ASP.NET Application

By
Hi Friend,Today I am going to show you "How to send mail from ASP.NET Application".You can easily send the mail through your gmail account or other account to another account (  yahoo ,outlook, reddiffmail etc).In this application you can easily send mail without opening your gmail account or other account.First i will used a simple ASP.NET Application for  sending the mail. After that i will tell you how to use this mail service in our ASP.NET Website . It is secure because you are sending the mail through gmail account or other account.In this application ASP.NET 's Variable (object) holds your credential Information and pass to your mail service provider.When your mail server will validate then your message will be delivered successfully to the correct destination.

There are some steps to make this application which are given below:
Step 1:First open your visual studio --> File -->New -->ASP.NET Empty website --> open your solution Explorer -->Add a New Web Form -->Drag and drop Label, Button and TextBox Control on the Web Form as shown below:
send mail

Step 2: Now Double click on Send Button and Write the following codes which are  given below:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;

public partial class sendmail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient smtpc = new SmtpClient("smtp.gmail.com");
        smtpc.Port = 587;
        smtpc.EnableSsl = true;
        smtpc.UseDefaultCredentials = false;
        smtpc.Credentials = new NetworkCredential(TextBox1.Text, TextBox2.Text);
        MailMessage email = new MailMessage(TextBox1.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text);
        try
        {
            smtpc.Send(email);
            Label1.Text = "Message has been successfully send";
        }
        catch
        {
       Label1.Text= "server authentication is faild,check your id and password";
        }
    }
}


Description:- In above example,First i have include two Namespace to use the mail service in ASP.NET Application.
  • using System.Net.Mail;
  • using System.Net;
After that i have created a Smtpclient class object (smtpc) and passed the Smtp server name and port Number  to the Smtpclient. After that  i have passed the our credential information to the Network through Smtpclient object(smtpc). After that i have created a MailMessage class object(email) and passed sender mail address,  destination mail address,subject,message body to the MailMessage class. After that send the mail message through Smtpclient object(smtpc) as shown in  above codes.

Step 3: Now Run the Program (press F5)-->Fill the required fields as shown below -->press Send button.
Output:
form details
Step 4: Now Open Your Mail Box (destination) ,you will see that your mail has received in mail Box  successfully as shown below:

output

Step 5: If you will Enter incorrect ID or password then this Application will give an Error message on the screen as sown below:
authentication failed


How to use Above Email service in ASP.NET Registration Page

You will have to seen that many website are using mail service on your website.When any user does register  on the website then website automatically send a mail to your email address.In tutorial ,i will show you how to a send mail ,when any user will register on your website.For implementation of this concept ,i will use our Previous Registration Page 
There are some steps to implement mail service in ASP.NET website.which are given below:
 Step 1First open your visual studio --> File -->New -->ASP.NET Empty website --> open your solution Explorer -->Add a New Web Form -->Drag and drop Label, Button and TextBox ,Validation Control on the Web Form as shown below:


form

 Step 2: Now Double click on Submit Button and Write the following codes which are give below:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {  
        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select*from regform where username='" + TextBox1.Text + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.Read())
        {
            Label1.Text = "User Name is Already Exist";
            this.Label1.ForeColor = Color.Red;
        }
        else
        {
            Label1.Text = "UserName is Available";
            this.Label1.ForeColor = Color.Red;
        }
        con.Close();
    }
    protected void  Button1_Click(object sender, EventArgs e)
{
    captcha1.ValidateCaptcha(TextBox6.Text.Trim());
    if (captcha1.UserValidated)
    {
        //you can use disconnected architecture also,here i have used connected architecture.

        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        String str = "insert into regform values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.ExecuteNonQuery();
        Session["name"] = TextBox1.Text;
        //Mail sending codes...
        SmtpClient smtpc = new SmtpClient("smtp.gmail.com");
        smtpc.Port = 587;
        smtpc.EnableSsl = true;
        smtpc.UseDefaultCredentials = false;
        Label1.Text = "aryasamaj8@gmail.com"; //<--Enter your gmail id here
        Label2.Text = "9716805874";//<--Enter gmail password here
        Label3.Text = "msdotnet website"; //Subject for your website
        Label4.Text = "webcome to http://msdotnet.co.in. please share this website to your friends"; //Message body
        smtpc.Credentials = new NetworkCredential(Label1.Text,Label2.Text);
        MailMessage email = new MailMessage(Label1.Text,TextBox5.Text,Label3.Text,Label4.Text);
         smtpc.Send(email);
        Response.Redirect("default.aspx");
        con.Close();
    }
    else
    {
        //Response.Redirect("Registration.aspx");
        Label2.ForeColor = System.Drawing.Color.Red;
   Label2.Text = "You have Entered InValid Captcha Characters please Enter again";
    }       
}   
}

Note:- In above codes  ,you have to changed gmail  ID and password ,Subject and Message Body Values.which are shown in above codes.
Step 3: Now Run the Application (Press F5)-->Fill the required control  fields as shown below:
submit details

Step 4: Now Click Submit Button.
Output:


output

Step 5: Now open your Email Box.You will see as shown below:


output

Note:- Here I have used a gmail mail service  because it is free and secure.Every person can implement this Email service on your ASP.NET Website free.If you have own mail server then you can use your mail server instead of gmail server.You will have to seen that all website on internet use your own mail server for sending the mail.
For More:-
  1. How to solve SQL Server problem
  2. Create setup file with Database
  3. Web Form controls
  4. File Handling Real Application
  5. oops concepts
I hope this is helpful for you.

To Get the Latest  Free Updates Subscribe
Click below for Download whole Application
             Download

5 comments:

  1. very nice.. Thanks!
    Have a look at this link http://www.etechpulse.com/2012/09/how-to-send-email-in-aspnet-using-c.html

    ReplyDelete
  2. Thanks, I've been looking for this for a while now to incorporate on my site.
    Thank you..I shall follow you.

    ReplyDelete
  3. How to Resolve a SMTP Server Secure Connection and Authentication 5.5.1 Required Error | The SMTP Server Requires a Secure Connection or the Client was not authenticated

    http://allittechnologies.blogspot.in/2015/04/the-smtp-server-requires-secure-connection-or-the-client-was-not-authenticated-the-server-response-was-5.5.1-authentication-required.html

    ReplyDelete
  4. I am getting server authentication error while sending mail .... even though putting correct username and password

    ReplyDelete
  5. I need to send email when user clicks on button which already inside the body of mail as a reply of predefined few lines...

    ReplyDelete

Powered by Blogger.