|
|
Here is a tutorial showing you how to create a simple contact page in ASP.NET using VB.NET. First of all I will show you how to create the form which the user will enter details in to send the email. 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" %>
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>
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)
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
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")
Source1 - 2824contactexample.aspx (right-click > "save target as" to download) Sponsored Link
|
|
||

