Article ID: 138534
Article Last Modified on 11/3/2003
113033 VB3: How to Send a Mail Message Using Visual Basic MAPI Controls
Form1.MapiMessages1.AttachmentPathName = "C:\WINNT35\ARCADE.BMP"
Form1.MapiMessages1.AttachmentName = "ARCADE.BMP"
Form1.MapiMessages1.AttachmentType = 2 ' ATTACHTYPE_SOLE an SOLE is a
' static OLE object attachment
This code sends the attachment with the rest of the message, but the
attachment shows up within the message body as an icon. In order to view
the actual data, you must double-click the icon and start the necessary
application, which may not be what you need.
Control Name ------------------------------------ OLE Control OLE1 MAPI Session Control MapiSession1 MAPI Messages Control MapiMessages1 One Command Button saveOLE One Command Button sendMessage
Dim vstAttachedOLEFile As String 'MAPI constants from CONSTANT.TXT file: Const ATTACHTYPE_DATA = 0 Const ATTACHTYPE_EOLE = 1 Const MESSAGE_COMPOSE = 6 Const MESSAGE_RESOLVENAME = 13 Const MESSAGE_SEND = 3 Const OLE_SAVE_TO_OLE1FILE = 18 Const RECIPTYPE_TO = 1 Const SESSION_SIGNON = 1 Const SESSION_SIGNOFF = 2
Sub saveOLE_Click ()
Dim iFileNum As Integer
' Get a free file handle
iFileNum = FreeFile
' So it won't raise an error if the file doesn't exist, disable error
' checking.
On Error Resume Next
vstAttachedOLEFile = "c:\temp\test.bmp"
Kill vstAttachedOLEFile
On Error GoTo 0
' Set the SourceDoc property to the file you want to attach to the
' message.
OLE1.SourceDoc = "C:\winnt35\arcade.bmp"
' Embed the file in the OLE control
OLE1.Action = 0
DoEvents
' Open the file that will contain the OLE 1.0 compliant object
' information.
Open vstAttachedOLEFile For Binary As #iFileNum
OLE1.FileNumber = iFileNum
' Save the object as an OLE 1.0 compliant object.
OLE1.Action = OLE_SAVE_TO_OLE1FILE
Close iFileNum
End Sub
Sub sendMessage_Click ()
' Open up a MAPI session:
MapiSession1.Action = SESSION_SIGNON
' Point the MAPI messages control to the open MAPI session:
MapiMessages1.SessionID = form1.MapiSession1.SessionID
' Start a new message
MapiMessages1.Action = MESSAGE_COMPOSE
' Set the subject of the message:
MapiMessages1.MsgSubject = "This is the subject."
' Set the message content:
MapiMessages1.MsgNoteText = " This is the mail message."
form1.MapiMessages1.AttachmentIndex = _
form1.MapiMessages1.AttachmentCount
form1.MapiMessages1.AttachmentPathName = vstAttachedOLEFile
form1.MapiMessages1.AttachmentName = vstAttachedOLEFile
form1.MapiMessages1.AttachmentType = ATTACHTYPE_EOLE
form1.MapiMessages1.AttachmentPosition = 1
' Set the recipient type. RECIPTYPE_TO sends message to recipient:
MapiMessages1.RecipType = RECIPTYPE_TO
' Set the recipient's E-Mail name.
' You can have multiple recipients separated by semicolons
' Change to a valid e-mail name
MapiMessages1.RecipDisplayName = "joecool"
' MESSAGE_RESOLVENAME checks to ensure the recipient is valid and
' puts the recipient address in MapiMessages1.RecipAddress
' If the E-Mail name is not valid, a trappable error will occur.
MapiMessages1.Action = MESSAGE_RESOLVENAME
' Send the message:
MapiMessages1.Action = MESSAGE_SEND
' Close MAPI mail session:
MapiSession1.Action = SESSION_SIGNOFF
End Sub
Additional query words: 3.00 send
Keywords: KB138534