Last Update 2006 / 03 / 05
|
Outlook-AutomationAnother quite common way to integrate email-functionality in an Access-Application is to use automation of MS Outlook via the MS Outlook Objectlibrary. The following codesnippet illustrates how simple this can be done for the purpose of sending an single mail.
Public Sub sendMail()
Dim myMail As Outlook.MailItem
Dim myOutlApp As Outlook.Application
' Creating an Outlook-Instance and a new Mailitem
Set myOutlApp = New Outlook.Application
Set myMail = myOutlApp.CreateItem(olMailItem)
With myMail
' defining the primary recipient
.To = "recipient@somewhere.invalid"
' adding a CC-recipient
.CC = "other.recipient@somewhere.else.invalid"
' defining a subject for the mail
.Subject = "My first mail sent with Outlook-Automation"
' Addimg some body-text to the mail
.Body = "Hello dear friend, " & vbCrLf & vbCrLf & _
"This is my first mail produced and sent via Outlook-Automation." & vbCrLf & vbCrLf & _
"And now I will try add an attachment."
' Adding an attachment from filesystem
.Attachments.Add "c:\path\to\a\file.dat"
' sending the mail
.Send
' You can as well display the generated mail by calling the Display-Method
' of the Mailitem and let the user send it manually later.
End With
' terminating the Outlook-Application instance
myOutlApp.Quit
' Destroy the object variables and free the memory
Set myMail = Nothing
Set myOutlApp = Nothing
End Sub
To make this sample work, you'll have to add a reference to the Outlook-Objectlibrary. This way of emailintegration is very easy to use and offers the developer almost full control over any common property of the mail that is to be created without having to bother about mail-configuration settings on of the target environment of his application. Obviously this approach has a tremendous limitation. It requires MS Outlook to be installed on every Workstation where the mail-functionality is intended to be used. - Before you ask: No, Microsoft Outlook Express will not work! Furthermore, with quite similar code as the sample above, many malicious internet-worms and viruses have spread through the web and wreaked havoc upon many naive and uncautious users' data. To prevent this happening again, MS has reduced the automation-features of MS Outlook with updates and securityfixes to a point somewhere near total uselessness. With these "fixes" installed the users has to confirm any access to his Outlook-Addressbook and any attempt to send email by an automation-process. Once confirmed that process may access these auotmation-features for a short period of time. © 1999 - 2005 by Philipp Stiefel |