Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5381335
  • 博文数量: 671
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 7310
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-14 09:56
文章分类

全部博文(671)

文章存档

2011年(1)

2010年(2)

2009年(24)

2008年(271)

2007年(319)

2006年(54)

我的朋友

分类:

2007-04-27 17:25:57

Sending Email With MAPI Components in Visual Basic - Using MAPI Components


Using the MAPI controls involves two steps: firstly establishing a MAPI session, and secondly using various MAPI properties and methods to access and manage an individual inbox; for example, to create and send a message, include a file attachment, verify the recipient's email address against the email system's address book, etc.

Let's get started on the Visual Basic project, shall we? Start by creating a new standard executable application. You will need to add the MAPI components to your project, so start by clicking project from the top menu and find the menu item components (you may also use the hotkey CTRL+T). When the components window appears, scroll down the list until you find the Microsoft MAPI controls:

Adding the MAPI controls to our project

Place a check in the box next to it, and then click the OK button. Two additional controls have been added to the Visual Basic toolbox; they're called MAPISession and MAPIMessages. We will be using both of these in our application.

Setting up the form

Let's setup the layout of our Visual Basic application. Our form will contain four textboxes, three labels, four command buttons, and both of the MAPI components. You should layout the controls on your form so that it looks like this (alternatively, you can see the support material for this article and download the complete project):

The layout of our MAPI VB form

Here are the details of each component on our Visual Basic form:

Details of the controls on our form
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
In this article we've created a program that sends and receives e-mail messages using the windows built-in MAPI components. Please note that we've only looked at the very basics of using the MAPI components in this article.

There are many other features that you might want to add to this project, such as a label that displays the Message number (what message is being viewed), and even something as simple as a clear button that erases the contents of the text boxes on the form when it's clicked. You can also take a look at the links and books below if you're after more information on using the MAPI components.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
阅读(2232) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~