Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
The Best Place Yet for ASP.Net - ASPNet101.com
Power Search


Don't Miss any New Content!
Sign up for Newsletter!

  Menu  
  Code Samples
Tips and Tricks
Tutorials
ASP.Net FAQ
Training
Resources
Hosting
Recommended Books
Home
 
     
  Our Sponsors :  
 
ASP Express WebHost4Life DotNetSlackers
 
     

72
If you find this site helpful, please donate to help keep it online


     
 
   

Emailing Form Results
(It's much easier than you might think)

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 actually send 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.

   
Choose From Tutorials:
    Beginners Guide to Functions    
    Defining Error Displays Globally    
    Creating a Method to Return a DataSet in C#    
    Creating a Feedback Form    
    Beginners Guide to Comparing Strings    
    Introduction to Parsing Strings    
    Beginners Guide: Scoping of Methods    
    Adding 'Last Updated' To Footer    
    A Beginners Guide to the HyperinkField    
    Error Trapping in ASP.Net    
    Page Level Impersonation    
    Creating a Hit Counter    
    Inserting and Updating with a SQLDataSource    
    Sorting/Filtering Fixed DataSet Results    
    Using PreviousPage with a Master Page    
    The Basics of Using SQL    
    Helper Functions    
    Querystring Results and SQL Statements    
    A Beginners Guide to the Connection String    
    Beginners Guide to Com in ASP.Net    
    Membership/Roles with Remote DB    
    Creating a Login/Email/Activation Page    
    Multiple DropdownLists with AppendDataBoundItems    
    The Beginner's Guide to an ArrayList    
    Creating a Popup Details Page    
    Configuring a Trusted SQL Srvr Connection    
    Beginners Guide to the BulletedList    
    Adding Dynamic Content to Your Pages    
    Emailing Form Results with ASP.Net 2.0    
    Menu User Control with Rollovers    
    Using TemplateFields    
    Transferring Form Results to 2nd Page    
    Buttons and LinkButtons    
    Using the AdRotator Control    
    Two Page Data Retrieval    
    The 3 'Execute' Command Methods    
    Using the QueryStringParameter    
    Displaying DataBase Information    
    Beginner's Guide to Master Pages    
    Sessions/Visitors Online    
    Beginner's Guide to Themes    
    Sending Multiple Emails At Once    
    Sending Emails With ASP.Net 2.0    
    Emailing Form Results    
    A Beginner's Guide to the GridView    
    Inserting Data Into Two Tables    
    A Beginner's Guide to the DataSource Control    
    File System List With System.IO    
    Coolest New v2.0 Additions    
    Understanding Regular Expressions    
    Remote SQL Server/Web Matrix Server/WinXP Home    
    Parameterized Queries - Part 2    
    Creating a 'States' User Control    
    Creating an Online Bible    
    Iterating Through a List-Type Server Control    
    Nesting Server Controls    
    Beginner's Guide - Installing MSDE    
    String Properties/Methods - Part 2    
    Function Libraries    
    String Properties/Methods - Part 1    
    An Introduction to Validation Controls - Part I    
    Examining List Controls    
    Sending Mass Emails Part 2    
    If Not Page.IsPostBack    
    Beginners Guide to Forms Authentication    
    Single & Double Quotes in SQL    
    Using MySQL with ASP.NET    
    Parameterized Queries in ASP.Net    
    Matching Regular Expressions    
 
     
Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
Send us your comments, questions or suggestions : Suggestions
Copyright © 2010 ASPNet101.com All Rights Reserved