Send Mail from script, no smtp client required

This script will send an e-mail with no smtp client installed. Also no IIS is necessary.

You can use multiple authentication methods to send mail. Also there is the option to add attachements, use HTML body or TextBody in the e-mail.

On Error Resume Next
Const NAMESPACE = “http://schemas.microsoft.com/cdo/configuration/”
Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoAnonymous = 0  ‘ Use anonymous
Const cdoBasic = 1  ‘ Use basic (clear-text) authentication.
Const cdoNTLM = 2  ‘ Use NTLM authentication
Set Msg = CreateObject(“CDO.Message”)
Set Conf = CreateObject(“CDO.Configuration”)
‘ ============================
‘ Send Configuration
‘ —————————-
strSMTPServer = “smtpserver”
iSMTPPort = 25
iConnectionTimeout = 10
bAuthenticate = False  ‘Set true for authentication
strUsername = “username”
strPassword = “password”
‘ ============================
‘ Mail Configuration
‘ —————————-
strFrom = “email-address”
strTo = “”"Some Body”"  ;”"Another Person”" “
strCC = “”
strBCC = “”
strSubject = “Hello”
strTextBody = “Just wanted to say hi” & vbCrlf
strHTMLBody = “Hello”
strAttachement = “C:\test.txt”
bUseHTMLBody = False
‘ —————————-
With Conf
 .Fields(NAMESPACE & “sendusing”) = cdoSendUsingPort
 .Fields(NAMESPACE & “smtpserver”) = strSMTPServer
 .Fields(NAMESPACE & “smtpserverport”) = iSMTPPort
 .Fields(NAMESPACE & “smtpconnectiontimeout”) = iConnectionTimeout
 if bAuthentication = True then
  .Fields(NAMESPACE & “sendusername”) = strUsername
  .Fields(NAMESPACE & “sendpassword”) = strPassword
  .Fields(NAMESPACE & “smtpauthenticate”) = cdoBasic
 end if
  .Fields.Update
End With
With Msg
 .Configuration = Conf
 .From = strFrom
 .To = strTo
 .CC = strCC
 .BCC = strBCC
 .Subject = strSubject
 if bUseHTMLBody = false then
  .TextBody = strTextBody
 else
  .HTMLBody = strHTMLBody
 end if
 .MIMEFormatted = False
 .AddAttachment strAttachement
 wscript.echo .GetStream.ReadText
 .Send()
 if err.number <> 0 then
  wscript.echo err.number & “: ” & err.description
 end if
End With

Comments

Post a comment