tell_a_friend.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>
<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
Dim strReferringPage As String
Dim strBody As String
strReferringPage = Request.ServerVariables("HTTP_REFERER")
If strReferringPage = "" Or InStr(1, strReferringPage, "asp101.com", 1) = 0 Then
strBody = ""
strBody = strBody & "I found a site I thought you'd like to see:" & vbCrLf
strBody = strBody & vbCrLf
strBody = strBody & " http://www.asp101.com" & vbCrLf
Else
strBody = ""
strBody = strBody & "I found an article I thought you'd like to see:" & vbCrLf
strBody = strBody & vbCrLf
strBody = strBody & " " & strReferringPage & vbCrLf
End If
txtMessage.Text = strBody
End If
End Sub
Sub btnSendMsg_OnClick(Source As Object, E As EventArgs)
Dim myMessage As MailMessage
Dim mySmtpClient As SmtpClient
If Page.IsValid() Then
myMessage = New MailMessage
myMessage.From = New MailAddress(txtFromEmail.Text, txtFromName.Text)
myMessage.To.Add(New MailAddress(txtToEmail.Text))
myMessage.Subject = "Check out ASP 101!"
myMessage.Body = txtMessage.Text & vbCrLf _
& "This message was sent from: " _
& Request.ServerVariables("SERVER_NAME") & "." & vbCrLf & vbCrLf _
& "This email was sent to " & txtToEmail.Text & "."
mySmtpClient = New SmtpClient("localhost")
mySmtpClient.Send(myMessage)
lblMessageSent.Text = "Your message has been sent to " & txtToEmail.Text & "."
End If
End Sub
</script>
<html>
<head>
<title>ASP.NET Tell A Friend Sample</title>
</head>
<body>
<h3>Tell a Friend:</h3>
<form runat="server">
<table border="0">
<tr>
<td valign="top" align="right"><strong>Your Name:</strong></td>
<td>
<asp:TextBox id="txtFromName" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validNameRequired" ControlToValidate="txtFromName"
errormessage="Please enter your name."
display="Dynamic" />
</td>
</tr>
<tr>
<td valign="top" align="right"><strong>Your E-mail:</strong></td>
<td>
<asp:TextBox id="txtFromEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validFromEmailRequired" ControlToValidate="txtFromEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validFromEmailRegExp" ControlToValidate="txtFromEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />
</td>
</tr>
<tr>
<td valign="top" align="right"><strong>Friend's E-mail:</strong></td>
<td>
<asp:TextBox id="txtToEmail" size="30" runat="server" />
<asp:RequiredFieldValidator runat="server"
id="validToEmailRequired" ControlToValidate="txtToEmail"
errormessage="Please enter an email address."
display="Dynamic" />
<asp:RegularExpressionValidator runat="server"
id="validToEmailRegExp" ControlToValidate="txtToEmail"
ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
errormessage="Please enter a valid email address."
Display="Dynamic" />
</td>
</tr>
<tr>
<td valign="top" align="right"><strong>Message:</strong></td>
<td>
<asp:TextBox id="txtMessage" Cols="60" TextMode="MultiLine" Rows="5" ReadOnly="True" runat="server" />
</td>
</tr>
<tr>
<td> </td>
<td><asp:Button id="btnSend" Text="Send Message" OnClick="btnSendMsg_OnClick" runat="server" /></td>
</tr>
</table>
</form>
<asp:Label id="lblMessageSent" runat="server" />
<hr />
<p>
Click <a href="http://www.asp101.com/samples/tell_a_friend_aspx.asp">here</a> to read about and download the source code.
</p>
</body>
</html>