Quantcast

ASP.NET – Emailing Form Content

Get the WebProNews Newsletter:


[ Business]

One of the problems that I first encountered when I was learning the ropes was that a book can never tell you everything.

So as I was struggling to learn the development techniques that I apply, I had to frequently rely on online resources or forums to get the job done. This was not easy, especially when you are trying to solve a problem and all you get is errors.

So, one of my goals with my blog was to help all of you out there who are struggling to get on your feet with new technologies. It’s kind of like repayment for all those out there who have helped me and still continue to help me learn.

So today, I want to show you how it is possible to email form content to yourself or another person using the asp.net postback feature and a little creativity.

I am assuming that your reading this post because you have a form with the runat=”server” attribute and well formed server controls already ready to go. Server controls take quite a long time to explain so if you don’t already have your form ready to go, I suggest you read up on the many tutorials available that can teach you about server controls.

So how is it possible to do this on the page. Well once you understand how it works, you’ll never forget how simple it is.

First you have to make sure your button server control has the attribute OnClick. The value for OnClick will be equal to the value of the Sub control that will send the email. The value I selected for the OnClick attribute is “SendEmail”. Here’s what it looks like: In the code section at the top of the page, make sure you import the right Namespace so that the right classes will load with the page. The only one for this task that you have to worry about is the System.Web.Mail Namespace. For vb users, the correct usage just below the page attribute at the top of the page is this:

For the Sub which should follow the OnClick selection and have the same name should start like this:

Sub SendEmail(Sender As Object, e As EventArgs)

Then we define the entire message as one variable Dim:

Dim msg as String

And for each line in the message you start it off with the name of the variable and followed by the “+=” sign to add it to the variable. Then for text that you want to display like a label or header or something, you make sure that it is enclosed in quotation marks and for variables that you are bringing into the massage (ie: for the text from a taxtbox in the form that you want mail, you make sure that you include the technical name for that selection. For instance, let me write out a couple lines of code so you can see how it all works and then I’ll describe the details so you can understand:

Dim msg as String

msg+="Form mailer header" & vbcrlf

msg+="Contact Name : " & contactname.Text & vbcrlf

msg+="Contact Email : " & email.Text & vbcrlf

msg+="Agree To Terms : " & terms.SelectedValue & vbcrlf

Now I’ve included a couple of different items here so you can understand. First, I made sure I included the call on the variable:

Dim msg as String

This is so you can begin to put the pieces together. Then I included one line that I want as full text and include no values from the form.

msg+="Form mailer header" & vbcrlf

Notice I included something new: vbcrlf. This tells the parser to create a new line in the email. Everything that is text is fully enclosed in the quotation marks and there is a space between that and the character &, then there is another space and the vbcrlf code. This would make one complete line in your email message.

msg+="Contact Name : " & contactname.Text & vbcrlf

The next line is a mix of two different items. First we have some text that we want to display, then the value of one of the items from the form. This is separated by spaces and the & character and the item from the form is now listed as contactname.Text. The text portion help the server identify that the text that was in the server control with the id of “contactname” is what is needed to be placed there. The next line is also similar to this.

msg+="Agree To Terms? " & terms.SelectedValue & vbcrlf

The final value that I’ve listed in this example is similar, but has a different suffix for the id information that you want to pass along. Instead of .Text, it has .SelectedValue. This takes the place of the .Text suffix when you have a drop-down server control or multiple choice selection, as in the case of a group of radio buttons. This would identify the selected value from that group. There is also another one for this specific case and that is .SelectedIndex. This gives a number instead of the value. It’s the number of the value in the order they are presented in the list. So if I have a list of three possible answers or selection, and they choose the second one down, the .SelectedIndex would return the number 2 instead of the text for that selection. This is especially helpful if you are querying a database, but that’ll be saved for another lesson.

So once you have your entire message laid out line by line we’ll create another variable which will actually help to pull everything together and define the direction of the message variable “msg”.

Dim MailObj as New MailMessage

This actually puts all the various pieces together. Under that, you need to add a few variables. The System.Web.Mail class that we imported into the file will be able to identify these and utilize them properly.

MailObj.To=johnpwooton@gmail.com <-put your email address, I added mine to give you an example.

MailObj.FROM=email.text <- specify an email address in quotes or include the declaration for the server control that is passing along the address.

MailObj.SUBJECT="Form Mail"

MailObj.BODY=msg

MailObj.BodyFormat = MailFormat.Text

SmtpMail.SmtpServer ="your.smtpserver.com" <- your smtp server address. You must have this to send the message.

SmtpMail.Send(MailObj) <-tells the Sub to send the message.

You can also add an optional redirect to another page that tells them the form mailer was a success by using this before you end the Sub:

Response.Redirect("thankyou.aspx")

and of course, you have to end the Sub properly: End Sub

So that's it. Pure and simple. Of course if you have any questions, post a comment and I'll do what I can to help you out. I have used this particular script on other pages on my sites and it has worked very well.

Add to | DiggThis | Yahoo! My Web

Technorati:

John Wooton

Author and Creator, The SEO Journal Blog http://seojournal05.blogspot.com

ASP.NET – Emailing Form Content
Comments Off
Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom