Sending File Attachments
To add attachments to an outgoing message:
Prepare a variable of type
MAPIMessageto represent the message (as already described in this chapter).Set the
FileCountmember of theMAPIMessagevariable equal to the desired number of attachments.Dimension an array of
MapiFileelements with as many elements as there are files to attach.Set the member values of each array element.
Pass the array to the
MAPISendMailfunction.
Example 3-3 shows a code fragment that sends a
single file attachment to a single recipient. This code assumes that
a session has already been established via a call to the
MAPILogon function, and that the session handle
is held in a variable called nMAPISession.
The message thus sent, when viewed in Microsoft Outlook 98, is shown
in Figure 3-3.
Example 3-3. Sending a File Attachment
Dim nRetVal As Long Dim MyMessage As MAPIMessage Dim MyRecips( ) As MapiRecip Dim MyFiles( ) As MapiFile ' Set the subject and body text. MyMessage.Subject = "Test message from Simple MAPI, with attachment." MyMessage.NoteText = "This is the body text of the message." ' Add a recipient. MyMessage.RecipCount = 1 ReDim MyRecips(1 To MyMessage.RecipCount) As MapiRecip nRetVal = MAPIResolveName(nMAPISession, 0, "Annemarie", 0, 0, MyRecips(1)) ' Add an attachment. MyMessage.FileCount = 1 ReDim MyFiles(1 To MyMessage.FileCount) As MapiFile MyFiles(1).PathName = "c:\autoexec.bat" ' Send the message. nRetVal = MAPISendMail(nMAPISession, 0, MyMessage, MyRecips, MyFiles, ...