ASP Email

There are two methods of sending email using asp, with different server components; CDONTS found on Windows 2000 servers, and CdoSys found on Windows 2003 servers.

Be aware that CDONTS is deprecated and may not be present on some Windows 2000 servers either, so you may need to use CdoSys.


Using CDONTS

<%

' read in the fields from your form, whatever they may be, for example:

Dim strEmail, strMsg

strEmail  = Request.Form("txtEmail")
strMsg = Request.Form("txtMsg")

Set myMail = CreateObject("CDONTS.NewMail")

myMail.From = "My Website<admin@mysite.com>"
myMail.To = strEmail
myMail.cc =   ' if required
myMail.bcc =  ' if required
myMail.Subject = "Subject"
myMail.Importance = 2  ' 0 = Low, 1 = Normal 2 = High
myMail.BodyFormat = 0  ' 0 = HTML  1 = Plain text
myMail.MailFormat = 0  'leave this out for Plain text
myMail.Body = strMsg
myMail.Send

set mymail = nothing

%>


Using CdoSys

First, you need to put a reference to the type library at the top of your asp page in comment fields, as follows: (yes it refers to Windows 2000, .......)

<!--
METADATA
TYPE="typelib"
UUID="CD000000-8B95-11D1-82DB-00C04FB1625D"
NAME="CDO for Windows 2000 Library"
-->

Then read in the form fields you are sending in the email, configure the CDO object and create the mail

<%

' read in the fields from your form, whatever they may be, for example:

Dim strEmail, strMsg

strEmail  = Request.Form("txtEmail")
strMsg = Request.Form("txtMsg")

' configure the CDO object

Set cdoConfig = CreateObject("CDO.Configuration")

With cdoConfig.Fields
  .Item(cdoSendUsingMethod) = cdoSendUsingPort
  .Item(cdoSMTPServer) = "smtpmail"
  .Update
End With

' create and send the email

Dim myMail

Set myMail = CreateObject("CDO.Message")

myMail.Configuration = cdoConfig
myMail.From = "My Website<admin@mysite.com>"
myMail.To = strEmail
myMail.To =   ' if required
myMail.bcc =   ' if required
myMail.Subject = "Subject"
myMail.HTMLBody = strMsg
myMail.Send

set myMail = nothing
set cdoConfig = nothing

%>

For a 'plain text' email, use myMail.Body, rather than myMail.HTMLBody.


Important

When sending an email from your site to yourself, say with details of an enquiry, you may be tempted to use the enquirers email address as the 'From' address. Don't!

Most servers now prevent sending email that is not from a domain on the server, to stop the server being used to relay spam.

Use a know email address associated with your site, such as admin@mysite.com.

 




