Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » ASP .NET
 {ASP.NET} - Emailing Form Content
Pages: [1]   Go Down
  Print  
Author Topic: {ASP.NET} - Emailing Form Content  (Read 2367 times)
Daniel Franklin
TWI Hero
**********


Karma: 3
Offline Offline

Posts: 16647


View Profile Email
{ASP.NET} - Emailing Form Content
« Posted: September 29, 2007, 09:46:02 AM »




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.

[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.

Articles Source - Free Articles

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.11 seconds with 24 queries.