Emailing Form Results in ASP.NET

The task at hand is taking the input from a form, and emailing those results to a desired target. It’s not as hard as one might think. Many of you probably worked with the CDO object in Classic ASP to get this done. If so, you’re much closer to understanding this than you think. Even if you are new to programming, you’re going to be amazed at how simple this really is.

The first thing that’s necessary, of course is the form. For this example, we’re using VB.Net and ASP.Net server controls. Naturally, for the purposes of this tutorial, we’re going to keep it fairly simple, but the same principles apply for a much more expanded form. Here’s a sample of the HTML section for the form:

<Form id="form1" runat="server">
First Name: <asp:TextBox id="txtFname" runat="server" />
Last Name: <asp:TextBox id="txtLname" runat="server" />
Email: <asp:TextBox id="txtEmail" runat="server" />
<asp:Button id="btnEmail" Text="Email Form" onclick="doEmail" runat="server" />
</Form>

The next thing to make sure we have listed on our page is a reference to the correct namespace. For emailing, it’s:

<%@ Import Namespace=”System.Web.Mail” %>

As you probably noticed, there is an ‘onclick’ event referenced for the form’s button, ‘doEmail‘. This is where we’ll take the output of the form and do the actual emailing of the input.

Sub doEmail(Source as Object, E as EventArgs)
	Dim sMsg as String
	sMsg+="Hi there - here's the information I entered in the form." & vbcrlf
	sMsg+="First Name : " & txtFname.Text & vbcrlf
	sMsg+="Last Name : " & txtLname.Text & vbcrlf

	Dim objEmail as New MailMessage
	objEmail.To=txtEmail.text
	objEmail.BCC="JohnnyWhite@jwmason.com" '<--- this would be where you can send it to yourself at the same time.
	objEmail.FROM="me@here.com"

	objEmail.SUBJECT="Here's the subject of the email"
	objEmail.BODY=sMsg
	objEmail.BodyFormat = MailFormat.Text
	SmtpMail.SmtpServer ="mail.Wherever.com"
	SmtpMail.Send(objEmail)
End Sub

In this sub, you’ll notice that the first thing we’re doing is creating a string variable to store our email message. The first part of the message is the opening line, but then you’ll see we’re adding and formatting the specific data from the form, item by item. Naturally, that can be formatted pretty much most anyway you’d like.

Next, we actually reference and instantiate the email object (MailMessage), followed by the essential, basic parts of any email message, the To, From, Subject and Body segments, along with the CC (carbon copy) section. We could also add a BCC section if wanted (Blind Carbon Copy). Remember that string variable we created at the first of the subroutine? We assign it in the Body section. You’ll probably notice that there are no quote marks around sMsg, and that’s BECAUSE it’s a variable. If you wanted to hard code a BODY section here, you’d surround it with double quotes.

Before sending the message, there are two things necessary – assigning a format (Text or HTML), and assigning an SMTP server. If you are using a hosted site, many of them use the format – ‘mail.Wherever.com‘, with ‘Wherever.com’ section being your own domain name. In this case, we’re using a pure text format, but, just as easily, we could have used an HTML format:

objEmail.BodyFormat = MailFormat.HTML

You could then use just about any valid HTML markup tags you wanted inside the BODY section. Of course, you’d need to make sure the person to whom the email is being sent (most likely YOU, in this case) can accept HTML emails correctly. Then, the last line is what really creates and sends the email message:

SmtpMail.Send(objEmail)

Naturally, you’d probably want to add in some validation (ASP.Net validation will be addressed in another Tutorial here), so blank form field results are not sent in the email. So, here you have it in a nutshell – create an input form, along with a subroutine to actuallysend the email, and you’re ready to go. The user just fills in the information for which you asked, clicks the ‘Email Form’ button and the email is on it’s way.

‘What about attachments?’, you may ask. That’s a very good question. In continuing with my ‘It’s not as hard is it may sound’ way of presenting – that’s exactly right – it’s very simple. All you need to do (before the SMTP.Send method, of course is to add this line:

objEmail.Attachments.Add(New MailAttachment(server.mappath(“/yourpath/yourfile.txt”)))

The only catch here is that the file that you’re sending MUST to be ON the file server, and the path you include here needs to be a valid path, either using ‘Server.Mappath’, or an explicit path.

Well – that’s all there is to it…no pain, right?

You might want to check out another Emailing Tutorial on this site, entitled ‘Sending Multiple Emails at Once (Mass Emails)‘ also.

Related Posts:

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!