Wednesday, January 31, 2007

Sending Email With MAPI Components in Visual Basic

Now that we've got the layout and details of the controls on our form sorted out, let's add some code to our form to interact with our MAPI compatible mail system.
Start by opening the general declerations section in the code window. Enter the following code:
Option Explicit
Dim lPosition As Long
The long integer variable that we've created in the code snippet above will be used to remember which message we are currently viewing. This variable will also be used to check whether or not we are currently viewing the last message in the inbox.
Our next step is to add some code to the Form_Load() event. Add this code to your application:
Private Sub Form_Load()
'Sign on to the MAPI Session
MAPISession1.SignOn
MAPIMessages1.SessionID = MAPISession1.SessionID
MAPIMessages1.Fetch
If MAPIMessages1.MsgCount > 0 Then
'If there are messages, display the first one.
txtFrom.Text = MAPIMessages1.MsgOrigDisplayName
txtTo.Text = MAPIMessages1.RecipDisplayName
txtSubject.Text = MAPIMessages1.MsgSubject
txtMessage.Text = MAPIMessages1.MsgNoteText
'Enable the buttons to allow message navigation.
cmdBack.Enabled = True
cmdForward.Enabled = True
cmdSend.Enabled = True
Else
'No Messages? Tell the user immediately and Sign off!
MsgBox "You have no messages”
MAPISession1.SignOff
'Disable the buttons to avoid error messages
cmdBack.Enabled = False
cmdForward.Enabled = False
cmdSend.Enabled = False
End If
End Sub
At runtime your application will automatically sign on to a new MAPI session and check for any e-mails. If there are no e-mails, then we are informed and the session is terminated. On the other hand, if there was at least one e-mail in the Inbox, the program will show the first one in the list.
We are now going to configure the forward and backwards buttons to allow our users to navigate their way through the list of emails in their inbox. Add this code to the cmdForward_Click() event:
Private Sub cmdForward_Click()
'If the user has not reached the end of the list, show the next message.
If lPosition < MAPIMessages1.MsgCount Then
lPosition = lPosition + 1
'Change the Message number to the value in lPosition
MAPIMessages1.MsgIndex = lPosition
txtFrom.Text = MAPIMessages1.MsgOrigDisplayName
txtTo.Text = MAPIMessages1.RecipDisplayName
txtSubject.Text = MAPIMessages1.MsgSubject
txtMessage.Text = MAPIMessages1.MsgNoteText
'The 4 lines above will preview the current message.
End If
End Sub
Add the following code to the cmdBack_Click() event:
Private Sub cmdBack_Click()
'If the user hasn’t reached the beginning of the list, preview message
If lPosition > 0 Then
lPosition = lPosition – 1
'Change the Message number to the value in lPosition
MAPIMessages1.MsgIndex = lPosition
txtFrom.Text = MAPIMessages1.MsgOrigDisplayName
txtTo.Text = MAPIMessages1.RecipDisplayName
txtSubject.Text = MAPIMessages1.MsgSubject
txtMessage.Text = MAPIMessages1.MsgNoteText
'The 4 lines above will preview the current message.
End If
End Sub
Our program is now capable of previewing e-mail messages in the Inbox of our MAPI-compatible mailbox, such as Outlook Express.
Let's now take it to the next level by adding the ability to send e-mail. Don't let this frighten you off in any way, because it is actually just as easy to send email as it is to receive it and preview it by using the text boxes on the form. Add this code to the Click() method of the cmdSend button:
Private Sub cmdSend_Click()
'Start by telling the control that we are composing an e-mail
MAPIMessages1.Compose
'Use whatever is in the Textboxes as the information for our e-mail.
MAPIMessages1.RecipDisplayName = txtTo.Text
MAPIMessages1.MsgSubject = txtSubject.Text
MAPIMessages1.MsgNoteText = txtMessage.Text
MAPIMessages1.ResolveName
'Send the e-mail message to the Recipient
MAPIMessages1.Send
End Sub
Your application can now send emails, as well as receive them. To send an email, all we have to do is change the text in the To, Subject, and Message boxes. The email message will be sent using the email address defined in our outgoing mail application, so you don't need to change the text in the from box. There is still one more thing that needs to be added to your application. The "Close Session" button still doesn't have any code in it. Add the following code to the Click() event of the cmdClose button:
Private Sub cmdClose_Click()
MAPISession1.SignOff
Unload Me
End Sub
When clicked, the close session button signs the user oout of their e-mail client that he/she was connected to when MAPI called its SignOn method in for Form_Load() event. After we're signed out, our app will then unload itself from memory (exit the program).

1 comment:

Anonymous said...

GREAT !!