Dot Net Sparks C#.NET,ASP.NET,ADO.NET,LINQ,AJAX,JavaScript,SharePoint

/ 0

Send mail in ASP.NET

This example shows how to send mail in asp.net. To send mail in asp.net MailMessage and SmtpClient class are used, which are in System.Net.Mail namespace.


Create a new website in VS and add the following code that designs our aspx page with text boxes, buttons, etc.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send Mail Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <b>Send Mail Example</b>
        <table cellpadding="2">
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblInfo" runat="server" Text="Info" ForeColor="#990000"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    From
                </td>
                <td>
                    <asp:TextBox ID="txtFrom" runat="server" Width="300px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvFrom" runat="server" ControlToValidate="txtFrom"
                        Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="revFrom" runat="server" ControlToValidate="txtFrom"
                        Display="Dynamic" ErrorMessage="Invalid Email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td>
                    To
                </td>
                <td>
                    <asp:TextBox ID="txtTo" runat="server" Width="300px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvTo" runat="server" ControlToValidate="txtTo" Display="Dynamic"
                        ErrorMessage="*"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="revTo" runat="server" ControlToValidate="txtTo"
                        Display="Dynamic" ErrorMessage="Invalid Email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td>
                    Subject
                </td>
                <td>
                    <asp:TextBox ID="txtSubject" runat="server" Width="300px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Message
                </td>
                <td>
                    <asp:TextBox ID="txtMessage" runat="server" Rows="10" TextMode="MultiLine" Width="500px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:Button ID="btnSend" runat="server" Text="Send" Width="150px" OnClick="btnSend_Click" />
                    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="150px" OnClick="btnClear_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
And it’s code behind
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblInfo.Visible = false;
        }

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress(txtFrom.Text);
            message.To.Add(new MailAddress(txtTo.Text));
            message.Subject = txtSubject.Text;
            message.Body = txtMessage.Text;
            message.IsBodyHtml = true;
            message.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient("localhost");
            client.Send(message);
            lblInfo.Visible = true;
            lblInfo.Text = "Your message has been sent.";
            txtFrom.Text = "";
            txtTo.Text = "";
            txtSubject.Text = "";
            txtMessage.Text = "";
        }
        catch (Exception ex)
        {
            lblInfo.Visible = true;
            lblInfo.Text = ex.Message;
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Default.aspx");
        txtFrom.Text = "";
        txtTo.Text = "";
        txtSubject.Text = "";
        txtMessage.Text = "";
    }
}
Note: This method of sending mail requires application or website to be hosted on some webserver.

0 comments
Comments (0)

Leave a comment