In this article we will learn how to send a mail in using web from in c sharp code.


To implement mail functionality in first we need to add following reference to our application System.Net.Mail and System.Net namespace .

In The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.


What is SMTP? 


Simple Mail Transfer Protocol (SMTP) is a TCP/IP protocol used in sending and receiving e-mail. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another. The messages can then be retrieved with an e-mail client using either POP or IMAP.


SMTP Class Properties:

Host: Server URL for SMTP (

EnableSsl: Check your host accepts SSL Connections (True or False).

Port: Port Number of the SMTP server (25).

Credentials: Valid login credentials for the SMTP server (the email address and password).

UseDefaultCredentials: When we set to True in UseDefaultCredentials then that specifies to allow authentication based on the credentials of the account used to send emails.


Let’s Simple Example of send mail in Asp.Net.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
namespace Email
   public partial class WebForm1 : System.Web.UI.Page
       protected void btnSendMail_Click(object sender, EventArgs e)
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress("");
                msg.Subject = "Testing emaildelivery";
                msg.Body = "Testing";
                msg.IsBodyHtml = true;
                SmtpClient client = new SmtpClient("", 587);
                client.EnableSsl = true;
                NetworkCredential credentials =
                new NtworkCredential("", "Password here");
                client.Credentials =credentials;
                Response.Write("We Will Contact you as soon as Possible!!! ");
                Response.Write("please try again letter!!! ");
            finally { }

I hope it will help to you.

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like