Script to alert if no email has arrived...

Jamie786
Jamie786 used Ask the Experts™
on
Hi there,

Have a bit of a left field request, I need to monitor a mailbox and if no emails has arrived within say 1 hour (that is no email at all), then an alert is sent to another email address?

Is this going to be easy to do?  The mailbox's are on an exchange 2010 server if that helps :)...

Thanks
James
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
For now, I can only think of some VBA code that monitors to main mailbox every now and then.
That needs a PC to be constantly on and has Outlook installed. Is that a workable solution for you?
For now, I don't see how to do this server side on the Exchange server (similar to a server side mailbox rule, which works without the need of a client to be running)

Author

Commented:
this can be run on a server of any kind really so yes its an option...
Commented:
This is a vbscript (save as scriptname.vbs) and can be run as a scheduled job:

Dim myolApp 
Dim myNamespace
Set myolApp = CreateObject("Outlook.Application")
Set myNamespace = myolApp.GetNamespace("MAPI")
Set folder = myNamespace.Pickfolder()
Set folderitems = folder.Items

checkdate  = DateAdd("h", -1, now())

count = 0

for each item in folderitems
	 dateStr = item.SentOn
	 if dateStr >  checkdate then
		count = 1
	 end if
next

if count = 1 then
	'newer found in past hour
else
	'no newer mails found
	SendMail "towho", "email@address.com"
end if

Sub SendMail(TheName, TheAddress) 
Dim objMessage, Rcpt 

Rcpt = Chr(34) & TheName & Chr(34) & "<" & TheAddress & ">" 
Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "No new emails in the past hour received" 
objMessage.From = """email alert"" <alert@mail.com>" 
objMessage.To = Rcpt 
objMessage.HTMLBody = "No new emails in the past hour received" 

objMessage.AddAttachment strFilePath

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") = "smtp.server.com"

'Server port (typically 25)
objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

objMessage.Configuration.Fields.Update

objMessage.Send 

End Sub

Open in new window


Obviously, some strings need to be changed to fit what you want, email address and smtp server also.
Depends on Outlook being installed, the correct profile loading. Depends also on CDO the be working on the server.
Set folder = myNamespace.Pickfolder() also needs to be fixed to the correct folder in the form of
Set folder = myNamespace.Folders("mailboxname").Folders("inbox")
Works best in Exchange/Outlook environment, but also POP3 if auto retrieve has been set (just don't run the script only once per hour, since it might needs to retrieve messages that are only "seen" during the next run)
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Should the set folders be set like this :-

Set folder = myNamespace.Pickfolder("voicemailbox").Folders("inbox") ("voicemailfolder")
Set folderitems = folder.Items

So inside a mailbox called voicemailbox and inside a folder that is nested below the inbox is a folder called voicemailfolder.  Or have I totally got the syntax wrong :)

Commented:
The syntax is:

Set folder = myNamespace.Folders("voicemailbox").Folders("inbox").Folders("voicemailfolder")

Open in new window


Please note, the first folder isn't really a folder, it's the name of the most top level entry (in folder view, minimize as many top folders as you can, you will end up with this name, this is the only top row that can't be minimized)

Author

Commented:
perfect that seems to work fine.. many thanks for that!

Author

Commented:
perfect!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial