Sending Auto generated Emails through ASP.NET

In this article i am going to explain, how to send auto generated email through your Gmail credentials in ASP.NET. This is very simple code and you can use it in any .nET applications like sending an email to the users who have contacted you.

Sometimes in our applications we need to send automated emails to the users. Like while the registration, forgot password link and many more. Using this article we can achieve this very eaisly-

Create a simple ASPX page (like sample.aspx) and on the button click/pageload of the form you can write the following in sample.aspx.cs, If you want at button click mail should be sent, follow the below or if you want on the page load itself, mail should get delivered- write whole code on Page_Load method itself.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class sample: System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void submit_Click(object sender, EventArgs e)
MailMessage Msg = new MailMessage();

Msg.From = new MailAddress("");
// Recipient e-mail address.
Msg.Subject = "Alert For Contacted User!";

Msg.Body = "One user " + name.Text + " with EmailID "+emailid.Text+" and Contact # "+number.Text+" has contacted you rerading query "+description.Text+ ". plesae get in touch";

SmtpClient smtp = new SmtpClient();
smtp.Host = "";

smtp.Port = 587;
//Sender mail id and password
smtp.Credentials = new System.Net.NetworkCredential("", "XXXX");
smtp.EnableSsl = true;
Msg = null;
ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "alert('Thank you for contacting us.We will get back to you soon!');", true);
//this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);

catch (Exception ex)
Console.WriteLine("{0} Exception caught.", ex);
name.Text = "";
emailid.Text = "";
number.Text = "";
description.Text = "";
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);
protected void reset_Click(object sender, EventArgs e)
name.Text = "";
emailid.Text = "";
number.Text = "";
description.Text = "";


Here you need to use one additional reference for the mail in namespace-
using System.Net.Mail;
This will enable the mailing feature.

In this form, i have used 4 textbox for name, email, number and message.

Below are the some of the terms that i have used in the above code.

1. msg.from - is the email id through which you want to send the emails. If you want to send it dynamically like email id from textbox, just write id of the textbox.text.
2.Msg.To.Add- Email id to whom you want to send emails. You can also make this field dynamic same as above.
3.Msg.Subject- Subject line of the email. Can be done dynamic. Suppose in your for you have a textbox with id sub then make msg.subject-sub.text.
4.Msg.Body- whatever you want to write in the body of the mail.
5.smtp.Host- gateway is smtp
6.smtp.Credentials- write the credentials. or you can use default credentials also by setting defaultcredentials prperty to true.

Ashutosh Jha


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: