Send E-mail using ASP.NET 4.5

If you build a large site then you will be required to provide a facility for the users to contact the relevant department or web master through online form. This is the best way to prevent spam messages as users will not know the real e-mail id.

The whole process of sending emails from an ASP.NET form has been simplified with the release of .NET Framework 4.5.

Look at the below figure where I added few Textbox controls

Figure_1

You need to substitute FROM EMAIL ID, MAIL SERVER NAME, EMAIL ID and PASSWORD on the below code with relevant correct values.

Looking for quality ASP.NET 4.5 Hosting? Look no further than Arvixe Web Hosting!

Copy the code given below after providing correct values as mentioned above using Notepad, save it as an .aspx file and upload to your web server.

The below code requires access to a shared Windows hosting server with ASP.NET 4.5.

<%@ Page Language="VB" %>
<%@ Import Namespace = "System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>

<script runat="server">    

    Protected Sub btnSubmit_Click1(sender As Object, e As EventArgs)
        Dim mail As New MailMessage()

        mail.To.Add(txtEmail.Text)
        mail.From = New MailAddress("FROM EMAIL ID")
        mail.Subject = "Contact Form"

        mail.Body = txtName.Text & vbCrLf & txtComments.Text

        Dim smtp As New SmtpClient("MAIL SERVER NAME")
        smtp.Credentials = New NetworkCredential("EMAIL ID", "PASSWORD")

        smtp.Send(mail)
        lblStatus.Text = "Your data has been submitted successfully"
        txtName.Text = ""
 	txtEmail.Text = ""
	txtComments.Text = ""

    End Sub
</script>

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Contct Form Demo</title>
</head>

<body>
    <form id="form1" runat="server">
        Name -
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
        <br />
        <br />
        Email -
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
         <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" 

ErrorMessage="Please Enter Valid Email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)

*"></asp:RegularExpressionValidator>
        <br />
        <br />
        Comments -
        <asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Height="71px" Width="301px"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="lblStatus" runat="server"></asp:Label>
        <br />
        <br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click1" />
        <br />
        <br />
    </form>
</body>
</html>

In the above code, I have enabled validation for E-mail ID field. This means that if you forgot to enter e-mail id in correct format you will not be able to submit the form.

Figure_2

Looking for quality ASP.NET Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , | Posted under ASP .NET 4.0 | RSS 2.0

Author Spotlight

Anand Narayanaswamy

Anand Narayanaswamy

Anand works as a freelance writer based in Trivandrum, India. Anand runs NetAns, which provides hosting for developers and small scale businesses. He also provides blog installation services.

Leave a Reply

Your email address will not be published. Required fields are marked *


6 − 2 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>