Use the OLE DB Jet provider to access Exchange and Outlook data.
The sample code contains two event handlers:
Form.Load
Displays a form that allows the user to specify the mailbox name and mail profile to connect to.
- Connect
Button.Click
Creates and opens a connection to Outlook or Exchange data using the OLE DB .NET data provider. A
DataAdapter
is used to fill a table with theSubject
andContent
of each message in theInbox
. The default view of the table is bound to a data grid on the form.
The C# code is shown in Example 1-8.
Example 1-8. File: ConnectExchangeDataForm.cs
// Namespaces, variables, and constants using System; using System.Windows.Forms; using System.Data; using System.Data.OleDb; // . . . private void ConnectExchangeDataForm_Load(object sender, System.EventArgs e) { mailboxNameTextBox.Text = "Personal Folders"; profileTextBox.Text = "Outlook"; } private void connectButton_Click(object sender, System.EventArgs e) { String sqlText = "SELECT Subject, Contents FROM Inbox"; // Build the connection string. String connectionString="Provider=Microsoft.Jet.OLEDB.4.0;" + "Outlook 9.0;" + "MAPILEVEL=" + mailboxNameTextBox.Text + "|;" + "PROFILE=" + profileTextBox.Text + ";" + "TABLETYPE=0;" + "DATABASE=" + System.IO.Path.GetTempPath( ); // Create the DataAdapter. OleDbDataAdapter da = new OleDbDataAdapter(sqlText, connectionString); // Create and fill the table. DataTable dt = new DataTable("Inbox"); try { da.Fill(dt); dataGrid.DataSource = dt.DefaultView; } catch(Exception ex) { MessageBox.Show("ERROR: " + ex.Message); return; } }
The .NET data provider for OLE DB does not support OLE DB Version 2.5 interfaces including the Microsoft OLE DB Provider for Exchange. The Jet OLE DB provider can access an Outlook or Exchange store. An example of the connection string:
Microsoft.Jet.OLEDB.4.0;Outlook 9.0;MAPILEVEL=Personal Folders|; PROFILE=Outlook;TABLETYPE=0;DATABASE=c:\temp;
The connection string attributes-and-value pairs are described in Table 1-1.
Table 1-1. Outlook or Exchange connection string attributes
Element |
Description |
---|---|
|
With an Identifier of With an Identifier of |
|
|
|
Outlook or Exchange password. This parameter is not required if your network logon password is passed to the Outlook or Exchange server. This parameter is optional. |
|
The name of the Outlook or Exchange profile to use. If this not specified, the default profile is used. |
|
|
|
|
Get ADO.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.