Latest Microsoft Blogs
  • Murach's ASP.NET Web Programming with VB.NET Murach's ASP.NET 3.5 Web Programming with VB 2008: Anne Boehm: Books ISBN : 1890774472 ISBN-13 : 9781890774479 I love this book. If I were going to teach a course on ASP.NET Development with VB - THIS is the book I would teach it from ! Get a a copy ! Murach's Description There is no faster or better way to learn ASP.NET web programming using Microsoft’s Visual Studio .NET than to use "Murach’s ASP.NET web programming with VB.NET." To get you off to a fast start, the first 5 chapters show you how to use Microsoft Visual Studio to design, code, and test multi-page web applications that use view state, session state, cookies, database data, and code-behind files. From there, you learn even faster as you master the core professional...(read more)
  • The Importance of Community Driven Events - DevLink - OH WOW !!! I go to ALOT of developer events. Usually, they are work - meaning I probably wouldn't go if I were a retired Millionaire. DevLink was AWESOME. It was kind of link that perfect summer back in school when all the right people were in the right place and the right mood and the sun was shining and the VIBE was just happening. The basics were all there, Tennessee was very sunny but not too hot, the people are very nice, the MTSU campus is super (and the Sororities were pledging while we were there). John Kellar (DevLnk Chairman) and all the folks with the DevLink organization did a simply AMAZING job and because the event is SO cool the speaker pool is a "WHo's Who". I went to some great session like Sara Ford's session on...(read more)
  • Free Data Structures and Algorithms Book from the Slackers Data Structures and Algorithms: Annotated Reference with Examples This book written by Granville Barnett and Luca Del Tongo is part of an effort to provide all developers with a core understanding of algorithms that operate on various common, and uncommon data structures. Data Structures and Algorithms: Annotated Reference with Examples is completely free! [ CILICK HERE ] Read More......(read more)
  • Creating a UI Module For IIS7 to watch Current Requests A while back I posted about Failed Request Tracing in IIS 7 and I mentioned the command appcmd list requests which lists the requests that are currently executing in the process... I was playing around a bit with UI Modules in IIS7, you know the ones that show up in the Features view when you open up a site in IIS 7... ... and I decided to create one that displays the requests that are currently executing... This is a step-by-step of how I created it, along with a few notes about things to think about when creating them. The sample should be seen as a proof-of-concept and although you are welcome to use this sample I will advice that it is not tested and doesn't have much in terms of error handling etc. I just wanted to show how I went through...(read more)
  • IE8 Beta 2 is now out In case you haven't seen already, as announced here , IE8 Beta 2 can now be downloaded from http://www.microsoft.com/ie8 . Why is this important?  Well, Beta 1 was for developers to see the new browser and test their sites with.  This beta is about everyone.  I'd really suggest taking a look at the IE blog referenced above and see some of the new features that are in this. I haven't had a chance to install it yet, but it is on my list of things to do in the near future. Read More......(read more)
  • ASP.NET MVC Tip #39 – Use the Velocity Distributed Cache Improve the performance of ASP.NET MVC applications by taking advantage of the Velocity distributed cache. In this tip, I also explain how you can use Velocity as a distributed session state provider. The best way to improve the performance of an ASP.NET MVC application is by caching. The slowest operation that you can perform in an ASP.NET MVC application is database access. The best way to improve the performance of your data access code is to avoid accessing the database at all. Caching enables you to avoid accessing the database by keeping frequently accessed data in memory. Because ASP.NET MVC is part of the ASP.NET framework, you can access the standard ASP.NET System.Web.Caching.Cache object from your ASP.NET MVC applications. The standard...(read more)
  • Visual Web Developer support for out of band releases. Recently we have received a few requests from customers asking whether our out of band (OOB) releases (Silverlight, Dynamic Data Wizard, Model View Controller and Web Deployment Project) are supported in Visual Web Developer (VWD) Express or not. This blog post lists all the OOB releases that we have shipped during the last year and what releases are supported in Visual Web Developer currently.   VS 2008 SP1 RTM VWD 2008 SP1 RTM Timeline Silverlight Refresh Supported Not Supported We have plans to add Silverlight Tools support in VWD Express for the Silverlight 2 release. Model View Controller Supported Supported N/A Dynamic Data Wizard Supported Not Supported Dynamic Data Wizard will support VWD Express with our next code gallery release...(read more)
  • Debugging Puzzler - WinForms crash - Can you figure out what caused it? Earlier this week I was doing a presentation and since the company I presented for had mostly winforms developers I wanted to convert all my ASP.NET debugging demos to winforms equivalents for the presentation. As I was converting my first demo (a performance issue) I ran into a bit of a snag because the first time I ran the demo (which populates 4 datagridviews with product information) things worked fine. I clicked the Featured Products button, my datagrids were nicely filled, I could reproduce my performance issue and debug it. The second time I clicked the button to reproduce the issue however I was met with this: Instead of data in my datagrids they were just covered with big red X:es and the application crashed with a NullReferenceException...(read more)
  • Silverlight Tips of the Day – Week 5 Silverlight Tip of the Day #32   Title : How to Declare a Custom User Control from a XAML Page. Demo : http://silverlight.services.live.com/invoke/66033/Custom%20UserControl/iframe.html Silverlight Terrain Tutorial Part 3 Title : Creating Smooth Tile Transitions using Opacity Masks. Demo : http://silverlight.services.live.com/invoke/66033/Terrain%20Transititions/iframe.html Silverlight Tip of the Day #31 Title : How to Detect Alt, Shift, Control, Windows and Apple keys with Left Mouse Down in Silverlight Demo : http://silverlight.services.live.com/invoke/66033/Left%20Mouse%20Down/iframe.html Silverlight Tip of the Day #30 : Title : Fast Sprite Animation in Silverlight Demo : http://silverlight.services.live.com/invoke/66033/Fast%20Sprites...(read more)
  • ASP.NET MVC Tip #38 – Simplify LINQ to SQL with Extension Methods In this tip, Stephen Walther demonstrate how you can create new LINQ to SQL extension methods that enable you to dramatically reduce the amount of code that you are required to write for typical data access scenarios. By taking advantage of LINQ to SQL, you can dramatically reduce the amount of code that you need to write to access a database. LINQ to SQL has given me back several weeks of my life that I would have otherwise wasted in writing tedious ADO.NET code. In short, I am a fan. However, LINQ to SQL is a general data access technology. It is not specific to ASP.NET MVC. You can use LINQ to SQL when building Windows Forms applications, console applications, or just about any other type of Microsoft application. For this reason, it is not...(read more)
  • Billy Hollis on Making a good UI with Windows Presentation Foundation I was chatting with Billy Hollis about how we often use new technology to do the same old thing and how I was seeing lots of folks doing this with WPF and Silverlight. He mentioned that he had done a video in UI Design with WPF. Check it out, its great for WPF and SIlverlight aficionados. DNR-TV Show #115 | 6/20/2008 Billy Hollis on Getting Smart with WPF Billy Hollis shows us some clever ways to make a clean, efficient user interface in Windows Presentation Foundation. Read More......(read more)
  • ASP.NET Tip: Keep hidden files hidden I have seen a few cases of this so I wanted to let everyone know.  If you install the FrontPage Server Extensions (FPSE) on a web server, it will install some configuration files in a directory under your web site called _vti_cnf.  This folder contains a bunch of files that FPSE use to update files and perform the various tasks that it needs to do. The problem comes if that _vti_cnf folder is not marked hidden.  If something keeps that from happening, you can get a few different errors because of it.  The reason is that the aspnet_compiler.exe will try to compile these files since the folder isn't hidden.  There is a web.config file in there that will cause an error like: Invalid token 'Text' at root level...(read more)
  • Using the Ajax Control Toolkit in ASP.NET MVC Stephen Walther has a pretty cool post on using the new file-only version of the Ajax Control Toolkit from an MVC application: http://weblogs.asp.net/stephenwalther/archive/2008/08/22/asp-net-mvc-tip-36-create-a-popup-calendar-helper.aspx Read More......(read more)
  • SOS Best Practice: Match the SOS version with the process being analyzed I haven’t seen a whole of of issues around this as of yet, but now that 3.5 SP1 has released that may change.  If you copy SOS.dll to another location and then load it out of that directory to troubleshoot dumps, you may start seeing problems.  Namely, things like !clrstack not returning any valid names for functions and just printing out the numbers.  For example: 0:006> !clrstack OS Thread Id: 0x1154 (6) ESP EIP 0154cfdc 7614f35f [HelperMethodFrame: 0154cfdc] 0154d080 5fb7b8cd 0154d094 5fb7b78b 0154d0bc 5fd26907 0154d0d0 5f95a87c 0154d0e8 5f959458 0154d108 5f9634f1 0154d124 5f96342d 0154d150 65525e55 0154d17c 5e339ea3 0154d1f8 5e33ce4d 0154d37c 5e3400cc 0154d380 5dd60d14 0154d3b4 5e56def3 0154d408 5dce876b 0154d438 5dd12799...(read more)
  • Streamlined BDD Using SubSpec for xUnit.NET I admit, up until now I’ve largely ignored the BDD (Behavior Driven Development) Context/Specification style of writing unit tests. It’s been touted as a more approachable way to learn TDD (Test Driven Development) and as a more natural transition from user stories to the actual code design. I guess my hesitation to give it a second thought was that I felt I didn’t need a more approachable form of TDD. Recently, my Subtext partner in crime, Steve Harman , urged me to take another fresh look at BDD Context/Specification style tests. I trust Steve’s opinion, so I took another look and in doing so, the benefits of this approach dawned on me. I realized that it wasn’t BDD itself I didn’t like, after all, I did enjoy writing specs using minispec...(read more)