ASP: Basic contact page

ASP: Basic contact page


Here is a tutorial showing you how to create a simple contact page in ASP.NET using VB.NET.

Please Note: You will need a basic understanding of ASP.NET web controls as well as basic HTML to be able to complete this tutorial.



Creating the contact form

For a user to be able to send an email they are going to need to enter the following details:

- Receiver's email address

- Their own email address

- A subject title

- A message

Create a new .aspx file and call this 'Contact'. Then enter the following ASP.NET basic page layout:
<%@ Page Language="VB" %>

<script runat="server">

</script>

<html>
<head runat="server">
<title>Contact page</title>
</head>
<body>
<form id="form1" runat="server">

</form>
</body>
</html>

Within the <form></form> tags we need to create the contact form. Here is a simple table containing all the text boxes we will need:
<table>
<tr><td>Your Email:</td><td><asp:TextBox ID="youremail" runat="server" /></td></tr>
<tr><td>To:</td><td><asp:TextBox id="toemail" runat="server" /></td></tr>
<tr><td>Subject:</td><td><asp:TextBox ID="subject" runat="server" /></td></tr>
<tr><td>Body:</td><td><asp:TextBox ID="body" runat="server" TextMode="MultiLine" Rows="8" Columns="40" /></td></tr>
<tr><td colspan="2" align="center"><asp:Button ID="button1" runat="server" Text="Send Email" /></td></tr>
<tr><td colspan="2" align="center"><asp:Label ID="label1" runat="server" /></td></tr>
</table>

Creating the sub-routine

Next we must create the sub-routine. This is the action which happens when the user clicks the button on the page which will send the email. This will go within the <script></script> tags. Before the email is sent we must first check that none of the fields are blank. To do this we must create an If statement within the sub-routine like so:
Sub sendemail(ByVal sender As Object, ByVal e As EventArgs)

If youremail.Text = "" Or toemail.Text = "" Or subject.Text = "" Or body.Text = "" Then
label1.Text = "You have left some fields blank."
Else
'Send Email
End If

End Sub

This if statement will now return a message on the screen where 'label1' has been placed saying 'You have left some fields blank.'. However if all text boxes are filled in then the email can be sent.

Next we must create the code which will actually create the email and send it. Here is the above if statement, but with the email code placed inside:
If youremail.Text = "" Or toemail.Text = "" Or subject.Text = "" Or body.Text = "" Then
label1.Text = "You have left some fields blank."
Else
Dim objMailMessage As MailMessage

'Create mail message
objMailMessage = New MailMessage
objMailMessage.BodyFormat = MailFormat.Html
objMailMessage.From = youremail.Text
objMailMessage.To = toemail.Text
objMailMessage.Subject = subject.Text
objMailMessage.Body = body.Text

'Send mail
SmtpMail.Send(objMailMessage)
End If

For the above code to work we must also import a namespace into the page. Don't worry if you don't know what this means yet as this tutorial is for beginners and you won't need to know just yet. So go ahead and add this on the second line of code on the page:
<%@ import Namespace="System.Web.Mail" %>
Now that the sub-routine is finished we must connect it with the button on the page. To do this add 'onclick="sendemail"' into the button tag like so:
<asp:Button ID="button1" runat="server" Text="Send Email" OnClick="sendemail" />
Finishing touches

To finish off the contact page you may want to display a message to confirm the email has been sent. To do this add the following code into the sub-routine:
label1.text = "Email has been sent successfully!"
Place this below 'SmtpMail.Send(objMailMessage)'. Alternatively you can redirect to another page like so:
Response.redirect("emailsent.aspx")

That's all for the tutorial :)



Submit Comment

Comments

Unfortunately no comments have been left on this tutorial. Be the first using the form above.