Sending Email

To send email, we'll add a new item to the File menu: Send email…. There is already a Send… item in the File menu for sending files:

We change that to Send email… in the Menu Editor:

Now let's add the ability to send email to our program. Click the Send email… item to open its click event:

Private Sub mnuFileSend_Click()
End Sub

We start by creating a MAPI session, as we did when we checked email:

Private Sub mnuFileSend_Click()
—> MAPISession1.SignOn
—> If Err <> 0 Then
—>     MsgBox "Logon Failure: " + Error$
—> End If
     .
     .
     .
End Sub

Now we want to compose a new email message and send it. To do that, we will use the MAPIMessage1 control; the MAPISession1 control is used to establish an email session and to automatically download new email, and the MAPIMessages1 control is used to work with individual messages, including examining the text in incoming messages and composing new messages. To connect the MAPIMessages control to our new MAPI session, we load the MAPI session ID into the MAPIMessages SessionID property. We get the session ID from the MAPISession control's SessionID property:

Private Sub mnuFileSend_Click()
    MAPISession1.SignOn
    If Err <> 0 Then

MsgBox "Logon Failure: " + Error$

End If
—> MAPIMessages1.SessionID = MAPISession1.SessionID
        .
        .
        .
End Sub

Now the MAPIMessages1 control is connected to our MAPI session.

The MAPIMessages control is an indexed control; its methods appear in Table 4.1, and its important properties appear in Table 4.2.

Table 4.1 MAPIMessages Control Email Operations

Do This Method Action Method Constant (Obsolete)
Get email from Inbox Fetch MESSAGE_FETCH
Send email with Send MESSAGE_SENDDLG
 Compose box
Send email Send MESSAGE_SEND
Save a message Save MESSAGE_SAVEMSG
Copy message for reply Copy MESSAGE_COPY
Compose email Compose MESSAGE_COMPOSE
Reply to a message Reply MESSAGE_REPLY
Reply to all messages ReplyAll MESSAGE_REPLYALL
Forward a message Forward MESSAGE_FORWARD
Delete a message Delete MESSAGE_DELETE
Show address book Show MESSAGE_SHOWADBOOK
Show message details Show MESSAGE_SHOWDETAILS
Resolve recipient name ResolveName MESSAGE_RESOLVENAME
Delete recipient Delete RECIPIENT_DELETE
Delete attachment Delete ATTACHMENT_DELETE

Table 4.2 MAPIMessages Control Email Properties

Property Does This
Action Property Obsolete. Performs actions now performed by methods.
AddressCaption Sets caption of the address book.
AddressEditFieldCount Sets which address book edit controls to display.
AddressLabel Sets appearance of "To" edit control in address book.
AddressModifiable Sets whether address book can be modified by user.
AttachmentCount Gets total number of attachments for current message.
AttachmentIndex Set currently indexed attachment.
AttachmentName Sets the name of the currently indexed attachment.
AttachmentPathName Sets full path name of the currently indexed attachment.
AttachmentPosition Sets position of indexed attachment in the message body.
AttachmentType Sets type of currently indexed attachment.
FetchSorted Property Sets message order when creating message set.
MsgConversationID Sets the conversation thread identification value.
MsgCount Gets the total number of messages in message set.
MsgDateReceived Gets date on which current indexed message was received.
MsgID Gets string identifier of current message.
MsgIndex Sets index number of current message.
MsgNoteText Text of current message.
MsgOrigAddress Gets email address of originator of current message.
MsgOrigDisplayName Gets originator's name for current message.
MsgRead True or False depending on whether message has been read.
MsgReceiptRequested Indicates if return receipt is requested for message.
MsgSent Indicates if the message has been sent to mail server.
MsgSubject Message's subject.
MsgType Sets type of current message.

To get the messages in the Inbox, we use the MAPIMessage method Fetch. This creates a message set in the MAPIMessages control, and you can find out how many messages are in this set using the control's MsgCount property. Then you set the MAPIMessages control's MsgIndex property to point to the various messages in the message set. When a message is selected, you can examine it (and display it to the user) by using the various properties of the MAPIMessages control, such as the MsgOrigDisplayName property, which gives the name of the sender, or originator, of the message. You can get the email's subject from the MsgSubject property, the text of the message from the MsgNoteText property, and the date it was received from the MsgDateReceived property. In this way, you can work with the email messages in the computer's Inbox.

The MAPIMessages control also has an Action property, and you can perform standard email operations by setting the Action property to predefined constants, as in Table 4.1. However, the Action property is now considered obsolete, and Microsoft recommends use of the MAPIMessages methods, such as Fetch, Compose, and so on.

When we compose a new email message to send, that message is clearly not part of a message set, because it doesn't yet exist. For that reason, we set the MAPIMessages1 control's MsgIndex to –1 (this is necessary when you want to compose a new message):

Private Sub mnuFileSend_Click()
    MAPISession1.SignOn
    If Err <> 0 Then
        MsgBox "Logon Failure: " + Error$
    End If
    MAPIMessages1.SessionID = MAPISession1.SessionID
—> MAPIMessages1.MsgIndex = -1
        .
        .
        .
End Sub

To compose the new message, we use the MAPIMessages Compose method:

Private Sub mnuFileSend_Click()
    MAPISession1.SignOn
    If Err <> 0 Then
        MsgBox "Logon Failure: " + Error$
    End If
    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.MsgIndex = -1
—> MAPIMessages1.Compose
        .
        .
        .
End Sub

To let the user compose and send an email message, we use the Send method. This method takes an optional parameter that we can set to True or False (the default). Setting it to True places the Compose dialog box on the screen; leaving it False hides that box. We'll set the parameter to True:

Private Sub mnuFileSend_Click()
    MAPISession1.SignOn
    If Err <> 0 Then
        MsgBox "Logon Failure: " + Error$
    End If
    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.MsgIndex = -1
    MAPIMessages1.Compose
—> MAPIMessages1.Send True
        .
        .
        .
End Sub

The preceding code will place the Microsoft Exchange Compose dialog box on the screen:

The user then addresses and types the message and clicks the Send button, sending the message. All that's left is to sign off from the MAPI session using the MAPISession SignOff method:

Private Sub mnuFileSend_Click()
    MAPISession1.SignOn
    If Err <> 0 Then
        MsgBox "Logon Failure: " + Error$
    End If
    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.MsgIndex = -1
    MAPIMessages1.Compose
    MAPIMessages1.Send True
—> MAPISession1.SignOff
End Sub

Run eMailer and select the Send email… item in the File menu. The Microsoft Exchange Compose dialog box appears, as shown in Figure 4.2, and you can use it to send email.

Figure 4.2 Composing and sending email.

At this point, we can send and receive email. Our final goal for eMailer is to let the user register automatically through email with the click of the mouse.

© 1997 by Steven Holzner. All rights reserved.