Home arrow Visual Basic arrow Page 3 - Sending Email With MAPI Components in Visual Basic
VISUAL BASIC

Sending Email With MAPI Components in Visual Basic


MAPI is an acronym for the Messaging Application Programming Interface. It's a set of core system components that can be used to automate several windows tasks. MAPI components allow developers to create mail-enabled Visual Basic applications easily. In this article Jason shows us how to use MAPI controls to read messages from our inbox, iterate through these messages, and more.

Author Info:
By: Jason Brimblecombe
Rating: 5 stars5 stars5 stars5 stars5 stars / 233
February 15, 2002
TABLE OF CONTENTS:
  1. · Sending Email With MAPI Components in Visual Basic
  2. · Using MAPI Components
  3. · Using MAPI Components (contd.)
  4. · Conclusion

print this article
SEARCH DEVARTICLES

Sending Email With MAPI Components in Visual Basic - Using MAPI Components (contd.)
(Page 3 of 4 )

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).

Here's what our application looks like when it's running:

Our VB MAPI application just before sending mail
blog comments powered by Disqus
VISUAL BASIC ARTICLES

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials