几种基于ASP页面的邮件发送技术大全
Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook.
There are several ways to send an email from VBS (ASP, WSH, IE-HTA, ...) or from VBA (Word, Excel). This page contains sample code to send simple email message for CDO, CDONTS and Outlook objects.
Please see Pure/Huge asp file upload to send emails with attachment from a client side. Live sample of upload a file and send it by email is on Huge-ASP file upload to email sample page.
1. IIS SMTP service, CDONTS.NewMail
If you have IIS SMTP service installed on your machine, you can send an email using the service and CDONTS.NewMail object. This object lets you send text or HTML emails (with attachment/images) by a simple way, but its performance is not so good. You can send only several few emails per second. Next sub shows basic CDONTS.NewMail idea.
Sub
SendMailCDONTS(aTo, Subject, TextBody, aFrom)
Const
CdoBodyFormatText
=
1
Const
CdoBodyFormatHTML
=
0
Const
CdoMailFormatMime
=
0
Const
CdoMailFormatText
=
1
Dim
Message
'
As New cdonts.NewMail
'
Create CDO message object
Set
Message
=
CreateObject
(
"
cdonts.NewMail
"
)
With
Message
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set email adress, subject And body
.To
=
aTo
.Subject
=
Subject
.Body
=
TextBody
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
set mail And body format
.MailFormat
=
CdoMailFormatText
.BodyFormat
=
CdoBodyFormatText
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set sender address If specified.
If
Len
(aFrom)
>
0
Then
.From
=
aFrom
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Send the message
.Send
End
With
End Sub
2. IIS SMTP service, CDO.Message
CDO for W2k lets you send an email using any SMTP server - you can send the email with IIS SMTP service also.
Sub
SendMailCDO(aTo, Subject, TextBody, aFrom)
Const
cdoOutlookExvbsss
=
2
Const
cdoIIS
=
1
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Dim
Message
As
New
CDO.Message
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Create CDO message object
Set
Message
=
CreateObject
(
"
CDO.Message
"
)
With
Message
'
Load IIS configuration
.Configuration.Load cdoIIS
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set email adress, subject And body
.To
=
aTo
.Subject
=
Subject
.TextBody
=
TextBody
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set sender address If specified.
If
Len
(aFrom)
>
0
Then
.From
=
aFrom
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Send the message
.Send
End
With
End Sub
You can cache configuration and use it multiple times to get a better performance of the object. CDO.Message can send 5 times more messages in the same time than CDONTS.
Sub
SendMailCDOCacheConf(aTo, Subject, TextBody, aFrom)
'
cached configuration
Static Conf
'
As New CDO.Configuration
If
IsEmpty
(Conf)
Then
Const
cdoOutlookExvbsss
=
2
Const
cdoIIS
=
1
Set
Conf
=
CreateObject
(
"
CDO.Configuration
"
)
Conf.Load cdoIIS
End
If
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Dim
Message
'
As New CDO.Message
'
Create CDO message object
Set
Message
=
CreateObject
(
"
CDO.Message
"
)
With
Message
'
Set cached configuration
Set
.Configuration
=
Conf
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set email adress, subject And body
.To
=
aTo
.Subject
=
Subject
.TextBody
=
TextBody
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set sender address If specified.
If
Len
(aFrom)
>
0
Then
.From
=
aFrom
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Send the message
.Send
End
With
End Sub
3. Remote SMTP server, CDO.Message
If you want to send an email using another SMTP server, you can set configuration properties to the server. ...
'
cached configuration
Static Conf
'
As New CDO.Configuration
If
IsEmpty
(Conf)
Then
Const
cdoSendUsingPort
=
2
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Set
Conf
=
CreateObject
(
"
CDO.Configuration
"
)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
With
Conf.Fields
.Item(
"
http://schemas.microsoft.com/cdo/configuration/sendusing
"
)
=
_
cdoSendUsingPort
.Item(
"
http://schemas.microsoft.com/cdo/configuration/smtpserver
"
)
=
_
"
any smtp server
"
.Update
End
With
End
If
...
4. Send message using Outlook
You can send emails using Outlook also. You can send email over Microsoft Exchange with this object (or another email server, using IMAP/POP).
Sub
SendMailOutlook(aTo, Subject, TextBody, aFrom)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Create an Outlook object
Dim
Outlook
'
As New Outlook.Application
Set
Outlook
=
CreateObject
(
"
Outlook.Application
"
)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Create e new message
Dim
Message
'
As Outlook.MailItem
Set
Message
=
Outlook.CreateItem(olMailItem)
With
Message
'
You can display the message To debug And see state
'
.Display
.Subject
=
Subject
.Body
=
TextBody
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set destination email address
.Recipients.Add (aTo)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Set sender address If specified.
Const
olOriginator
=
0
If
Len
(aFrom)
>
0
Then
.Recipients.Add(aFrom).Type
=
olOriginator
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Send the message
.Send
End
With
End Sub
5. Send message with client-side attachment - Upload to email
This live sample is located on http://www.motobit.com/util/upload/upload.asp URL. The sample uses Pure-ASP file upload (free ASP include) or Huge-ASP file upload (hi-performance upload component).
Each of email objects have a method to attach one or more files located on disk, the base idea is bellow.
'
Create a new email message
Set
objNewMail
=
CreateObject
(
"
CDONTS.NewMail
"
)
Const
CdoMailFormatMime
=
0
objNewMail.MailFormat
=
CdoMailFormatMime
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Attach some file.
objNewMail.AttachFile FileName
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Send the new email
objNewMail.Send eFrom, eTo, Subject, Message
Next is a full source code of upload-to-email sample. The sample processes source HTML form with From, To, Subject and Message fields and one or more file fields, then uses AttachFile method of CDONTS.NewMail object to put uploaded files into email
<
%
'
Sample file Form-Email.asp
'
Let's you send one an email with one Or more attachments from client-side disk.
'
Variable with output/result data of the form processing
Dim
ResultHTML
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Create upload form
'
Using Huge-ASP file upload
'
Dim Form: Set Form = Server.CreateObject("ScriptUtils.ASPForm")
'
Using Pure-ASP file upload
Dim
Form:
Set
Form
=
New
ASPForm %
><
!
--
#INCLUDE FILE
=
"
_upload.asp
"
--><
%
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Do Not upload data greater than 1MB.
Form.SizeLimit
=
&
H100000
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Const
fsCompletted
=
0
Const
fsSizeLimit
=
&
HD
If
Form.State
=
fsCompletted
Then
'
Completted, form OK
ResultHTML
=
ProcessForm
Else
Select
Case
Form.State
Case
fsSizeLimit:
'
client sends too big file
ResultHTML
=
"
<br><Font Color=red>Source form size (
"
&
_
Form.TotalBytes
&
"
B) exceeds form limit (
"
&
_
Form.SizeLimit
&
"
B)</Font><br>
"
End
Select
End
If
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Response.Write ResultHTML
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Function
ProcessForm
Dim
eFrom, eTo, Subject, Message
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
get source form fields - From, To, Subject And Message
eFrom
=
Form(
"
From
"
)
eTo
=
Form(
"
To
"
)
Subject
=
Form(
"
Subject
"
)
Message
=
Form(
"
Message
"
)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Dim
HTML
HTML
=
"
<br><Font Color=red>Server-side ASP script accepted source form
"
&
_
"
with fields And files And email object was created.
"
HTML
=
HTML
&
"
<br><br>Email parameters:
"
HTML
=
HTML
&
"
<br>From: <b>
"
&
eFrom
&
"
</b>
"
HTML
=
HTML
&
"
<br>To: <b>
"
&
eTo
&
"
</b>
"
HTML
=
HTML
&
"
<br>Subject: <b>
"
&
Subject
&
"
</b>
"
HTML
=
HTML
&
"
<br>Message: <b>
"
&
Message
&
"
</b>
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Dim
objNewMail, File, FileName, FS, TempFolder
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
Set
FS
=
CreateObject
(
"
Scripting.FileSystemObject
"
)
'
Get temporary folder To store uploaded file
TempFolder
=
FS.GetSpecialFolder(
2
)
&
"
emailtemp
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Create a new email message
Set
objNewMail
=
CreateObject
(
"
CDONTS.NewMail
"
)
Const
CdoMailFormatMime
=
0
objNewMail.MailFormat
=
CdoMailFormatMime
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Save source files To temporary folder
'
Add these files To the new e-mail
For
Each
File In Form.Files
'
If source file is specified.
If
Len
(File.FileName)
>
0
Then
HTML
=
HTML
&
"
<br>
"
&
File.Name
&
"
: <b>
"
&
_
File.FileName
&
"
,
"
&
File.Length
1024
&
"
kB</b>
"
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
FileName
=
TempFolder
&
"
"
&
File.FileName
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
Store the file To temporary folder
File.SaveAs FileName
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
attach it To the NewMail object
objNewMail.AttachFile FileName
End
If
Next
'
Send the new email
objNewMail.Send eFrom, eTo, Subject, Message
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
'
delete temporary files
For
Each
File In Form.Files
If
Len
(File.FileName)
>
0
Then
FileName
=
TempFolder
&
"
"
&
File.FileName
on
error
resume
Next
FS.DeleteFile FileName
End
If
Next
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
HTML
=
HTML
&
"
</Font><br>
"
ProcessForm
=
HTML
End Function
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
%
>
See also for 'Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook.' article:
Email from ASP - Using external smtp server and CDO
Check if a pop3 email account exists in a windows 2003 pop3 service Let's you check if a pop3 user email account exists in a windows 2003 pop3 service
Send email from MS SQL using CDO. This stored procedure lets you send an email using CDO.