How to Send a Mail Message Using Visual Basic MAPI Controls

ID: Q113033


The information in this article applies to:


SUMMARY

This article demonstrates how to create a Microsoft Mail message programmatically and send it by using the Visual Basic MAPI controls. You can use this technique to automate the process of sending messages.


MORE INFORMATION

The professional editions of Visual Basic versions 2.0 and 3.0 come with the custom control MSMAPI.VBX, which contains two controls (MAPI session control and MAPI message control) for creating Microsoft Mail enabled applications. The MAPI session control is used to manipulate a Microsoft Mail session, and the MAPI message control is used to create and manipulate mail messages. It is possible to use these two controls to automate the process of sending mail messages.

The following example illustrates the use of the MAPI controls to send messages. The example creates a mail message with an attachment and sends it to a recipient.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add MSMAPI.VBX to the project.


  3. Add a MAPI session control (MAPISession1) and a MAPI message control (MAPIMessages1) to the form.


  4. Add a command button (Command1) to the form.


  5. Put the following code in the command button click event.
    
       Sub Command1_Click ()
          'MAPI constants from CONSTANT.TXT file:
          Const SESSION_SIGNON = 1
          Const MESSAGE_COMPOSE = 6
          Const ATTACHTYPE_DATA = 0
          Const RECIPTYPE_TO = 1
          Const RECIPTYPE_CC = 2
          Const MESSAGE_RESOLVENAME = 13
          Const MESSAGE_SEND = 3
          Const SESSION_SIGNOFF = 2
    
          'Open up a MAPI session:
          MapiSession1.Action = SESSION_SIGNON
          'Point the MAPI messages control to the open MAPI session:
          MapiMessages1.SessionID = form1.MapiSession1.SessionID
    
          MapiMessages1.Action = MESSAGE_COMPOSE   'Start a new message
    
          'Set the subject of the message:
          MapiMessages1.MsgSubject = "This is the subject."
          'Set the message content:
          MapiMessages1.MsgNoteText = "This is the mail message."
    
          'The following four lines of code add an attachment to the message,
          'and set the character position within the MsgNoteText where the
          'attachment icon will appear. A value of 0 means the attachment will
          'replace the first character in the MsgNoteText. You must have at
          'least one character in the MsgNoteText to be able to attach a file.
          MapiMessages1.AttachmentPosition = 0
          'Set the type of attachment:
          MapiMessages1.AttachmentType = ATTACHTYPE_DATA
          'Set the icon title of attachment:
          MapiMessages1.AttachmentName = "System Configuration File"
          'Set the path and file name of the attachment:
          MapiMessages1.AttachmentPathName = "C:\CONFIG.SYS"
    
          'Set the recipients
          MapiMessages1.RecipIndex = 0                    'First recipient
          MapiMessages1.RecipType = RECIPTYPE_TO          'Recipient in TO line
          MapiMessages1.RecipDisplayName = "EddieSpaghetti"   'e-mail name
          MapiMessages1.RecipIndex = 1                  'add a second recipient
          MapiMessages1.RecipType = RECIPTYPE_TO        'Recipient in TO line
          MapiMessages1.RecipDisplayName = "TanyaLasagna"     'e-mail name
          MapiMessages1.RecipIndex = 2                   'Add a third recipient
          MapiMessages1.RecipType = RECIPTYPE_CC         'Recipient in CC line
          MapiMessages1.RecipDisplayName = "BlairAngelHair"  'e-mail name
          MapiMessages1.RecipIndex = 3                  'Add a fourth recipient
          MapiMessages1.RecipType = RECIPTYPE_CC          'Recipient on CC Line
          MapiMessages1.RecipDisplayName = "JoanieCannelloni" 'e-mail name"
    
          '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
     


  6. Save the project.


  7. Run the code, and click the command button.


The program should start a MAPI session, create a message, send the message, and then close the session.

Additional query words: 2.00 3.00


Keywords          : 
Version           : WINDOWS:2.0,3.0
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: May 13, 1999