Mail Message using Simple Message Transfer Protocol in ASP.Net

Step 1: Open visual studio -> website -> Asp.net Empty website then choose any name and click OK

Step 2:  From solution explorer right click on your website name and then choose add new item -> web form give name to that web form and click OK

Step 3: Take 6 label,5 Text box , 1 button and RegularExpressionvalidator.

Design will look like:

Design.jpg

RegularExpressionvalidator is used so that if user enter email id in incorrect format it will show an error.

Way to set RegularExpressionvalidator is Properties of RegularExpressionvalidator-> validation expression -> Internet email address

Code used at default.aspx page:

<%@ 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 id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Label ID="lem" runat="server" Text="Email id"></asp:Label>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="tem" runat="server"></asp:TextBox>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="tem"

            ErrorMessage="Email id should be in format [email protected]" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

        <br />

        <br />

        <asp:Label ID="lpwd" runat="server" Text="Password"></asp:Label>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="tpwd" runat="server" TextMode="Password"></asp:TextBox>

        <br />

        <br />

        <asp:Label ID="lre" runat="server" Text="Receiver Email ID"></asp:Label>

        &nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="tre" runat="server"></asp:TextBox>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="tre"

            EnableViewState="False" ErrorMessage="Email id should be in format aba@anyserver(gmail/yahoo/hotmail).com"

            SetFocusOnError="True" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

        <br />

        <br />

        <asp:Label ID="lsub" runat="server" Text="subject"></asp:Label>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="tsub" runat="server"></asp:TextBox>

        <br />

        <br />

        <asp:Label ID="lbdy" runat="server" Text="Body"></asp:Label>

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="tbdy" runat="server" TextMode="MultiLine"></asp:TextBox>

        <br />

        <br />

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:Button ID="b1" runat="server" Text="Submit" OnClick="b1_Click" />

        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <asp:Label ID="l" runat="server" Text=" "></asp:Label>

    </div>

    </form>

</body>

</html>

 
Then click on submit button and then there will be another page default.aspx.cs

code on default.aspx.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Net; // additional namespace for sending mail message

using System.Net.Mail; // additional namespace for sending mail message

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected void b1_Click(object sender, EventArgs e)

    {

        MailMessage m = new MailMessage(tem.Text, tre.Text, tsub.Text, tbdy.Text);

        NetworkCredential n = new NetworkCredential(tem.Text, tpwd.Text);

        SmtpClient s = new SmtpClient("smtp.gmail.com", 587);

        s.EnableSsl = true;

        s.UseDefaultCredentials = false;

        s.Credentials = n;

        s.Send(m);

        l.Text = "Sended Successfully";

    }

}

 
Output


Before Sending message

before.PNG

After Sending message

after.PNG

Output


outcome.PNG

Ebook Download
View all
Learn
View all