Visual Basic
  Home arrow Visual Basic arrow Page 3 - Sending Email With MAPI Components in Visu...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

Sending Email With MAPI Components in Visual Basic
By: Jason Brimblecombe
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 204
    2002-02-15

    Table of Contents:
  • Sending Email With MAPI Components in Visual Basic
  • Using MAPI Components
  • Using MAPI Components (contd.)
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    More Visual Basic Articles
    More By Jason Brimblecombe


       · This is great, but I don't like the way it asks me if I want to allow the program to...
       · "By: Anonymous Loozahat: 09-14-08 @ 8:21 pm EST This is great, but I don't like...
       · I am happy with the solution provided.But I am unable to send html mail using...
       · I found this an excellent introduction to basic email operation.Nice and clear...
     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek