正文

MAPI Tutorial2006-04-17 21:38:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/12623.html

分享到:

MAPI Tutorial


ID: 713
Author: Abstractvb
Date: 4/22/2000 6:50:49 PM
VB6

 

This Tutorial Covers
  • MAPISession and MAPIMessage Controls.
  • Logging onto the Mail Server.
  • Reading Email headers.
  • Sending Messages. (SMTP)
  • Receiving Messages. (POP3)
  • Forwarding Messages.
  • Deleting messages.
  • Displaying the Address Book.
  • Sending Attachments.

 

Part 1 - MAPI Session Control

In order to use this tutorial you must have the Microsoft MAPI Controls Installed. These reside in the file MSMAPI32.OCX.

The MAPI Session control provides the interface by which you can access the Mail Server. To use the MAPI controls a MAPI compliant mail server program must be installed on your machine. (Such as Microsoft Outlook Express)

To be able to get and send mail messages you have to sign on to the mail server using your ID and password. MAPI has two basic ways to do this: You can let the MAPI controls, prompt for the necessary information, or you can provide the information yourself though the code. Here are two examples: (One of each)

Private Sub Form_Load()
    'The MAPI controls will ask for UserName and Password
    MAPISession1.SignOn
    
    MAPISession1.SignOff
End Sub

Or you can do this:

Private Sub Form_Load()
    MAPISession1.UserName = "MyName"
    MAPISession1.Password = "MyPassword"
    MAPISession1.SignOn
    
    MAPISession1.SignOff
End Sub

This will not ask for the password and username since it is already being supplied though the code.

Now this code is not very functional since it just opens a connection and then closes the connection right away. To be able to provide messaging services you must use the MAPIMessage Control.

 

Part 2  - MAPI Message Control

The MAPIMessage control, is the one you will probably be working the most with. This control exposes all the properties and methods you need to create your own mail application with full send / receive capabilities. Like usual lets jump right into something useful. Try running this code:

Private Sub Form_Load()
    MAPISession1.DownLoadMail = True
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.Fetch
    MsgBox "You have " & MAPIMessages1.MsgCount & _ 
           " messages in your inbox!"
        
    MAPISession1.SignOff
End Sub

This code snippet will tell you how many messages you have in your inbox. Notice the line: MAPIMessages1.SessionID = MAPISession1.SessionID? Each time you start a MAPI session the API's behind the MAPI controls returns a unique session identifier called the SessionID. The SessionID must be passed from the MAPISession control to the MAPIMessage control before the message control can be used.

 

Part 3 - Email Headers and MAPI

Add a Listbox to your Form and call it List1. Then add this code and try running it:

Private Sub Form_Load()
    Dim i As Long
    
    MAPISession1.DownLoadMail = True
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    MAPIMessages1.Fetch
    
    If MAPIMessages1.MsgCount > 0 Then
        For i = 0 To MAPIMessages1.MsgCount - 1
            MAPIMessages1.MsgIndex = i
            List1.AddItem "From:" & MAPIMessages1.MsgOrigDisplayName & _
                         " Subject:" & MAPIMessages1.MsgSubject
        Next
    End If
    MAPISession1.SignOff
End Sub

You should have gotten a list of all the messages in your Inbox, showing who the message was from and the subject line of the message. 

This code is fairly straight forward except for the MsgIndex line. Think of the MAPIMessage control has having an Array it uses to store each message returned. (I doubt it really uses an array) By changing the MsgIndex property it is just like changing the index of an array, so you point at a new element, or in this case a new Email message. I also used the MsgOrigDisplayName property of the MAPIMessages control above instead of the MsgOrigAddress property to show the difference between the two. The   MsgOrigAddress property will show the Email address of the sender, while the MsgOrigDisplayName will show the display name of the sender. So you could have a display name of "Clark Kent" but an address of ckent@daily-planet.com.

There are plenty more headers to play like the MsgDateReceived header, but once you know to access one of them it's simple to access another.

 

Part 4 - Composing an Email Message using MAPI

The easiest way to compose and send an EMail message is to use your default mail program and let it handle all the dirty work, like this:

Private Sub Form_Load()
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    
    MAPIMessages1.Compose
    MAPIMessages1.Send True
    
    MAPISession1.SignOff
End Sub

As you can see this method is simple, but by using the send method without any specified parameters you give up a lot of control on how your application behaves. If you would like to completely control the process you can do this:

Private Sub Form_Load()
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    
    MAPIMessages1.Compose
    MAPIMessages1.RecipAddress = "abstractvb@home.com"
    MAPIMessages1.MsgSubject = "Love your site"
    MAPIMessages1.MsgNoteText = "Your MAPI tutorial is great."
        
    MAPIMessages1.Send False
    
    MAPISession1.SignOff
End Sub

By passing False to the Send method will not show the your default mail programs compose screen, but you must make sure to supply a Recipient Address. Add this line before the Send line:

    MAPIMessages1.Show

This line will show the AddressBook so the user may choose one or multiple Recipients from it. If you set the AddressModifiable property to True, the user will be able to modify address information in the AddressBook.

 

Part 5 - Creating Email Attachments

MAPI makes adding attachments to your email messages very simple. You don't need to encode your binary file using MIME or UUENCODE, instead the MAPIMessages control will handle all the encoding for you. (If you've ever seen the MIME specification, you will be VERY grateful for this feature.)

NOTE:Please change the RecipAddress. I don't want to get a million Autoexec.bat files!

Private Sub Form_Load()
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    
    MAPIMessages1.Compose
    MAPIMessages1.RecipAddress = "abstractvb@home.com"
    MAPIMessages1.MsgSubject = "Here is my Autoexec"
    MAPIMessages1.MsgNoteText = "Below you will find the file."
        
    'Add the Attachment at the end of the message
    MAPIMessages1.AttachmentPosition = Len(MAPIMessages1.MsgNoteText)
    
    'Set the type to a data file
    MAPIMessages1.AttachmentType = mapData
    
    'Give it a name
    MAPIMessages1.AttachmentName = "Autoexec File"
    
    'Specify what file to send
    MAPIMessages1.AttachmentPathName = "c:\autoexec.bat"
    
    MAPIMessages1.Send True
    
    MAPISession1.SignOff
End Sub
If you have problems or experience errors with adding the attachment try commenting out this line: MAPIMessages1.AttachmentPosition = Len(MAPIMessages1.MsgNoteText)  I have received feedback that some Mail programs will only accept attachments though MAPI if they are at the beginning of the message, and by commenting this line the attachment is sent fine and no error is returned.

If your still having problems Microsoft has two Knowledge Base articles on this that may help Error 32002 "Unspecified Error" when Adding an Attachment and Error 32002 Working with Attachments in MAPI Controls

 

Part 6 - Forwarding an Email Message
Private Sub Form_Load()
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    
    MAPIMessages1.Fetch
    
    'Forward the first message retrieved
    MAPIMessages1.Forward
    MAPIMessages1.RecipAddress = "abstractvb@home.com"
    MAPIMessages1.Send False
    
    MAPISession1.SignOff
End Sub
Part 7 - Deleting an Email Message
Private Sub Form_Load()
    MAPISession1.SignOn
    
    MAPIMessages1.SessionID = MAPISession1.SessionID
    
    MAPIMessages1.Fetch
    
    'Delete the first message retrieved
    MAPIMessages1.Delete
    MAPIMessages1.RecipAddress = "abstractvb@home.com"
    MAPIMessages1.Send False
    
    MAPISession1.SignOff
End Sub

阅读(4889) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册