How to Send and Receive Mail Using Visual Basic 4.0
by Dave Grundgeiger
Like other useful computer tools, electronic mail benefits from automation. Would you like to write a utility that monitors a file server’s resources and sends you an email when resources are low? You can do that with Visual Basic and Microsoft's Messaging Application Programming Interface (MAPI). Or would you like to write a help desk call log program that emails a technician when a new call is entered into the database? You can do that too. In this article I'll show you the basics of sending and receiving electronic mail.
The Tools You'll Need
I'm using Visual Basic 4.0 for 32-bit development, running under Windows 95. This version of VB includes the two controls that will be at the center of every mail-enabled application you write: the MAPI session control and the MAPI messages control. The MAPI session control is used to establish and control a Microsoft Mail session, and the MAPI messages control is used to create and work with mail messages. You can also use the 16-bit Visual Basic 4.0 platform.
You'll also need to be running a MAPI-compliant messaging system. I'm using Microsoft Exchange, but Windows for Workgroups, Microsoft Mail or Outlook should work just as well. Make sure that the messaging system is running correctly (i.e. check that you can send and receive emails) before proceeding.
Getting Started
You'll need to add the MAPI controls to your Visual Basic toolbox each time you start a new mail-enabled project. To do so, from the Visual Basic menu bar, select Tools, then Custom Controls. The Custom Controls dialog box appears as shown in Figure 1.

Figure 1: Custom Controls Dialog Box
In the Available Controls list box, scroll down until you find "Microsoft MAPI Controls." Click on the check box if it isn't already checked, and click on OK. After doing so, you should see the two MAPI controls in your toolbox. (If you don't see the toolbox at all, select View, then Toolbox, from the Visual Basic menu to make it appear.) The tool icons are shown in Figure 2.

Figure 2: MAPI Tool Icons
Sending Mail
Let's get our feet wet by creating a form that allows you to enter and send a mail message. Refer to Figure 3 for control placement while you build the form, as follows:
Note: Because MAPI session and MAPI messages controls aren’t visible at runtime, it doesn’t matter where on the form you place them.
Private Sub Form_Load()
MAPISession1.SignOn
End Sub
Private Sub Form_Unload(Cancel As Integer)
MAPISession1.SignOff
End Sub
Private Sub cmdSend_Click()
With MAPIMessages1
.MsgIndex = -1
.RecipDisplayName = txtSendTo.Text
.MsgSubject = txtSubject.Text
.MsgNoteText = txtMessage.Text
.SessionID = MAPISession1.SessionID
.Send
End With
MsgBox "Message sent!", , "Send Message"
End Sub

Figure 3: Send Message Form
Save and run the program, then enter and send a message. If you don't get any runtime errors, then your mail message is on its way! (If you do get an error, you may have entered the recipient's address incorrectly.) That's all it takes to write a mail-enabled application. (Don't you just love Visual Basic?)
Just what have we done? As you can see from the code, the format of your mail-enabled applications will be:
Receiving Mail
Processing received mail is only a little more complex than sending mail. You first must fetch the mail by calling the MAPI messages control’s Fetch method. This loads the control with unread messages from the user’s inbox. After calling the Fetch method, examine the control’s MsgCount property to find out how many messages were fetched, then set the MsgIndex property to tell the control which message you would like to process. Note that the MsgIndex property is 0-based. That is, the first message has an index of 0, the second message has an index of 1, and so on. An example will illustrate these concepts.
Public Sub FetchNewMail()
MAPIMessages1.FetchUnreadOnly = True
MAPIMessages1.Fetch
End Sub
Public Sub DisplayMessage()
lblMsgCount.Caption = "Message " & _
LTrim(Str(MAPIMessages1.MsgIndex + 1)) & " of " & _
LTrim(Str(MAPIMessages1.MsgCount))
lblMsgDateReceived.Caption = MAPIMessages1.MsgDateReceived
txtMsgNoteText.Text = MAPIMessages1.MsgNoteText
lblMsgOrigDisplayName.Caption = MAPIMessages1.MsgOrigDisplayName
lblMsgSubject.Caption = MAPIMessages1.MsgSubject
End Sub
Private Sub Form_Load()
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
FetchNewMail
DisplayMessage
End Sub
Private Sub cmdPrevious_Click()
If MAPIMessages1.MsgIndex > 0 Then
MAPIMessages1.MsgIndex = MAPIMessages1.MsgIndex - 1
DisplayMessage
Else
Beep
End If
End Sub
Private Sub cmdNext_Click()
If MAPIMessages1.MsgIndex < MAPIMessages1.MsgCount - 1 Then
MAPIMessages1.MsgIndex = MAPIMessages1.MsgIndex + 1
DisplayMessage
Else
Beep
End If
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub

Figure 4: The Receive Mail Form
Save and run the project. When the form loads, the code in the Load event fetches the new mail (if any), and displays the first message. If there are multiple messages, you can use the Next button to move through them. The Previous button, of course, moves you back toward the first message.
Fetching All Messages
If you’d like to fetch all messages from the user’s inbox, set the FetchUnreadOnly property of the MAPI messages control to False before executing the Fetch method. The MsgRead property (boolean, read-only) then tells you whether any given message has previously been read. The message is automatically marked as read after the note text or attachment information has been accessed. Accessing the header information does not mark the message as read.
Attachments
Receiving Attachments
MAPI provides a count and an index for attachments, much as it does for messages. When processing received mail, your program checks the AttachmentCount property to find out how many attachments the message has. You then set the AttachmentIndex property to process each attachment in turn. Valid values for AttachmentIndex are 0 through AttachmentCount-1. After you’ve set the AttachmentIndex, you can read its attributes, as follows:
AttachmentName
This property gives you the file name of the attachment, or, if it the attachment is an OLE object, it gives you the class name of the object.
AttachmentPath
This property gives you the full path of the attached file. If you want to open or otherwise manipulate the attached file, use this property to find out where it is.
AttachmentPosition
This property specifies the attachment’s position within the body of the message. A mail program would use this information to place a representation of the attachment at the appropriate position in the displayed message text.
AttachmentType
This property specifies the type of the attachment. It is an integer that can take on one of three values, as represented by the following Visual Basic constants:
Sending Attachments
When sending mail, the above properties are used in much the same way as they are when receiving mail, except that you’re setting them instead of reading them. The notable exception is that the AttachmentIndex property can be set to anything you want. This automatically sets the AttachmentCount property to the appropriate value. (You don’t set the AttachmentCount property directly.)
Summary
In this article we’ve looked at the basics of sending and receiving mail using Microsoft’s Messaging API and Visual Basic 4.0, including processing attachments. Using this information, you can enable your applications to send and receive mail messages without human intervention.
About the Author
Dave Grundgeiger is a Microsoft Certified Solution Developer (MCSD), and is the author of .INI Master—The .INI File Difference Utility for Windows. Dave lives in Madison, Wisconsin, where he is a senior developer at Tara Software, Inc.. Dave can be reached at daveg@tarasoftware.com.
How to Send and Receive Mail Using Visual Basic 4.0
© 1997 Dave Grundgeiger. All rights reserved.
Home
Paperback
- 384 pages
(October 2000)
O'Reilly & Associates;
ISBN: 156592665X