Gs 9711
Gs 9711
G E T T IN G S T A R T E D
sers have come to expect all Windows applications to feature the ability to
Encapsulate e-mail send e-mail. If your application creates a data file, you definitely want to
provide a menu item that attaches that data file to an e-mail message and
functionality into sends the message.
Only a few years ago, it was difficult to mail-enable your application. You needed to
install an e-mail client, such as MSMail, as well as several Messaging Application
ActiveX DLLs for Programming Interface (MAPI) DLLs to link to a MAPI service provider, such as a
Microsoft Mail (MSMail) post office or Exchange. Today, e-mail is ubiquitous and most
computers are set up for e-mail. The interfaces have been simplified, and your
easy reuse later. Windows Messaging or Outlook client can communicate with your organization’s
Session
Click & Retrieve
Source
AddressEntries
Collection Folder
Message
Attachments Collection
Attachment
Recipients Collection
Recipient
Exchange Server through MAPI service Active Messaging gives you easy ac- SendWithAM
providers such as CompuServe, Microsoft cess to sending and receiving e-mail End Sub
Network, and America Online, or through from within a Visual Basic application.
any Simple Mail Transfer Protocol (SMTP)/ It also gives you full access to the fold- Calling a procedure from your button
Post Office Protocol 3 (POP3) mail server. ers within your information store. To Click events rather than writing the code
In previous articles, I suggested you send e-mail from an application with inline gives you more flexibility to call the
use the MAPI controls that come with Vi- Active Messaging, start a new Visual procedure from a different place in your
sual Basic, or direct calls to the MAPI DLL, Basic 5 project and design a simple form application.
to add e-mail functionality to your apps [“A to send e-mail. Make sure you create The object model for Active Messag-
New Outlook on MAPI,” VBPJ November text-box controls for the recipient, sub- ing is simple. The Session object is the
1996]. Now, with the release of Outlook ject, message, and attachment, as well starting point for all use of the Active
8.01 and Exchange 5.0, you should use the as a CommandButton control labeled Messaging components. Begin an Active
new Active Messaging 1.1 components re- Send. Select the References menu item Messaging session by creating a
leased by Microsoft because Active Mes- on the Project menu, and add a refer- MAPI.Session object. All other objects are
saging is easier to use, more flexible, and ence to the Microsoft Active Messaging derived from the Session object (see Fig-
more powerful. You can download these 1.1 Object Library. Double-click on the ure 1 for a simplified version of the object
components from Microsoft’s Web site at Send button and add this code to call model from the Active Messaging help
ftp://ftp.microsoft.com/services/technet/ the new procedure SendWithAM: file). For example, you can create an out-
samples/boes/bo/mailexch/exchange/ going message by adding a Message ob-
appfarm/actmsg.exe. Private Sub butSend_Click() ject to the Messages collection in the
mLoggedOn = True
Exit Function
End If
Option Explicit 'logon failed so try to get default profile
Public Profile As String Profile = GetDefaultProfile
Private mLoggedOn As Boolean If Len(Profile) Then oSession.Logon ProfileName:=Profile
If Err = 0 Then
Public Property Get LoggedOn() As Boolean mLoggedOn = True
LoggedOn = mLoggedOn Exit Function
End Property End If
'finally try a normal logon
Private Sub Class_Initialize() oSession.Logon
On Error Resume Next If Err = 0 Then
Set oSession = CreateObject("MAPI.Session") mLoggedOn = True
If oSession Is Nothing Then Exit Function
Err.Raise vbObjectError + Err, "SOTMapi", _ End If
"Create failed"
Exit Sub Err.Raise vbObjectError + Err, "SOTMapi", "Logon failed"
End If Logon = Err
End Sub End Function
MAPIClass Class Module. This class module contains the properties and methods for the new class. Design the Logon
LISTING 1method to perform a “quiet” logon—first, by using a given profile name from the class’s Profile property, then by trying to log
on to an existing session, and finally by finding the default profile from the registry.
oMessage.Send
oSession.Logoff
End Sub
This should be a read-only property of the for the text-box controls (see Listing 1).
class, so you need a private variable to You should design the Logon method
hold the value and a Property Get proce- to perform a “quiet” logon—first, by using
dure to return the value. You can use the a given profile name from the class’s Pro-
Profile property to specify the profile to file property, then by trying to log on to an
use when logging on. Because the applica- existing session, and finally by finding the
tion using the class sets the Profile prop- default profile from the registry. This last
erty, the property can be a simple public step can be a bit complicated. Article
string variable. The Logon method calls Q171422 in the Microsoft Knowledge Base
the Session object’s Logon method, but (www.microsoft.com/kb) provides some
has the capability to find the default profile good sample code that I included in the
if one is not specified. The SendQuick GetDefaultProfile procedure in the class’s
method accepts the message’s recipient, module. Download Listing 2 from the free,
subject, text, and attachment as arguments, Registered Level of The Development Ex-
and provides a single simple call to send a change (see the Code Online box for de-
message with no more than one attach- tails). The class should display a dialog
ment to a single recipient. box only if these quiet attempts fail (see
You might find that you want to ex- Listing 1).
pand this class to allow for multiple re- Now build your ActiveX DLL, and
cipients and multiple attachments, but change your test application to call your
let’s start with this simple class for now. new class by adding the SendMailClass
Dimension the public Profile property and procedure to the form and changing the
the private mLoggedOn variable in the code in the Send button’s Click event to
class module. Write the Property Get pro- call the new procedure:
cedure to return the value of this private
variable: Private Sub SendWithClass()
Dim MyMapi As New MAPIClass
Option Explicit On Error Resume Next
Public Profile As String If MyMapi.SendQuick(txtTo, txtSubject, _
Private mLoggedOn As Boolean txtNoteText, txtAttach) = 0 Then
If Err Then
Public Property Get LoggedOn() As _ MsgBox "Send Failed " & Err
Boolean Else
LoggedOn = mLoggedOn MsgBox "Message Sent"
End Property End If
Else
Create the MAPI Session object in the MsgBox "Send Failed"
class’s Initialize event, and log off the End If
session in the Terminate event: End Sub