Tuesday, 5 May 2009

VBScript: Sending e-mail

Sending e-mail automaticly at various test contexts could be a useful feature (beware of "spam" if you are sending mail to a shared address...) so as a follow-up to this post: http://abouttesting.blogspot.com/2008/01/cdomessage.html I thought I would share an example on how to send an HTML-based e-mail using CDO:

'Sending E-mail sample

strEmailSubject = "Testing CDO"
strMessageFrom = "myemailfrom@mydomain.com"
strMessageTo = "myemailto@mydomain.com"
strHTMLBody = "<B>Bold</B>&nbsp;example"
strSMTPServer = "mail.mydomain.com"

SendEmail strEmailSubject, strMessageFrom, strMessageTo, strHTMLBody, strSMTPServer

Function SendEmail (strEmailSubject, strMessageFrom, strMessageTo, strHTMLBody, strSMTPServer)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = strEmailSubject
objMessage.From = strMessageFrom
objMessage.To = strMessageTo
'The line below shows how to send using HTML included directly in your script
objMessage.HTMLBody = strHTMLBody
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM
'==This section provides the configuration information for the remote SMTP server.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoAnonymous
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False
'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
'==End remote SMTP server configuration section==
End Function

You can download the script here:


QualityPoint Technologies said...

I am wondering how you are using different colors in sample code.

Have you done it manually? Or is there any way (e.g any plug-in) to do it automatically in the blogger?

I am also using the blogspot only. But I didn't find a way to copy and paste the code without losing color.


Stefan said...


Check out http://abouttesting.blogspot.com/2008/02/code-formatat-last.html and http://abouttesting.blogspot.com/2008/08/java-display-java-code-in-html.html

QualityPoint Technologies said...

Thanks for the information

Stefan said...

You're welcome! :-)