CoverYourASP --> Sending email from a form --> Part 4

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
15 active users
94 visitors today
92 pages today
(only part of today)
Tools I use

CoverYourASP
Copyright © 1999-2012 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

Using the SendEmail( ) function

This function is used to send all the emails throughout the site - so it's the only place that cares what email system you are using. Currently, 3 components are supported, but I'll support whatever else you ask for, within reason!

• Microsofts' "Collaboration Data Objects for Windows NT Server", commonly known as CDONTS.
• Persits' ASPEmail component.
• ServerObjects' ASPMail component.
• Dimac's w3 JMail component.

The email component to use is specified by the nEmailServer setting in include/config.asp.

Using CDONTS

// get a mail object
oMail = Server.CreateObject ( "CDONTS.NewMail" );

// setup the mail
if ( sFromEmail == "" )
   oMail.From = 'Anonymous';
else
   oMail.From = sFromEmail;

var sEmailList = sToEmail.split ( /[\s;,]/ );
var nEmail;
var sMail = '';

for ( nEmail in sEmailList )
   sMail += sEmailList [ nEmail ] + ';';

oMail.To = sMail;

sEmailList = sBccEmail.split ( /[\s;,]/ );
sMail = '';

for ( nEmail in sEmailList )
   sMail += sEmailList [ nEmail ] + ';';

oMail.Bcc = sMail;
oMail.Importance = 1;

// if you want HTML mail...
// uncomment the next two lines
// oMail.BodyFormat = 0;
// oMail.MailFormat = 0;

// if you want to add an attachment...
// uncomment the next line
// oMail.AttachFile ( 'c://autoexec.bat' );

oMail.Subject = sSubject;
oMail.Body = sBody;

// send it
oMail.Send ( );

Sending a simple email is very easy. This is literally the only code I've ever used. Start by creating an instance of the CDONTS.NewMail object, then fill in the relevant properties and call the Send( ) method.

Some things to note though:

• In order to specify multiple To, Cc or Bcc recipients of a message, simply separate the addresses with a semicolon...
       oMail.To = '[email protected];[email protected];[email protected]'
• Although the documentation says that you can send in an optional caption to the AttachFile method, it's never worked for me!
• After the call to Send( ), the object is invalid. Do not try to use it again - you must create a new object.

Part 5: Using JMail...

Featured sponsor
My favorite resources


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!