ASP/.NET Web Hosting
* 3 Months FREE *
CLICK HERE!
 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
151 active users
3119 visitors today
4683 pages today
how is this done?
ASP.NET Showcase
ASP.NET Graph and Chart Component with FREE developer version

ASP.NET Shopping Cart and E-commerce Control with FREE trial download

ASP.NET Web Hosting with free month and setup

$15 Domain Name Registration from .DNR.
As seen on these sites
Special thanks go to the sites that support me by publishing my articles.
See more

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

ASP.NET Blog
ASPRSS
ShawThing

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 = 'me@me.com;you@you.com;her@her.com'
• 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

Selling products online - read how James does it now



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


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!

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?