Sending Email in ASP.NET using C#

In this tutorial we will learn how to send emails in ASP.Net using C#. To send email from webpage in is a common and most demanding requirement now days.

Asp.Net use System.Net.Mail namespace to send emails.

Now let’s have a look over example given below that will teach us how to send emails in using C#


<form id="form1" runat="server">
<table width="600" cellpadding="2" cellspacing="2" border="0">
<td colspan="2">

Sending Emails in ASP.NET using C#



<td><strong>To</strong></td><td><asp:TextBox runat="server" ID="txt_To" Width="220px"></asp:TextBox></td>

<td><strong>Cc</strong></td><td><asp:TextBox runat="server" ID="txt_Cc" Width="220px"></asp:TextBox></td>

<td><strong>Bcc</strong></td><td><asp:TextBox runat="server" ID="txt_Bcc" Width="220px"></asp:TextBox></td>

<td><strong>From</strong></td><td><asp:TextBox runat="server" ID="txt_From" Width="220px"></asp:TextBox></td>

<td><strong>Subject</strong></td><td><asp:TextBox runat="server" ID="txt_Subject" Width="380px"></asp:TextBox></td>

<td><strong>Message</strong></td><td><asp:TextBox runat="server" ID="txt_Message" TextMode="MultiLine" Rows="7" Columns="45"></asp:TextBox></td>

<td> </td><td><asp:Button runat="server" ID="btn_Submit" Text="Send Email"
onclick="btn_Submit_Click" /></td>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net; // Namespace used to send email
using System.Net.Mail; //Namespace used to send email

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

protected void Page_Load(object sender, EventArgs e)

protected void btn_Submit_Click(object sender, EventArgs e)

//Function to send Emails in ASP.NET using C#
protected void sendEmail()
MailMessage msg = new MailMessage(txt_From.Text,txt_To.Text);
msg.Subject = txt_Subject.Text;
msg.Body = txt_Message.Text;
msg.Subject = txt_Subject.Text;
SmtpClient yourSmptpclient = new SmtpClient("localhost");
catch(Exception Exp)
throw Exp;

//Ends Here

Now let’s understand MailMessage Class Properties

MailMessage Class Properties

From –Email Address of sender
To – Email Address of Recipient(s)
CC – Carbon Copies
BCC – Blind Carbon Copies
Subject – Subject of the Email
ReplyTo – Reply To Email address.
Attachments – Attachments if Any you want.
Body – Body of the Email
IsBodyHtml – Specify whether body contains text or HTML mark up.

Now let’s understand SMTP Class Properties

SMTP Class Properties
Host – Your SMTP Server
EnableSsl – Specify whether you host accepts SSL Connections or not.
Credentials – Valid login credentials for your SMTP server .
UseDefaultCredentials – Set True In order to allow authentication based on the Credentials of the Account used to send emails.
Port – Port No of the SMTP server

So this is the way to send emails in using C#