You are on page 1of 5

REM Internal and External Mailflow - End to End Monitoring script

REM Inputs to be provided


REM Intra = Comma seperated within Quotes - Provide the Primary SMTP Addresses of
the mailboxes to test messages between Sites
REM Extra = Comma seperated within Quotes - same as Intra
REM IntGmailID = THe Email ID Gmail will autoforward the test mail to.
REM GmailID = Gmail ID which is configured to autoforward the mail to IntGmailID
and delete the message
REM smtp = Specify a Mailbox sever name or the BH server name, it is used to send
mails both internal and external.
REM aMbxserver = Specify a mailbox server name to create outlook connection
profile.
'-------------------------------
Configuration---------------------------------------------------------------------
----------------------------------------------------------------------------------
----'

Intra = "Site1@org.com,Site2@org.com"
Extra = "Site1@org.com,Site2@org.com"
IntGmailID = "Site1@org.com"
GmailID = "test@gmail.com"
smtp = "BH1"
aMbxserver = "MBS1"

'-------------------------------Do not Edit


Below-----------------------------------------------------------------------------
------------------------------------
tm = now
Subject = "test email from script at " & tm

'for intra check


Wscript.echo "-----Starting Intra mailflow test------------------------"
Wscript.echo ""
eArr = split(Intra,",")
Mode = "Internal"

If uBound(eArr) < 1 then Wscript.echo "Input atleast 2 Email address for internal
mailflow test"

for i = 0 to ubound(eArr)-1
for j = i+1 to ubound(eArr)
aFrom=trim(eArr(i))
aTo = trim(eArr(j))
Sendmail = SendMailByCDO(Subject, smtp, aFrom, aTo, mode)
Wscript.echo Sendmail & VbCrlf
Wscript.echo "Sleeping for 30 Seconds" & VbCrlf
Wscript.sleep(30000)

aMbx = GetAlias(aFrom)
dMbx = GetAlias(aTo)

Checkmsg = chkmsg(Subject,aMbxserver,aMbx,Mode)
Wscript.echo Checkmsg & VbCrlf
If Mode = "Internal" then
Mode = "External"
Delmsg = chkmsg(Subject,aMbxserver,dMbx,Mode)

Else
End If

next
next

Wscript.echo "------------Internal Done --------------------------" & VbCrlf


'For extra check

eArr = split(Extra,",")
Mode = "External"

Wscript.echo "-----Starting External mailflow test------------------------" &


VbCrlf
Wscript.echo ""

for i = 0 to ubound(eArr)

aFrom=trim(eArr(i))
aTo = GmailID
Sendmail = SendMailByCDO(Subject, smtp, aFrom, aTo, mode)
Wscript.echo Sendmail
Wscript.sleep(120000) & VbCrlf
Wscript.echo "Sleeping for 120 Seconds" & VbCrlf

aMbx = GetAlias(IntGmailID)
aMbxserver = "usoldtms006"
'Wscript.echo aTo
'Wscript.echo dMbx
Checkmsg = chkmsg(Subject,aMbxserver,aMbx,Mode)
Wscript.echo Checkmsg & VbCrlf
'If Mode = "Internal" then
'Mode = "External"
'Delmsg = chkmsg(Subject,aMbxserver,dMbx,Mode)

'Else
'End If

next

Wscript.echo "external tests Done"

Private Function SendMailByCDO(Subject, smtp, aFrom, aTo, mode)


Const cdoSendUsingPickup = 1
Const cdoSendUsingPort = 2
Const cdoSendUsingExchange = 3

Const cdoAnonymous = 0
Const cdoBasic = 1
Const cdoNTLM = 2

on error resume Next

Dim Message 'As New CDO.Message '(New - For VBA)


'Create CDO message object
Set Message = CreateObject("CDO.Message")

'Set configuration fields.


With Message.Configuration.Fields
'Original sender email address
.Item("http://schemas.microsoft.com/cdo/configuration/sendemailaddress")
= aFrom

'SMTP settings - without authentication, using standard port 25 on host


smtp
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") =
cdoSendUsingPort
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
25
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
smtp

'SMTP Authentication
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")
= 2
' .Item("http://schemas.microsoft.com/cdo/configuration/sendusername")
=""
' .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") =
""

.Update
End With

'Set other message fields.


With Message
'From, To, Subject And Body are required.
.From = aFrom
.To = aTo
.Subject = Subject

'Set TextBody property If you want To send the email As plain text
.TextBody = TextBody

'Set HTMLBody property If you want To send the email As an HTML


formatted
'.HTMLBody = TextBody

'Blind copy And attachments are optional.


If Mode = "Internal" then
'request the delivery status
.DSNOptions = 14
End If

'Send the email


.Send
End With

'Returns zero If succesfull. Error code otherwise


If Err.Number <> 0 Then
SendMailByCDO = "Error " & Err.Number & " - " & Err.description & "
Sending FROM " & aFrom & " TO " & aTo
ElseIf Err.Number = 0 Then
SendMailByCDO = "Sending FROM " & aFrom & " TO " & aTo
Else
SendMailByCDO = "Warning -mail Sent FROM " & aFrom & " TO " & aTo & " did
not reported the delivery"
End If
End Function

Private Function chkmsg(Subj,Server,mailbox,Mode)

on error resume Next

Dim objSession
Set objSession = CreateObject("MAPI.Session")
strProfileInfo = Server & vbLf & Mailbox
objSession.Logon "", "", False, True, 0, False, strProfileInfo

Set objFolder = objSession.inbox.messages

If err.number <> 0 Then


Chkmsg = "Error opening mailbox " & Mailbox & "-" & Err.description
End If

If Mode = "Internal" Then


Subj = "Delivered: " & Subj
End If

for each objmessage in objfolder


'Wscript.echo objmessage.subject
If objmessage.subject = Subj then
Chkmsg = "Recieved Msg Sent FROM: " & objmessage.Sender & " TO: " &
objmessage.recipients.item(1)
objmessage.delete
Delt = True
Subj = Replace(Subj,"Delivered: ","")
end If
next
If Delt <> True Then
ChkMsg = "Error recieveing message - " & Subj
End If
Subject
objSession.Logoff

End Function

Function GetAlias(Emailadd)

Dim oGC 'As IADsContainer


Dim oEntrprise 'As IADs

' Get the enterprise object from the GC namespace.


Set oGC = GetObject("GC:")
For Each child In oGC
Set oEntrprise = child
'Exit For
Next
'Setup ADODB connection

Set objConnection = CreateObject("ADODB.Connection")


objConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

'Execute search command to look for Organization


objCommand.CommandText = _
"<" & oEntrprise.ADsPath & ">" & ";(&(mail=" & Emailadd &
"));mailNickName" & _
";subtree"
'Execute search to get Recordset
Set objRecordSet = objCommand.Execute

If objRecordSet.RecordCount = 0 Then
GetAlias = "Emailadd"
Else
While Not objRecordSet.EOF
GetAlias = objRecordSet.Fields("mailNickName")
objRecordSet.MoveNext
Wend
End If

End Function

You might also like