|
See also: Email account exporter, Export configuration of email accounts for Outlook and Express directly from an ASP page.
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
'Set email adress, subject And body
.To = aTo
.Subject = Subject
.Body = TextBody
'set mail And body format
.MailFormat = CdoMailFormatText
.BodyFormat = CdoBodyFormatText
'Set sender address If specified.
If Len(aFrom) > 0 Then .From = aFrom
'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
Dim Message As New CDO.Message
'Create CDO message object
Set Message = CreateObject("CDO.Message")
With Message
'Load IIS configuration
.Configuration.Load cdoIIS
'Set email adress, subject And body
.To = aTo
.Subject = Subject
.TextBody = TextBody
'Set sender address If specified.
If Len(aFrom) > 0 Then .From = aFrom
'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
Dim Message 'As New CDO.Message
'Create CDO message object
Set Message = CreateObject("CDO.Message")
With Message
'Set cached configuration
Set .Configuration = Conf
'Set email adress, subject And body
.To = aTo
.Subject = Subject
.TextBody = TextBody
'Set sender address If specified.
If Len(aFrom) > 0 Then .From = aFrom
'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. See more in Email from ASP - Using external smtp server and CDO article.
...
'cached configuration
Static Conf 'As New CDO.Configuration
If IsEmpty(Conf) Then
Const cdoSendUsingPort = 2
Set Conf = CreateObject("CDO.Configuration")
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)
'Create an Outlook object
Dim Outlook 'As New Outlook.Application
Set Outlook = CreateObject("Outlook.Application")
'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
'Set destination email address
.Recipients.Add (aTo)
'Set sender address If specified.
Const olOriginator = 0
If Len(aFrom) > 0 Then .Recipients.Add(aFrom).Type = olOriginator
'Send the message
.Send
End With
End Sub
|
5. Send message with client-side attachment - Upload to email
This live sample is located on https://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
'Attach some file.
objNewMail.AttachFile FileName
'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
'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"--><%
'Do Not upload data greater than 1MB.
Form.SizeLimit = &H100000
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
Response.Write ResultHTML
Function ProcessForm
Dim eFrom, eTo, Subject, Message
'get source form fields - From, To, Subject And Message
eFrom = Form("From")
eTo = Form("To")
Subject = Form("Subject")
Message = Form("Message")
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>"
Dim objNewMail, File, FileName, FS, TempFolder
Set FS = CreateObject("Scripting.FileSystemObject")
'Get temporary folder To store uploaded file
TempFolder = FS.GetSpecialFolder(2) & "\emailtemp"
'Create a new email message
Set objNewMail = CreateObject("CDONTS.NewMail")
Const CdoMailFormatMime = 0
objNewMail.MailFormat = CdoMailFormatMime
'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>"
FileName = TempFolder & "\" & File.FileName
'Store the file To temporary folder
File.SaveAs FileName
'attach it To the NewMail object
objNewMail.AttachFile FileName
End If
Next
'Send the new email
objNewMail.Send eFrom, eTo, Subject, Message
'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
HTML = HTML & "</Font><br>"
ProcessForm = HTML
End Function
%>
|
Copyright and use this code
The source code on this page and other samples at https://www.motobit.com/tips/
are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
or
2. Link this sample from you page.
<A
Href="https://www.motobit.com/tips/detpg_send-email-from-asp/"
Title="Short samples which lets you send
an email from ASP or
WSH, using several objects (CDONTS.NewMail,
CDO.Message, Outlook.Application) and VBScript"
>Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook.</A>
|