H
Henry Stock
My ISP provides this sample for an ASP.NET capable email form.
If you have any idea about how to do the following, I would greatly
appreciate your help.
I have a few alterations that I would like to implement, but I am not sure
where to begin.
First:
I want to implement the code in a codebehind page instead of in the
mailform.aspx page. I am not sure where I would put the code. I also know
that there are differences in background operations between ASP.NET 1.1 and
ASP.NET 2.0, but I don't know if the code would differ much. In other stuff
I found on the internet, the SMTP server was identified in a <mailsettings>
section in the web.config file. That seems cleaner to me, but I don't know
how the code would change. I probably would skip the code identifying the
server.
Second:
I already have an old mail form that was implemented using a CGI. I also
used a javascript based CAPTCHA function in that form to cut down on
automated spam.
In that instance the old form tag included and action attribute that pointed
to the cgi and an onsubmit="return jcap()" function that invoked the
javascript; elsewhere in the page there was a call to another javascript
that implemented the image that needed to be verified. I would still like
to use that script to prevent autospammers. I am not sure how to call the
javascript function jcap() before the contactUS_Click event handler.
The event handler is server based, so I am hoping that I can just leave the
call in the form onsubmit attribute because I think that should fire first.
As for the email based script I am guessing that I should check the
page.IsPostback variable and excute it only when that is true.
Third:
I want to add some additional validation on form fields. Specifically I
would like to test for a valid email address if nothing else. I am planning
a form that has more data elements such as name, address, phone number, etc
that I expect to concatenate into the message body.
Fourth:
I want to convert the script from VB.NET to C#
' ============================================
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub contactUS_Click(sender as Object, e as EventArgs)
'Create an instance of the MailMessage class
Dim objMM as New MailMessage()
objMM.To = "(e-mail address removed)"
objMM.From = email.Text
'If you want to CC this e-mail to someone else
'objMM.Cc = "(e-mail address removed)"
'e-mail format. Can be Text or Html
objMM.BodyFormat = MailFormat.Text
'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal
'Set the subject
objMM.Subject = "1&1 Contact US"
'Set the body
objMM.Body = message.Text
'Smtp Server
SmtpMail.SmtpServer = "mrelay.perfora.net"
'Send the message
SmtpMail.Send(objMM)
panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub
</script>
<html>
<body>
<aspanel id="panelSendEmail" runat="server">
<form runat="server">
<h2>CONTACT US FORM</h2>
<b>Your Name:</b>
<asp:textbox id="name" runat="server" />
<br>
<b>Your Email Address:</b>
<asp:textbox id="email" runat="server" />
<p>
<b>Your Message:</b><br>
<asp:textbox id="message" TextMode="MultiLine"
Columns="40" Rows="10" runat="server" />
<p>
<asp:button runat="server" id="contact_us" Text="Contact"
OnClick="contactUS_Click" />
</form>
</aspanel>
<aspanel id="panelMailSent" runat="server" Visible="False">
An e-mail has been sent. We will contact you soon!
</aspanel>
</body>
</html>
If you have any idea about how to do the following, I would greatly
appreciate your help.
I have a few alterations that I would like to implement, but I am not sure
where to begin.
First:
I want to implement the code in a codebehind page instead of in the
mailform.aspx page. I am not sure where I would put the code. I also know
that there are differences in background operations between ASP.NET 1.1 and
ASP.NET 2.0, but I don't know if the code would differ much. In other stuff
I found on the internet, the SMTP server was identified in a <mailsettings>
section in the web.config file. That seems cleaner to me, but I don't know
how the code would change. I probably would skip the code identifying the
server.
Second:
I already have an old mail form that was implemented using a CGI. I also
used a javascript based CAPTCHA function in that form to cut down on
automated spam.
In that instance the old form tag included and action attribute that pointed
to the cgi and an onsubmit="return jcap()" function that invoked the
javascript; elsewhere in the page there was a call to another javascript
that implemented the image that needed to be verified. I would still like
to use that script to prevent autospammers. I am not sure how to call the
javascript function jcap() before the contactUS_Click event handler.
The event handler is server based, so I am hoping that I can just leave the
call in the form onsubmit attribute because I think that should fire first.
As for the email based script I am guessing that I should check the
page.IsPostback variable and excute it only when that is true.
Third:
I want to add some additional validation on form fields. Specifically I
would like to test for a valid email address if nothing else. I am planning
a form that has more data elements such as name, address, phone number, etc
that I expect to concatenate into the message body.
Fourth:
I want to convert the script from VB.NET to C#
' ============================================
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub contactUS_Click(sender as Object, e as EventArgs)
'Create an instance of the MailMessage class
Dim objMM as New MailMessage()
objMM.To = "(e-mail address removed)"
objMM.From = email.Text
'If you want to CC this e-mail to someone else
'objMM.Cc = "(e-mail address removed)"
'e-mail format. Can be Text or Html
objMM.BodyFormat = MailFormat.Text
'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal
'Set the subject
objMM.Subject = "1&1 Contact US"
'Set the body
objMM.Body = message.Text
'Smtp Server
SmtpMail.SmtpServer = "mrelay.perfora.net"
'Send the message
SmtpMail.Send(objMM)
panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub
</script>
<html>
<body>
<aspanel id="panelSendEmail" runat="server">
<form runat="server">
<h2>CONTACT US FORM</h2>
<b>Your Name:</b>
<asp:textbox id="name" runat="server" />
<br>
<b>Your Email Address:</b>
<asp:textbox id="email" runat="server" />
<p>
<b>Your Message:</b><br>
<asp:textbox id="message" TextMode="MultiLine"
Columns="40" Rows="10" runat="server" />
<p>
<asp:button runat="server" id="contact_us" Text="Contact"
OnClick="contactUS_Click" />
</form>
</aspanel>
<aspanel id="panelMailSent" runat="server" Visible="False">
An e-mail has been sent. We will contact you soon!
</aspanel>
</body>
</html>