Mar
25
2013

C#.Net How To: Send email in asp.net using c#

In this C# tutorial we are going to see how to send email in asp.net c#. To send an email using C# is not a big task and following the steps given in this article anyone can send email in asp.net using c#.
Send Email from Asp.Net
Send Email from Asp.Net

For demonstration to send an email in asp.net we will first create a ASP.net page which will accepts some information like To address and Message body. Have a look at below image, which will be final output.

Asp.net Web App to send Email using c#.net
Asp.net Web App to send Email using c#.net

Steps to send email in asp.net using c#

  1. Open Visual Studio and create a new Website project. To create a new website click File -> New -> Project Or simply clicking Ctrl +Shift +N keys together, .net IDE will allow you to create a new project.
  2. Select Web from Installed Templates on left pane, select ASP.NET Web Application from right pane, enter your desired project name, location of the project and click on OK button.
    Create new Web Application to send email
    Create new Web Application to send email

    It will create a new website application. By default Default.aspx page will be opened with default lines of code.

  3. We need to create a new aspx page that will have To and Body field. To add a new aspx page, right click on your project name, then select Add and then select New Item. Or click Ctrl+Shift+A keys together; it will open a “Add New Item” window.
  4. Select Web Form and name it as “ComposeMail.aspx” and click on Add Button.
    Add new item - aspx page
    Add new item – aspx page

  5. As soon as you click on Add button, it will create a new page with name “ComposeMail.aspx” and the default source code will be visible on screen.
  6. Now we need to place few controls on this web page to compose an email. So we need to place textboxes that accepts inputs likes To, subject & Body and a send button. Below code includes all these controls. Please copy paste below source code to “ComposeMail.aspx” file.
    <%@ Page Title=”Compose Email” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true”
        CodeBehind=”ComposeMail.aspx.cs” Inherits=”SendEmailFromAsp.Net.ComposeMail” %>

    <asp:Content ID=”HeaderContent” runat=”server” ContentPlaceHolderID=”HeadContent”>
        <style type=”text/css”>
            .style1
            {
                width: 62px;
                text-align: right;
            }
            .style2
            {
                width: 24px;
                text-align: right;
            }
            .style3
            {
                width: 62px;
                height: 140px;
                text-align: right;
            }
            .style4
            {
                width: 24px;
                height: 140px;
                text-align: right;
            }
            .style5
            {
                height: 140px;
                width: 489px;
            }
            .style6
            {
                width: 489px;
            }
        </style>
    </asp:Content>
    <asp:Content ID=”BodyContent” runat=”server” ContentPlaceHolderID=”MainContent”>
        <h2>
            Welcome to Simple email send application in ASP.NET using C#.
        </h2>
        <p>
            To learn more about ASP.NET visit <a href=”http://www.csharptutorial.in/”
                title=”C# Tutorials”>C# Tutorials</a>.
        </p>
        <p>
            <table style=”width: 100%;” width=”2″ >
                <tr>
                    <td class=”style1″>
                        <strong></strong>
                    </td>
                    <td class=”style2″>
                        <strong></strong>
                    </td>
                    <td align=”center” class=”style6″>
                        &nbsp;
                        <asp:Label ID=”lblResult” runat=”server” Text=””></asp:Label>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td class=”style1″>
                        <strong>To </strong>
                    </td>
                    <td class=”style2″>
                        <strong>: </strong>
                    </td>
                    <td class=”style6″>
                        &nbsp;
                        <asp:TextBox ID=”txtTo” runat=”server” Width=”475px”></asp:TextBox>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td class=”style1″>
                        <strong>Subject </strong>
                    </td>
                    <td class=”style2″>
                        <strong>: </strong>
                    </td>
                    <td class=”style6″>
                        &nbsp;
                        <asp:TextBox ID=”txtSubject” runat=”server” Width=”475px”></asp:TextBox>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td class=”style3″>
                        <strong>Message </strong>
                    </td>
                    <td class=”style4″>
                        <strong>: </strong>
                    </td>
                    <td class=”style5″>
                        &nbsp;
                        <asp:TextBox ID=”txtBody” runat=”server” Height=”132px” Width=”474px” TextMode=”MultiLine”></asp:TextBox>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <td class=”style1″>
                    </td>
                    <td class=”style2″>
                        &nbsp;
                    </td>
                    <td class=”style6″>
                        &nbsp;&nbsp;
                        <asp:Button ID=”btnSend” runat=”server” Text=”Send” Width=”103px” OnClick=”btnSend_Click” />
                    </td>
                    <td></td>
                </tr>
            </table>
        </p>
        <p>
            &nbsp;</p>
    </asp:Content>

    Please see screenshot given in next step for more details.

  7. Now click on Design, you will notice below screenshot. As shown in the screenshot user will enter a To email address in To textbox and actual message (body) will enter in Message textbox.
    Compose page to send email in asp.net
    Compose page to send email in asp.net

  8. Now we are going to write a SendEmail method that will accepts three parameters i.e. To email id, subject and body of an email. Double click on Send button to open “ComposeMail.aspx.cs” file. To send an email using c#, we need to include “System.Net.Mail” in our project. Add below namespace in this file –
    using System.Net.Mail;

  9. In this step we are going to implement a method – “SendEmail”. This method would write a code in to include the business logic to send email in c#. Following is the complete code of SendEmail method. Copy and paste this in your “ComposeMail.aspx.cs” file.
            protected string SendEmail(string toAddress, string subject, string body)
            {
                string result = “Message Sent Successfully..!!”;

                string senderID = “SenderEmailID“;// use sender’s email id here..
                const string senderPassword = “Password“; // sender password here…

                try
                {
                    SmtpClient smtp = new SmtpClient
                    {
                        Host = “SmtpServerAddress“, // smtp server address here…
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                        Timeout = 30000,

                    };

                    MailMessage message = new MailMessage(senderID, toAddress, subject, body);

                    smtp.Send(message);
                }
                catch (Exception ex)
                {
                    result = “Error sending email.!!!”;
                }

                return result;
            }

  10. Don’t forget to change the highlighted part of the above code, i.e. in above code you need to change the Sender email id, password and the smtp server address. If you are using gmail as your smtp server, then follow my other article on how to send email using gmail.
  11. In above code I have used “EnableSsl ” property. Use this property if your smtp server needs SSL authentication. Otherwise comment the line or set it to false.
  12. In above steps we have added a method to send email, however we need to call SendEmail method from send button click event. Write below code in btnSend_Click event in ComposeMail.aspx.cs file.
            protected void btnSend_Click(object sender, EventArgs e)
            {
                lblResult.BackColor = System.Drawing.Color.Red;
                lblResult.Text = SendEmail(txtTo.Text, txtSubject.Text, txtBody.Text);
            }

  13. As per the above code, if the an email is successfully transmitted then below message will appear on screen just above the “To” textbox –
    Message Sent Successfully..!!

    Error Sending email..!!

  14. Please do not forget to change the User name, password and smtp server address as stated in above step 10.
  15. Now we are done with all design and coding part of the email sending application in ASP.net. Hit F5 button to execute the application, enter your recipients address, type your message in body box and click on send button to send the message.
  16. Complete code of ComposeMail.aspx.cs files is given below and find complete design source code in step 6.
    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;

    namespace SendEmailFromAsp.Net
    {
        public partial class ComposeMail : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }

            protected void btnSend_Click(object sender, EventArgs e)
            {
                lblResult.BackColor = System.Drawing.Color.Red;
                lblResult.Text = SendEmail(txtTo.Text, txtSubject.Text, txtBody.Text);
            }

            protected string SendEmail(string toAddress, string subject, string body)
            {
                string result = “Message Sent Successfully..!!”;

                string senderID = “SenderEmailID”;// use sender’s email id here..
                const string senderPassword = “Password”; // sender password here…

                try
                {
                    SmtpClient smtp = new SmtpClient
                    {
                        Host = “SmtpServerAddress”, // smtp server address here…
                        Port = 587,
                        EnableSsl = true,
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
                        Timeout = 30000,

                    };

                    MailMessage message = new MailMessage(senderID, toAddress, subject, body);

                    smtp.Send(message);
                }
                catch (Exception ex)
                {
                    result = “Error sending email.!!!”;
                }

                return result;
            }
        }
    }

In this c# tutorial we learned how to send email in c#. I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?

More articles –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

11 Comments+ Add Comment

  • I like your tutorial.

  • thanks tis code is working bt i want to change a layout how to do that

  • thanks you

  • Great Tutorial! Really Thanks.

    • Hi Suesha,
      Thanks for sharing your thought. Thank you!

  • aswm. thank u very much.

  • i cant send the email .. showing error msg only :(

  • not working

    it showing this error

    The operation has timed out.

  • The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

    i get this error

  • Great Tutorial. I get it very easily, I am a new bee in ASP.Net World! Thanks Author!!!

Leave a comment