InspireMediaInspireMedia
Menu
0

No products in the cart.

  • Home
  • What We Do
  • Blog
  • Plugins
  • Contact

{ASP.NET} – Emailing Form Content

  • Home
  • Blog
  • {ASP.NET} – Emailing Form Content

{ASP.NET} – Emailing Form Content

CategoriesDevelopment

John Wooton

March 26, 2006

0 0

Share this post

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 enother person using the asp.net postback fature 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 nameshould start like this:

Sub SendEmail(Sender As Object, e As EventArgs)

Then we define the entire message as one vairable 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 seperated 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 adio 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 anothew 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=[email protected] <–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.

Related Post

DECEMBER 6, 2017

New Website: Erin Hession...

Today we launched a new design revamp for Erin Hession Photography. Erin Hession...

00

JUNE 13, 2016

New Website: Le Jour du...

Le Jour du Mariage (which is French for “The Wedding Day”) is a sister...

00

FEBRUARY 14, 2016

New Website: Wilson Kehoe...

This past week we put the finishing touches on a brand new website revamp for our friends...

00

AUGUST 25, 2015

New Website: Same Day Courier...

Same Day Courier Service was referred to InspireMedia because they were looking for a...

00

APRIL 1, 2015

Will Your Site Be Penalized...

Late last year, Google began testing for and highlighting websites in their search...

00

MARCH 23, 2015

Gravity Forms VacationCRM...

Recently we published a new WordPress plugin that integrates Gravity Forms with our...

00

Leave a Comments Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • December 2017
  • June 2016
  • February 2016
  • October 2015
  • August 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • May 2014
  • March 2014
  • January 2014
  • November 2013
  • October 2013
  • May 2013
  • March 2013
  • March 2012
  • October 2011
  • September 2011
  • July 2011
  • June 2011
  • May 2011
  • November 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • July 2009
  • March 2009
  • February 2009
  • January 2009
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • August 2006
  • June 2006
  • May 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005

© 2022 InspireMedia, All Rights Reserved.