Send Email using Asp.net C# with sample code - Dotnetspan.com

Sending email with asp.net C# is no more difficult. You can easily use dotnet framework inbuilt class libraries. You can use System.Net.Mail or System.Web.Mail namespaces to get all mail properties and methods.
To send any mail you must aware with SMTP Server properties. You must declare smtp Server which is given by hosted mail server. If you don't have your own SMTP Server then you can use gmail, yahoo or any free mail service provider.
In this example we have make object of MailMessage. using MailMessage object you can set message from, message to, email subject, email body , email property, email attachment etc. You can send email will attachment and with html body, we will publish both examples in our next session.

Recommended to read
How to send Email with attachment using Asp.net C#
how to send Email with Html Body using Asp.net C#
how to send mail using Gmail or Gmail Server in Asp.net C#

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send Mail using Asp.net - Dotnetspan.com</title>
</head>
<body>
    <asp:Label ID="lblmessage" runat="server" Visible="false"></asp:Label>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td colspan="2"><h2>Send Mail using Asp.net - Dotnetspan.com</h2></td>
        </tr>
        <tr>
            <td>From:</td>
        <td>
            <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
        </td>
        </tr>
        <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="txtBody" TextMode="MultiLine" Rows="10" Columns="20" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td></td>
        <td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick="btnSend_Click" /></td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>
Default.aspx.cs
add using System.Net.Mail or using System.Web.Mail 
protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To = txtTo.Text.Trim();
            mailMessage.From = txtFrom.Text.Trim();
            mailMessage.Body = txtBody.Text.Trim();
            mailMessage.Priority = MailPriority.High;
            SmtpMail.SmtpServer = "smtp.yourwebmail.com";
            SmtpMail.Send(mailMessage);
            lblmessage.Visible = true;
            lblmessage.Text = "Email Sent Successfully";
            lblmessage.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblmessage.Visible = true;
            lblmessage.Text = "Error Occur while processing your request.";
            lblmessage.ForeColor = System.Drawing.Color.Red;
        }
    }
Demo
Download Sample Code

Search Terms -Asp.net send email in C#, sample code for send email in asp.net, send email in C#, asp.net C# email send example.