how to send mail in asp.net c#

 Admin   
1439 View    Feb 4 2020 3:54PM

Introduction

Sending email is a very common task in any web application for many purposes. In daily development we need to add some mail functionality to our project to send e-mail to the customer or another in our web site.

Using the code

For sending mail from ASP.NET we use the "System.Net.Mail" namespace. Let's see how to do this.

  • Open Visual Studio 2010
  • "File" -> "New" -> "Project..."
  • Choose Visual C#- Web - Select ASP.NET Empty Web Application
  • Add a new aspx page

The following is the code for the design of the page.

Sendmail.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<
head id="Head1" runat="server">
    <title>Untitled Page</title>
</
head>
<
body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>To : </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Subject:      </td>
                <td>
                    <asp:TextBox ID="txtsubject" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td>Message:
                
</td>
                <td>
                    <asp:TextBox ID="txtmessage" runat="server" TextMode="MultiLine" Height="99px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Bttn_Send" runat="server" Text="Send" OnClick=" Bttn_Send _Click" />
                </td>
            </tr>
        </table>
    </form>
</
body>
</
html>

In the code above we have the following 3 fields:

  • To
  • Subject
  • Message

When the user clicks the "Send" button, the mail will be sent to the specified mail address that you provide in the "txtTo" TextBox. So add the following code for the Button_Click.

SendMail.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Net.Mail;
 
public void SendMail()
{
    mailMessage mail = 
new MailMessage();
    mail.To.Add(txtTo.Text);

    mail.From = new MailAddress("sourabh9303@gmail.com");
    mail.Subject = 
"Email using Gmail";
    
string Body = txtmessage.Text;
    mail.Body = Body;
    SmtpClient smtp = 
new SmtpClient();
    smtp.Host = 
"smtp.gmail.com"//Or Your SMTP Server Address
    smtp.Port = 587;
    smtp.UseDefaultCredentials = 
false;
    smtp.Credentials = 
new System.Net.NetworkCredential
    (
"username""password");

    
//Or your Smtp Email ID and Password
    smtp.EnableSsl = true;
    smtp.Send(mail);
}
protected void Bttn_Send_Click(object sender, EventArgs e)
{
    SendMail();
}


Understanding the Code

In the code above we have a SendMail() userdefined method. In this method, we create an object of the MailMessage class.

MailMessage mail = new MailMessage();

MailMessage is the main class for sending mail, this is the part of the System.Net.Mail namespace.

The MailMessage class has properties, the important ones are:

  • To
  • From
  • Cc
  • Bcc
  • Subject
  • Body

So we add our data into specified properties.

mail.To.Add(txtTo.Text);
mail.From = new MailAddress("sourabh9303@gmail.com");
mail.Subject = "Email using Gmail";
string Body = txtmessage.Text;
mail.Body = Body;

For sending mail we need a SMTP Server, so in ASP.Net we have the SmtpClient class, using that class object we set its properties for the SMTP settings.

SmtpClient smtp = new SmtpClient();

The SMTPClient class has these basic properties:

  • Host
  • Port
  • UseDefaultCredential
  • Credentials
  • EnableSsl
  • Send

smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("username""password");
smtp.EnableSsl = true;

Here in the code above smtp.Host = "smtp.gmail.com"This is the SMTP Host address of Gmail, if you want to use any other SMTP host service please add a different SMTP host protocol, for example for Hotmail it is smtp.live.com.

Smtp.Port=587 , 587 is the port for Gmail, so for any other service port you have to change the port correspondingly.

smtp.Credentials = new System.Net.NetworkCredential("username""password");

Smtp.Credentials specifies the Network Crendentials of your Gmail id so please add your username and password instead of ("username""password");

The following is for a secure mail server, so you enable your SSL layer.

smtp.EnableSsl = true;

Smtp.Send sends the mail so please add your MailMesssage object here, based on properties, your mail will be sent. See:

Smtp.Send(mail);