Lets say you want to check a email account every couple of minutes and this was react such as run a couple of commands and email you back?
For example I have already setup
- Status checks of all lights
- Turn on or off the whole house or individual lights
- Randomize the house lights which will turn on my media player and play music remotely as well.
- Open / close the garage,
- Email me when movement is detected after I’ve turned the house off
- The sky’s the limit.
What im currently using is outlook checking a pop3 account every 5 min, since Mapi interfaces really good with VB I use this. This way outlook itself checks every 5 minutes and you code just checks the email every 3. Again my code is Vb2005 / Windows
First setup outlook as your normally would, make sure it checks your email every 2-3 minutes. Then in your code, you will need to make sure you first reference the “Microsoft Office object 12 or 11 “ library into your code. Depends your version it may have object version 11 I’ve used this with Outlook 2000,2003,2007. I suspect by a macro you wont need to have outlook running but in my case its no biggie.
In your imports section include the following
Friend Class MainMenu In your class definion add the following, i've commented each line
‘——– SET UP EMAIL goodies
Friend Class MainMenu Public inboxitem As Microsoft.Office.Interop.Outlook.MailItem '--- we will use this object to send emails out Public objOutlook As Microsoft.Office.Interop.Outlook._Application ' --- Reference the outlook object, and load it if not already Public objNS As Microsoft.Office.Interop.Outlook._NameSpace '--------- The Outlook namespace, used throught the code. Public objInBox As Microsoft.Office.Interop.Outlook.MAPIFolder'------ The folder object that we will assign the inbox to. Public objItems As Microsoft.Office.Interop.Outlook.Items'-------- Each of the items in the inbox, we will loop thru this to read the individual emails.
Then once the program starts we need to set the session that will hold the connection to outlook, this is done in the Load event
Public Sub MainMenu_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load objOutlook = New Microsoft.Office.Interop.Outlook.Application() '--- Assign the variable objOutlook to the application objNS = objOutlook.Session '--------- Tie to the active session, if its not running it will load automatically .. .. Then you can set a timer to check your email every certain amount of time executing the function CheckMail()
In the function that will do the actual reading, we will start with the assigning of variables and then check the emails and search for special text inside the body of the emails.
Public function Checkemail() as Boolean objInBox = objNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox) ' --- Assign ObjInBox to map to the default inbox. objItems = objInBox.Items.Restrict("[UnRead]=True") '----- assign individual items BUT filter only the UNREAD ones! Dim outmail As Microsoft.Office.Interop.Outlook.MailItem '---- This will hold the outgoing mail object Dim iCount As Integer = objItems.Count '------ Will hold the amount of NEW email, will return 0 if all email has already been read. For i = 1 To iCount inboxitem = objItems.Item(i) '----------- ASSIGN A EMAIL ITEM TO THE INBOXITEM, from there we can query stuff like date,subject sender etc. If InStr(LCase(inboxitem.Body), "ouse on") > 0 Then ' --- check for certain words in the Body, in this case 'house on' this also works for VB6 Occupy() ' ---- Run another special function or code somewhere else '------ Here we create our reply email. ------------------ outmail = objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem) outmail.To = inboxitem.SenderEmailAddress '-------- Get the sender email for the inboxitem object. outmail.Subject = "* Reply" outmail.Body = "NICE TO HEAR FROM YOU, HOUSE ON. WAITING YOUR ARRIVAL." outmail.Send() End If inboxitem.UnRead = False '- Mark email as read so its not checked twice next time next i Checkemail=True '----- Just to return something End Function
And thats it!. If you like you can add attachments, HTML text and others, I would checkout Microsofts site for more details on what you can do with the outlook client!.