Sending File Attachments

To add attachments to an outgoing message:

  1. Prepare a variable of type MAPIMessage to represent the message (as already described in this chapter).

  2. Set the FileCount member of the MAPIMessage variable equal to the desired number of attachments.

  3. Dimension an array of MapiFile elements with as many elements as there are files to attach.

  4. Set the member values of each array element.

  5. Pass the array to the MAPISendMail function.

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, ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.