VBScript code to connect to a enterprise microsoft exchange mail server, authenticate and send mail with attachments.


Dim iMsg, iCong, Flds, lastDate, mailBodyStr
Dim iResults, attachFile

attachFile= "C:\sample.xls"
lastDate=DateAdd("d", -1, Date())
dateStr=FormatDateTime(lastDate, 1)


Set iMsg=CreateObject("CDO.Message")
Set iConf=CreateObject("CDO.Configuration")

Set Flds=iConf.Fields

schema="http://schemas.microsoft.com/cdo/configuration/"

Flds.Item(schema & "sendusing") = 2
Flds.Item(schema & "smtpserver") = "smtp.gmail.com"
Flds.Item(schema & "smtpserverport") = 465
Flds.Item(schema & "smtpauthenticate") = 1
Flds.Item(schema & "sendusername") = "google_account_username@gmail.com"
Flds.Item(schema & "sendpassword") = "google_account_password"
Flds.Item(schema & "smtpusessl") = 1 ' it depends if mail server uses ssl communication a company intranet using exchange server may not use this, so then omit this
Flds.Update

Set iResults=CreateObject("ADODB.Recordset")
Set iResults=DTSGlobalVariables("gResults").Value

mailBodyStr="Dear Recipients

This is an auto generated test mail sent from Dept./SERVER with a sample attachment.

This mail demonstrates the functionality of automatic scheduled log in to a smtp server and send mail with attachments.

"

mailBodyStr = mailBodyStr & "----====----

"

mailBodyStr = mailBodyStr & "Some data"

mailBodyStr = mailBodyStr & " Generated: " & FormatDateTime(Now(), 0)


With iMsg
.To = ", "
.Cc = ""
.From = "Sender Name "
.Subject = "Auto generated test mail"
.HTMLBody = mailBodyStr
.Sender = "Name" ' Beware that the name doesnt contain spaces i dont know why it generates error
.Organization = "Organization / department"
.ReplyTo = ""
.AddAttachment attachFile
Set .Configuration = iConf
.Send
END With

' Free memory

Set iMsg = nothing
Set iConf = nothing
Set Flds = nothing

Comments