Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook.
ActiveX RegEdit.   ActiveX User account Manager   Pure-ASP Upload
Export MDB/DBF from ASP   Active LogFile   WebChecker   ActiveX/ASP Multi Dictionary object
 See 
 also 
 IISTracer, real-time IIS monitor and logging tool.
 Huge ASP file upload with progress bar. 



Do you like this article?
Please, rate it
and write review!
Rated:
by Aspin.com users
What do you think?
 Top messages
 22.3.2003 19:18:41 
 Read and write SQL image data, store binary file to sql table. (nbsp;WSHDatabaseConversionVBScript)
 19.9.2007 
 MS SQL - find text or command in a stored procedure (nbsp;MS SQL)
 6.2.2007 23:55:42 
 HttpWebRequest and binary document, VB.Net code (nbsp;FunctionsVB.Net)
 21.2.2003 12:58:38 
 Create html bar graph in ASP/VBSscript (nbsp;DatabaseFunctionsVBScript)

 Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook. 

 Areas>ASP / ASP.Net>Email
 Areas>Languages>VBScript
 Areas>WSH
 Areas>ASP / ASP.Net
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 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

  '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 

%>	
 
 

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.

If you like this page, please include next link on your pages:
<A
 Href="http://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>

     IISTracer - IIS ISAPI real-time monitor IISTracer is a real-time monitoring tool for Microsoft IIS, which will show/log you what is happenning on IIS server right now. It let's you reveal problems with long-running scripts (.asp, .cgi, cfm...), hang-up states and low resource situations and lets you stop long-running requests (uploads/downloads).      ActiveX User account Manager - Set of simple objects for creating, deleting, and managing user accounts, groups, servers and domains in the Windows NT environment.
     Active log file - Hi-performance text file logging for ASP/VBS/VBA applications. Lets you create daily/weekly/monthly log files with variable number of logged values and extra timing and performance info.      ActiveX windows registry editor - Intuitive, easy to use COM interface to windows registry. Set of classes to read/enumerate/modify windows registry keys and values from ASP, VBS and T-SQL.
     ActiveX/ASP Multi Dictionary object - Free-threaded hi-speed dictionary algorithm with unique/nonunique keys (map/multimap). Connect to another dictionary object in the same process. Lock and Unlock methods to synchronize tasks (application scope). Share ASP Application/Session objects.      Export DBF/MDB from ASP - Conversion from recordset to MDB/DBF. Direct binary output of MDB or DBF files from ASP pages with one row of code.
     Pure-ASP upload - lets you upload files using Pure ASP VBS code (using multipart/form-data and input type=file).      ByteArray - Works with safearray binary data (VT_UI1 | VT_ARRAY) - save/restore binary data from disk, find, work with code pages, convert to string/hexstring(SQL).
     WebChecker - Checks http, https, ftp and gopher internet connections in regular intervals. Lets you monitor web site functionality (uptime). Enables restart or notification on problems.      HTTPLog ISAPI filter - Lets you log incomming/outgoing http header and document data to separate files. Monitor of IIS service input/output.

© 1996 – 2010 Antonin Foller, PSTRUH Software, e-mail help@pstruh.cz