Programming Microsoft Outlook and Microsoft Exchange 2003, Third Edition [Electronic resources]

Thomas Rizzo

نسخه متنی -صفحه : 227/ 152
نمايش فراداده

A CDO for Exchange .NET Sample Application

Using CDO for Exchange with .NET is straightforward. You just add your references to the CDO library and then start coding. The following sample connects to a public folder and creates a new post in the folder. Then it creates a new appointment in the user's mailbox. It is shown in Visual Basic .NET.

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "

#End Region
Private Sub cmdGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cmdGo.Click
'Show how to use CDO for Exchange with VB.NET
'Open a public folder
'Create a new post in the public folder
Dim strURL As String = "http://thomrizwin2k/public/test/"
Dim strMailBoxURL As String = _
"http://thomrizwin2k/exchange/thomriz/calendar/"
Dim oFolder As New CDO.Folder()
'To make sure we use EXOLEDB, pass a new connection object to
'the open call
Dim oConnection As New ADODB.Connection()
oConnection.Provider = "EXOLEDB.DataSource "
oConnection.ConnectionString = strURL
oConnection.Open()
oFolder.DataSource.Open(strURL, oConnection, _
ADODB.ConnectModeEnum.adModeReadWrite)
MsgBox(oFolder.DisplayName & " has " & oFolder.ItemCount & " items.")
'Add a new item to the folder
Dim oItem As New CDO.Item()
oItem.ContentClass = "urn:content-classes:message"
oItem.Fields("urn:schemas:httpmail:subject").Value = "My New Post"
oItem.Fields("urn:schemas:httpmail:textdescription").Value = _
"New Message Body"
oItem.Fields("http://schemas.microsoft.com/exchange/" & _
"outlookmessageclass").Value = "IPM.Post"
oItem.Fields.Update()
oItem.DataSource.SaveToContainer(strURL, oConnection)
'Create a new appointment as well
Dim oAppt As New CDO.Appointment()
'Need to create a new Connection object to the
'mailbox store
Dim oConnectionMailbox As New ADODB.Connection()
oConnectionMailbox.Provider = "EXOLEDB.DataSource "
oConnectionMailbox.ConnectionString = strMailBoxURL
oConnectionMailbox.Open()
oAppt.Subject = "My New Appointment"
oAppt.StartTime = "1/23/2003 10:00 AM"
oAppt.EndTime = "1/23/2003 11:00 AM"
oAppt.Location = "Building 34"
oAppt.TextBody = "This is the body of an appointment."
oAppt.Fields("http://schemas.microsoft.com/exchange/" & _
"outlookmessageclass").Value = "IPM.Appointment"
oAppt.Fields.Update()
oAppt.DataSource.SaveToContainer(strMailBoxURL, oConnectionMailbox)
oConnection.Close()
oConnectionMailbox.Close()
End Sub
End Class

Here is a scaled-down C# version of the sample:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CDO;
namespace CDOEx2kCSharp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdGo;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
. . .
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void cmdGo_Click(object sender, System.EventArgs e)
{
string strURL = "http://thomrizwin2k/public/test";
CDO.Folder oFolder = new CDO.Folder();
ADODB.Connection oConnection = new ADODB.Connection();
oConnection.Provider = "EXOLEDB.DataSource ";
oConnection.ConnectionString = strURL;
oConnection.Open(oConnection.ConnectionString,",",0);
oFolder.DataSource.Open(strURL, oConnection,
ADODB.ConnectModeEnum.adModeReadWrite,
ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
ADODB.RecordOpenOptionsEnum.adOpenSource, ", ");
MessageBox.Show(oFolder.DisplayName + " has "
+ oFolder.ItemCount + " items.");
//Add a new item
CDO.Item oItem = new CDO.ItemClass();
oItem.ContentClass = "urn:content-classes:message";
oItem.Fields["urn:schemas:httpmail:subject"].Value =
"My New C# Post";
oItem.Fields["urn:schemas:httpmail:textdescription"].Value =
"New Message Body";
oItem.Fields["http://schemas.microsoft.com/"
+ "exchange/outlookmessageclass"].Value = "IPM.Post";
oItem.Fields.Update();
oItem.DataSource.SaveToContainer(strURL, oConnection,
ADODB.ConnectModeEnum.adModeReadWrite,
ADODB.RecordCreateOptionsEnum.adCreateNonCollection,
ADODB.RecordOpenOptionsEnum.adOpenSource, ", ");
oConnection.Close();
}
}
}