{ASP.Net 2.0} – Emailing Form Content (Update from previous post)

I added a post previously about emailing form content from an asp.net page. This is an update to that post to include changes in the .net 2.0 framework. Overall the changes are subtle, but much better. Check it out below:

I am assuming that your reading this post because you have a form with the runat=”server” attribute and well formed web 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 web 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 web 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.Net.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(ByVal Source As Object, ByVal 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, 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 textbox in the form that you want mail, you make sure that you include the web control id 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 needs 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 an index number of the selected value instead of the value. 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 MMsg As MailMessage = New MailMessage()

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

Dim MailObj As New SmtpClient(“localhost”) <-- where localhost is your outgoing smtp server. You can also programatically set this in the web.config file and continue to use the "localhost"MMsg.From = New MailAddress("[email protected]", "John Wooton") <-- This is where you would set the email address that is sending the email. This piece can take one or two variables being email address and name, but at a minumum must contain the email address or a reference of the string or resource that holds the email address. I've displayed my email address and name as an example and notice that the variables must be in quotation marks. Also note that if you send an email and set the from variables to an email address that is out of the domain name that is truly sending the mail, you can run into email deliverability problems. For example, if my domain name is inspiremediacode.com and the suffix for my email is gmail.com, the isp receiving the email will red flag it because it appears you are masking the email address as spammers do. Be careful on this one.Msg.To.Add(New MailAddress("[email protected]", "John Wooton")) <-- This is where you would set the email address to send the email to. This piece can take one or two variables being email address and name, but at a minumum must contain the email address or a reference of the string or resource that holds the email address. I've displayed my email address and name as an example and notice that the variables must be in quotation marks.MMsg.IsBodyHtml = "False" <-- This is just for this example, I will show you a way to have some html fun with your emails by setting this to TrueMMsg.Body = Msg <-- set the message to the string above.MMsg.Subject = "Your email subject"MailObj.Send(MMsg)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 SubNow I mentioned before about the possibility of sending html based emails. This is possible and very simple to do by setting the MMsg.IsBodyHtml to True. There is one kink in this. If you were to set this to true and just send the message, it would come through as a one line email because you haven't told the browser to display any page breaks. The way to fix this is to switch all vbcrlf to "
“. You can also add other markup to the email, but you must remember that whatever you add, must be enclosed in quotation marks or the server will think you are making reference to a variable in the page and throw an error.

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.