Outlook.Application

Using the Outlook.Application object to read e-mails from Visual Foxpro.

Trying to read e-mail using POP3 or IMAP proved to be hell to me. Documentation is not present or hardly legible. Because we use Microsoft® Outlook® at our company, I was able to connect to Outlook using the Outlook.Application object.

This simple code finds all mails in a certain folder (loTelecomMap):

loOutlook = CREATEOBJECT("Outlook.application")
loMAPI = loOutlook.GetNameSpace("MAPI")
loTelecomMap = loMAPI.GetDefaultFolder(6).Folders([Business]).folders([Telecom])
loMails = loTelecomMap.Items
loMessage = loMails.Find("[Subject]='Mail subject title'") && starts with the last (newest) message
DO WHILE NOT ISNULL(loMessage)
 ldDate = TTOD(loMessage.ReceivedTime)
 lcMessage = loMessage.Body
 lcHTMLsource = loMessage.HTMLbody
 * More actions on the loMessage object
 loMessage = loMails.FindNext() && Next message
ENDDO
RELEASE loOutlook

I had great help using a whitepaper by Andrew MacNeill. Linking to his page is prohibited, so you have to copy-and-paste the URL yourself: http://www.aksel.com/whitepapers/OutlookAutomation.htm. To accommodate for possible closing of that reference, I have included the pages contents (the whitepaper) in PDF form on this page.

Leave a Reply

Your email address will not be published. Required fields are marked *