Search  
   
Browse by Category
 
Netfirms 24/7 Support .: WINDOWS HOSTING .: ASP.NET .: How do I use a web form ASP.NET script so my visitors can send me email?

How do I use a web form ASP.NET script so my visitors can send me email?

Below is a sample ASP.NET script that may be used to send email to an email address from a web form:

sendemail.aspx
<%@ Page Language="VB" ClientTarget="downlevel" %>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat="server">
 Sub btnSendMail_OnClick(Source As Object, E As EventArgs)
  Dim myMessage    As New MailMessage
  Dim myMail       As SmtpMail
  Dim strEmail     As String
  If Page.IsValid() Then
   strEmail = txtEmail.Text
   'myMessage.From   = yourname@" & Request.ServerVariables("SERVER_NAME")
   myMessage.From    = "yourname@yourdomain.com"
   myMessage.To      = strEmail
   myMessage.Subject = "E-mail Sent from yourdomain.com!"
   ' This is the magic line.  Without this the message will just appear
   ' as plain HTML and won't be rendered by the recipient's email client.
   ' It'd be as if they did "View Source" on a web page.
   MyMessage.BodyFormat = MailFormat.Html
   ' This is multi-lined simply for readability.
   ' Notice that it is a properly formatted HTML
   ' message and not just plain text like most email.
   ' A lot of people have asked how to use form data
   ' in the emails so I added an example of including
   ' the entered address in the body of the email.
   myMessage.Body    = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & vbCrLf _
    & "<html>" & vbCrLf _
    & "<head>" & vbCrLf _
    & " <title>Sample Message</title>" & vbCrLf _
    & " <meta http-equiv=Content-Type content=""text/html; charset=iso-8859-1"">" & vbCrLf _
    & "</head>" & vbCrLf _
    & "<body bgcolor=""#FFFFCC"">" & vbCrLf _
    & " <h2>Sample Message</h2>" & vbCrLf _
    & " <p>" & vbCrLf _
    & "  <strong>" & vbCrLf _
    & "  We do not store e-mail addresses." & vbCrLf _
    & "  </strong>" & vbCrLf _
    & " </p>" & vbCrLf _
    & " <p><font size=""-1"">This message was sent to: " & strEmail & "</font></p>" & vbCrLf _
    & "</body>" & vbCrLf _
    & "</html>" & vbCrLf
   ' Doesn't have to be local... just enter your
   ' SMTP server's name or ip address!
   myMail.SmtpServer = "WinMailSrv"
   myMail.Send(myMessage)
   frmEmail.Visible = False
   lblUserMessage.Text = "An HTML-formatted email message has been sent to " & strEmail & "."
  End If
 End Sub
</script>
<html>
<head>
<title>sendemail50.aspx Email (HTML Format) Sample</title>
</head>
<body>
<asp:Label id="lblUserMessage" text="Enter your e-mail address:" runat="server" />
<form method="post" id="frmEmail" runat="server">
 <asp:TextBox id="txtEmail" size="30" runat="server" />
 <asp:RequiredFieldValidator runat="server"
  id="validEmailRequired" ControlToValidate="txtEmail"
  errormessage="Please enter an email address."
  display="Dynamic" />
 <asp:RegularExpressionValidator runat="server"
  id="validEmailRegExp" ControlToValidate="txtEmail"
  ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
  errormessage="Please enter a valid email address."
  Display="Dynamic" />
 <asp:Button id="btnSendMail" text="Send Mail!" OnClick="btnSendMail_OnClick" runat="server" />
</form>
<hr />
</body>
</html>
NOTE: For additional ASP.NET support and resources we recommend the following web sites:

How helpful was this article to you?


.: Powered by Lore 1.5.6
Visit Netfirms.com Web Hosting | Copyright © 1998 - 2009 Netfirms, Inc. All Rights Reserved.