About · Site feedback
welcome  Products  Scripts  Downloads  Forum  Contact us  Support  Resellers 
 
Scripts
 
PHP
JavaScript
Visual Basic
 
External downloads
If you use WSH or just VBScript / JScript, then the most important download to have is the Windows script host documentation (version 5.6).
 
Legal information
End User License Agreement
Non-commercial license
Disclaimer and legal notice
 
 
VB Script to send mail using CDO
(Windows 2000, XP, 2003, Vista, 7, 2008)

Sending an e-mail with Collaboration Data Objects (CDO) is a simple task.
1. First we create a reference object to the CDO component.
2. Then set the required From (or Sender), Subject and recipient To fields.
3. Add the body text which can be either plain text or HTML.
4. Optionally add a file attachment.
5. Finally use Send to actually send the e-mail.
Sending a plain text e-mail
Set oMsg = CreateObject("CDO.Message")
oMsg.To = "test@headquarters.com"
oMsg.From = "me@email.com"
oMsg.Subject = "Plain text CDO example"
oMsg.TextBody = "This is a plain text body."
oMsg.Send

The main benefits of using plain text mail are that it can be viewed in any e-mail client, it is small and less likely to be considered spam.
Note: To have multiple lines in the TextBody, separate each line with VbCrLf e.g. oMsg.TextBody = "This is a plain text body." + VbCrLf + "Regards" + VbCrLf + "Anon"


Sending a HTML e-mail
Set oMsg = CreateObject("CDO.Message")
oMsg.To = "test@headquarters.com"
oMsg.From = "me@email.com"
oMsg.Subject = "HTML CDO example"
oMsg.HTMLBody = "<h3>This is a HTML body.</h3>"
oMsg.Send

To use a HTML file instead of the HTML code replace oMsg.HTMLBody = "<h3>This is a HTML body.</h3>" with oMsg.CreateMHTMLBody("file://body.html") (using the fully qualified path to the HTML file).


Sending a plain text e-mail with an attachment
Set oMsg = CreateObject("CDO.Message")
oMsg.To = "test@headquarters.com"
oMsg.From = "me@email.com"
oMsg.Subject = "Plain text with attachment CDO example"
oMsg.TextBody = "This is a plain text body."
oMsg.AddAttachment("C:\image1.jpg")
oMsg.Send

To add more attachments, just repeat the oMsg.AddAttachment("") line with each of the required files.
Note: AddAttachment requires a fully qualified path or the error 800C000D: The specified protocol is unknown will be produced.


Requesting a return receipt and delivery status notification
Requesting a Delivery Status Notification (DSN) is a little more involved as it requires changing the default configuration to use an SMTP server (normally just localhost), adding a return receipt recipient and setting the DSNOptions to the required value.
Set oMsg = CreateObject("CDO.Message")
set oConf = CreateObject("CDO.Configuration")

' Set the configuration fields for the local SMTP server
' Note: This is required for the delivery status notification
Set Flds = oConf.Fields
Flds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 ' cdoSendUsingPort
Flds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
Flds.Update

oMsg.To = "test@headquarters.com"
oMsg.From = "me@email.com"
oMsg.Subject = "HTML CDO example with delivery receipt"
oMsg.HTMLBody = "<h3>This is a HTML body.</h3><br><br>Kind regards<br>E-mail: <a href='mailto:me@email.com'>me@email.com</a><br>"
'oMsg.TextBody = "This is a plain text body" + VbCrLf + VbCrLf + "Kind regards" + VbCrLf + "E-mail: me@email.com" + VbCrLf

' Set the message object to use the new configuration
Set oMsg.Configuration = oConf
' Add the address that the receipt is to be sent to
oMsg.fields("urn:schemas:mailheader:return-receipt-to") = "me@email.com"
' Set delivery status notification (DSN) 
'  Name                      Value   Description
'  cdoDSNDefault             0       No DSN commands are issued.
'  cdoDSNNever               1       No DSN commands are issued.
'  cdoDSNFailure             2       Return a DSN if delivery fails.
'  cdoDSNSuccess             4       Return a DSN if delivery succeeds.
'  cdoDSNDelay               8       Return a DSN if delivery is delayed.
'  cdoDSNSuccessFailOrDelay  14      Return a DSN if delivery succeeds, fails, or is delayed.
oMsg.DSNOptions = 14 ' cdoDSNSuccessFailOrDelay
oMsg.fields.update

oMsg.Send


Requesting a read receipt notification
Adding a Mail Delivery Notification (MDN) report is a little simpler than the delivery status notification as there is no need to change the configuration. All that is required is to set the MDNRequested flag to true and add the notification address.
Set oMsg = CreateObject("CDO.Message")

oMsg.To = "test@headquarters.com"
oMsg.From = "me@email.com"
oMsg.Subject = "HTML CDO example with read receipt"
oMsg.HTMLBody = "<h3>This is a HTML body.</h3><br><br>Kind regards<br>E-mail: <a href='mailto:me@email.com'>me@email.com</a><br>"
'oMsg.TextBody = "This is a plain text body" + VbCrLf + VbCrLf + "Kind regards" + VbCrLf + "E-mail: me@email.com" + VbCrLf
oMsg.MDNRequested = true

' Add the address that the notification is to be sent to
oMsg.fields("urn:schemas:mailheader:disposition-notification-to") = "me@email.com"
oMsg.fields.update

oMsg.Send



 
This page was created with RTF to HTML converter
 
Rate this page

Products

 

News

QS Disclaimer 4.4.15 is now available. This version adds the option to copy images when using the remote manager and a web interface. Also adds Exchange 2016 support.

RTF to HTML converter 1.2.11 is now available. This version fixes a colour table issue.

ID3 Editor 1.23.34 is now available. This version adds multiple defaults and fixes issues with El Capitan and Sierra.

MSAD Login 3.1.0 is now available. This version includes TLS and Kerberos support.

Archive SE 1.1.5 is now available. This upgrade adds support for Tar and BZip2.

back to top  


Facebook  Twitter  Google+ 


Copyright 1990-2017 Pa-software. All Rights Reserved.
Developer of Exchange Server e-mail disclaimers, compression, ISO CD/DVD disc image management, MP3 ID3 editing and Macintosh Active Directory integration.