Flylib.com

Books Software

 
 
 

19.6 Use MAPI to Send E-Mail Messages


19.6 Use MAPI to Send E-Mail Messages

Problem

You want to send an e-mail message, but you don't have a Simple Mail Transfer Protocol (SMTP) mail server configured for the computer.

Solution

Use the MAPI (Message Application Programming Interface) COM component, which interacts with the mail account configured on the current computer.

Discussion

MAPI is an interface that allows you to interact with the mailing features that are integrated into the Windows operating system. You can use MAPI (either through its unmanaged API, or through the MAPI COM component) to interact with the default mail client (usually Microsoft Outlook or Outlook Express). Tasks include retrieving contact information from the address box, retrieving the messages in the Inbox, and programmatically composing and sending messages. You should note that the MAPI control is not included with .NET—it's installed with earlier versions of Visual Studio, such as Visual Basic 6. Although you can redistribute the MAPI ActiveX control with applications that use it, you'll need to have a product such as Visual Basic 6 in order to develop with it.

{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

To use MAPI, right-click your project in the Solution Explorer, select Add Reference, and choose the COM tab. Find the Microsoft MAPI Controls 6.0 (msmapi32.ocx) item in the list, and add it. The necessary interop assemblies will be added to your project immediately. The objects are located in an MSMAPI namespace.

Figure 19-5 shows a simple e-mail client that uses MAPI to download a list of messages in the local Inbox and send a test message.

click to expand
Figure 19-5: A simple MAPI e-mail client.

The code simply creates and logs into a MAPI session using a MAPISession object, and then accesses mail services through a MAPIMessagesClass object.

Public Class EmailClient Inherits System.Windows.Forms.Form ' (Designer code omitted.) Private Sub cmdSend_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdSend.Click ' Start a new session. Dim Session As New MSMAPI.MAPISession() ' You can disable the

user

interface, provided you have ' already configured the account correctly. Session.LogonUI = False ' Do not retrieve mail from the mail server. Session.DownLoadMail = False ' Start a session. Session.SignOn() ' Create a new message. Dim Messages As New MSMAPI.MAPIMessagesClass() Messages.SessionID = Session.SessionID Messages.Compose() ' Specify some message information. Messages.RecipDisplayName = "

matthew

@prosetech.com" Messages.MsgNoteText = "Hey this is great the MAPI code works fine :)" Messages.Send(False) ' End the session. Session.SignOff() End Sub Private Sub cmdReceive_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdReceive.Click ' Start a new session. Dim Session As New MSMAPI.MAPISession() Session.LogonUI = False ' Do not retrieve mail from the mail server. Session.DownLoadMail = False ' Start a session. Session.SignOn() ' Fetch the e-mail in the Inbox. Dim Messages As New MSMAPI.MAPIMessagesClass() Messages.SessionID = Session.SessionID Messages.Fetch() ' Add all the messages to the ListView control. Dim i As Integer For i = 0 To Messages.MsgCount - 1 Messages.MsgIndex = i Dim lvItem As New ListViewItem(Messages.MsgOrigDisplayName) lvItem.SubItems.Add(Messages.MsgSubject) lvItem.SubItems.Add(Messages.MsgDateReceived) lstInbox.Items.Add(lvItem) Next ' End the session. Session.SignOff() End Sub End Class



19.7 Play Media Files

Problem

You want to play movie video (for example, MPEG files) or MP3 audio files.

Solution

Add the ActiveX MCI control for multimedia, and connect it to a PictureBox control if you need to display video.

Discussion

The .NET Framework does not include any classes for playing multimedia such as sounds, videos , or MIDI files. Recipe 10.14 described how you can use the Win32 API to play WAV files. Even more functionality is available if you use the MCI ActiveX control. However, the MCI ActiveX control is not included with .NET—it's installed with earlier versions of Visual Studio, such as Visual Basic 6. Although you can redistribute the MCI ActiveX control with applications that use it, you'll need to have a product such as Visual Basic 6 in order to develop with it.

To add the MCI control to a project, right-click the Toolbox and choose Customize Toolbox. Then select the COM Components tab, and check the Microsoft Multimedia Control 6.0 (mci32.ocx) item. This will add the MMControl to your Toolbox. When you drop this control onto a form, the necessary interop assemblies will be generated and added to your project.

The MCI control appears on a form as a bar of playback controls. Usually, these controls will be hidden from the user . Your application code can interact with the MCI control to start and stop playback. You perform actions such as opening a file, playing it, and so on, by setting the control's Command property with an action string like Open or Play .

Figure 19-6 shows a simple demonstration of the MCI control that adds two custom playback buttons .


Figure 19-6: Video playback with the MCI control.

When the form loads, a sample MPEG file is opened, and a PictureBox is bound to the control. The code then uses the Command property to start and stop playback.

Public Class VideoForm Inherits System.Windows.Forms.Form ' (Designer code omitted.) Private Sub Form_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Open the file. MMC.FileName = Application.StartupPath & "\test.mpg" MMC.Command = "Open" ' Hide the playback bar. MMC.Visible = False ' Show video in the PictureBox pic. MMC.hWndDisplay = pic.Handle.ToInt32() End Sub Private Sub cmdPlay_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdPlay.Click ' Rewind to the beginning. MMC.Command = "Prev" ' Start playback. MMC.Command = "Play" End Sub Private Sub cmdStop_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdStop.Click ' Stop playback. MMC.Command = "Stop" End Sub End Class

Note 

If you need more extensive multimedia capabilities, you might want to use the Windows Media Player 9 Series Software Development Kit, which can be downloaded from http://www.microsoft.com/downloads . Microsoft also provides a managed DirectX toolkit for more ambitious multimedia development.