Home > DeveloperSection > Forums > How to Send E-mails in asp.net c#
Ankit Singh

Total Post:341

Posted on    November-12-2015 10:22 PM

 .NET C#  ASP.Net 

 1 Reply(s)
 306  View(s)
Rate this:
I want to send Mail in asp.net c# how to write code in c# please help me.

aditya kumar Patel

Total Post:250

Posted on    November-12-2015 10:31 PM

Sending e-mails with ASP.NET is pretty straight forward. The .NET framework comes with an entire namespace for handling e-mails, the System.Net.Mail namespace. In the following examples, we will use two classes from this namespace: The MailMessage class, for the actual e-mail, and the SmtpClient class, for sending the e-mail.
As you may be aware, mails are sent through an SMTP server, and to send mails with the .NET framework, you will need access to an SMTP server. If you're testing things locally, the company that supplies your with Internet access, will usually have an SMTP server that you can use, and if you wish to use one of these examples on your actual website, the company that hosts your website will usually have an SMTP server that you can use. Go through the support pages to find the actual address - it's usually something along the lines of smtp.your-isp.com or mail.your-isp.com.
Once you have an accessible SMTP server, we're ready to send our very first e-mail using Gmail SMTP. For the first example, all you need is an empty page, with the following code in the CodeBehind button click:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Mail;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace Forumasp



public partial class sendmail : System.Web.UI.Page


protected void Page_Load(object sender, EventArgs e)



protected void Button1_Click(object sender, EventArgs e)


using (MailMessage Mail = new MailMessage("XXXXXX@gmail.com","XXXXXX@gmail.com"))


Mail.Subject = "Request";

Mail.Body = "hello";

Mail.IsBodyHtml = false;

SmtpClient smtp = new SmtpClient();

smtp.Host = "smtp.gmail.com";

smtp.EnableSsl = true;

NetworkCredential NetworkCred = new NetworkCredential("XXXXXX@gmail.com", "XXXXXXX");

smtp.UseDefaultCredentials = true;

smtp.Credentials = NetworkCred;

smtp.Port = 587;


ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);






Don't want to miss updates? Please click the below button!

Follow MindStick