チュートリアル : Exchange Server の WCF メール トランスポートを使用する
更新 : 2007 年 11 月
Microsoft Exchange Server を実行するコンピュータを媒介として使用することにより、Exchange Server の Windows Communication Foundation (WCF) メール トランスポートに基づいて、特定の入力/出力チャネル上でメッセージを送受信するアプリケーションを作成できます。Exchange Server の WCF メール トランスポートに基づくアプリケーションは、デスクトップとデバイスの両方でサポートされます。
このチュートリアルでは、Exchange Server の WCF メール トランスポートの機能について説明します。以下の作業について説明します。
コンピュータのセットアップ
デバイス (クライアント) 用のアプリケーションの作成
デスクトップ (サーバー) 用のアプリケーションの作成
ここで説明する手順に示すコードの完全な一覧については、このチュートリアルの最後の「例」を参照してください。
メモ : |
---|
実行コードでは、この例を使用しないでください。 |
前提条件
この例では .NET Compact Framework version 3.5 が必要です。
コンピュータのセットアップ
この手順では、この例を実行するために開発環境が適切にセットアップされていることを確認します。
例を実行するためにコンピュータをセットアップするには
Windows Mobile version 5.0 または Windows Mobile 6 ソフトウェア開発キット (SDK: Software Development Kit) をインストールします。
Windows Mobile 6 Professional SDK には ActiveSync Always-up-to-date (AUTD) 機能が含まれています。デバイス上のメール トランスポートはこの機能を使用して、[受信トレイ] を最新の状態に保ちます。詳しくは、Windows Mobile の Web サイトを参照してください。
Microsoft Exchange Server 2007 を実行している電子メール サーバーがネットワーク上で利用可能であることを確認します。
利用可能な Exchange Server 2007 がない場合、Virtual PC 上で実行される評価版を使用できます。詳細については、Exchange Server の Web サイトを参照してください。
コンピュータから Outlook Web Access (OWA) を実行して電子メール アカウントに接続することにより、コンピュータが新しい Exchange 電子メール サーバーに接続できることを確認します。
Exchange Web サービスが Exchange 電子メール サーバー上で有効になっていることを確認します。
これを確認する 1 つの方法は、URL として http://server-address/ews/exchange.asmx または https://server-address/ews/exchange.asmx を指定して、電子メール サーバー上の exchange.asmx ページにアクセスすることです。
エミュレータを使用する場合は、エミュレータにネットワーク機能を設定します。
メモ : ネットワーク接続が ActiveSync によって提供されている場合、ActiveSync AUTD 機能は実行されません。Device Emulator 2.0 を使用して、エミュレータ プロパティを構成することにより、NE2000 PCMCIA カードをホスト ネットワーク アダプタにバインドできます。Device Emulator 2.0 は Windows Mobile 6 Professional SDK に含まれていますが、別個にダウンロードすることもできます。
エミュレータが起動されて実行中になったら、Exchange 電子メール サーバーと通信するよう ActiveSync を構成します。詳細については、Microsoft TechNet Web サイトの「Deploying Windows Mobile 6 Powered Devices with Microsoft Exchange Server 2007」の「Step 5: Configure and Manage Mobile Device Access on the Exchange Server」を参照してください。
デバイス用のアプリケーションの作成
この手順では、クライアントの役割をするデバイス用のアプリケーションをビルドした後、メール トランスポートを使用してサーバーへのメッセージを作成および送信します。
デバイス用のアプリケーションを作成するには
新しいスマート デバイス プロジェクトを Visual Studio で作成します。
以下の参照をプロジェクトに追加します。
Microsoft.ServiceModel.Mail.dll
Microsoft.ServiceModel.Mail.WindowsMobile.dll
System.ServiceModel.dll
System.Runtime.Serialization.dll
CFMessagingSerializer クラスを追加します。「方法 : WCF アプリケーションでメッセージをシリアル化する」の説明を参照してください。
デスクトップでは、シリアル化したデータを作成するには、カスタム シリアライザと属性のいずれかを使用できます。ただし、デバイスとデスクトップの両方で同じシリアライザを使用することをお勧めします。
メッセージをビルドします。
Dim str As String = "Hello"
String str = "Hello";
メッセージを作成して、それをシリアル化します。
' Create the message. Dim serializer As New CFMessagingSerializer(GetType(String)) Dim m As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer)
// Create the message. CFMessagingSerializer serializer = new CFMessagingSerializer(typeof(string)); Message m = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer);
チャネル名、デバイスの電子メール アドレス、サーバーの電子メール アドレスを表す変数をそれぞれ作成します。
Dim channelName As String = "StoreandFowardMessageHelloWorld" Dim serverAddress As String = "ServerMailAddress@fabrikam.com" Dim clientAddress As String = "DeviceMailAddress@fabrikam.com"
string channelName = "StoreandFowardMessageHelloWorld"; string serverAddress = "ServerMailAddress@fabrikam.com"; string clientAddress = "DeviceMailAddress@fabrikam.com";
出力チャネルをビルドします。
' Build the output channel. Dim binding As New WindowsMobileMailBinding() Dim parameters As New BindingParameterCollection() Dim channelFactory As IChannelFactory(Of IOutputChannel) channelFactory = binding.BuildChannelFactory(Of IOutputChannel)(parameters) channelFactory.Open() Dim outChannel As IOutputChannel = channelFactory.CreateChannel(New EndpointAddress(MailUriHelper.Create(channelName, serverAddress))) outChannel.Open()
// Build the output channel. WindowsMobileMailBinding binding = new WindowsMobileMailBinding(); BindingParameterCollection parameters = new BindingParameterCollection(); IChannelFactory<IOutputChannel> channelFactory = binding.BuildChannelFactory<IOutputChannel>(parameters); channelFactory.Open(); IOutputChannel outChannel = channelFactory.CreateChannel(new EndpointAddress(MailUriHelper.Create(channelName,serverAddress))); outChannel.Open();
メッセージを送信するコードを追加します。
' Send the message. outChannel.Send(m)
// Send the message. outChannel.Send(m);
リスナをビルドして、応答をリッスンするコードを追加します。
リスナはコードの実行をブロックします。このため、実行コードでは、別個のスレッドでリスナを実行することをお勧めします。この例では、送信コードの後にリスナ コードを追加します。
' Listen for the response. Dim listener As IChannelListener(Of IInputChannel) listener = binding.BuildChannelListener(Of IInputChannel)(MailUriHelper.CreateUri(channelName, clientAddress)) listener.Open() Dim inputChannel As IInputChannel = listener.AcceptChannel() inputChannel.Open() Dim reply As Message = inputChannel.Receive()
// Listen for the response. IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel>(MailUriHelper.CreateUri(channelName,clientAddress),parameters); listener.Open(); IInputChannel inputChannel = listener.AcceptChannel(); inputChannel.Open(); Message reply = inputChannel.Receive();
サーバーからの応答を受信したら、応答を逆シリアル化して、結果をユーザーに表示します。
' When you receive a response, deserialize the message. str = reply.GetBody(Of String)(serializer) MessageBox.Show(str, "Received message")
// When you receive a response, deserialize the message. str = reply.GetBody<string>(serializer); MessageBox.Show(str, "Received message");
クリーンアップします。
outChannel.Close() channelFactory.Close() listener.Close() inputChannel.Close() binding.Close()
outChannel.Close(); channelFactory.Close(); listener.Close(); inputChannel.Close(); binding.Close();
クライアント アプリケーションをビルドして、Exchange 電子メール サーバーと同期するよう構成済みのエミュレータまたはデバイスにそれを配置します。
デスクトップ用のアプリケーションの作成
この手順では、この例でサーバーの役割をする、デスクトップ コンピュータ用のアプリケーションをビルドします。このサーバーはクライアントからのメッセージを処理し、メッセージにデータを付加してクライアントに戻します。
デスクトップ用のアプリケーションを作成するには
新しい Windows コンソール アプリケーションを作成します。
以下の参照をプロジェクトに追加します。
Microsoft.ServiceModel.Mail.dll
Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.dll
System.ServiceModel.dll
System.Runtime.Serialization.dll
CFMessagingSerializer クラスを追加します。「方法 : WCF アプリケーションでメッセージをシリアル化する」の説明を参照してください。
いくつかの変数を作成します。このうちいくつかは、デバイス プロジェクトの値と一致する必要があります。
' Set some global variables. Dim channelName As String = "StoreandFowardMessageHelloWorld" Dim serverAddress As String = "ServerMailAddress@fabrikam.com" Dim serverPWD As String = "MyPassword" Dim clientAddress As String = "DeviceMailAddress@fabrikam.com" Dim exchangeServerLocation As String = "http://fabrikam"
// Set some global variables. string channelName = "StoreandFowardMessageHelloWorld"; string serverAddress = "ServerMailAddress@fabrikam.com"; string serverPWD = "MyPassword"; string clientAddress = "DeviceMailAddress@fabrikam.com"; string exchangeServerLocation = "http://fabrikam";
リスナを作成します。
Windows 資格情報を使用する場合、2 番目の引数として null 値を ExchangeWebServiceMailBinding オブジェクトに渡します。デバイスの場合と同様に、入力チャネルはコードの実行をブロックします。このため、実行コードでは、各リスナごとに新しいスレッドを作成することをお勧めします。
' Create the listener. If you are using Windows credentials, ' pass a null value as the second argument to the ExchangeWebServiceMailBinding. Dim binding As MailBindingBase binding = New ExchangeWebServiceMailBinding(New Uri(exchangeServerLocation), New System.Net.NetworkCredential(serverAddress, serverPWD)) Dim parameters As New BindingParameterCollection() Dim listener As IChannelListener(Of IInputChannel) listener = binding.BuildChannelListener(Of IInputChannel)(MailUriHelper.CreateUri(channelName, serverAddress)) listener.Open() Dim inputChannel As IInputChannel = listener.AcceptChannel() inputChannel.Open() Dim reply As Message = inputChannel.Receive() Dim serializer As New CFMessagingSerializer(GetType(String)) Dim str As String = "" str = reply.GetBody(Of String)(serializer)
// Create the listener. If you are using Windows credentials, // pass a null value as the second argument to the ExchangeWebServiceMailBinding. MailBindingBase binding = new ExchangeWebServiceMailBinding(new Uri(exchangeServerLocation), new System.Net.NetworkCredential(serverAddress, serverPWD)); BindingParameterCollection parameters = new BindingParameterCollection(); IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel> (MailUriHelper.CreateUri(channelName, serverAddress), parameters); listener.Open(); IInputChannel inputChannel = listener.AcceptChannel(); inputChannel.Open(); Message reply = inputChannel.Receive(); CFMessagingSerializer serializer = new CFMessagingSerializer(typeof(string)); string str = ""; str = reply.GetBody<string>(serializer);
メッセージを処理するコードを追加します。
' Process the message. str += ", World!"
// Process the message. str += ", World!";
出力チャネルを介して応答を送信するコードを追加します。
' Send the response through an output channel. Dim m As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer) Dim channelFactory As IChannelFactory(Of IOutputChannel) channelFactory = binding.BuildChannelFactory(Of IOutputChannel)(parameters) channelFactory.Open() Dim outChannel As IOutputChannel = channelFactory.CreateChannel(New EndpointAddress(MailUriHelper.CreateUri(channelName, clientAddress))) outChannel.Open() outChannel.Send(m)
// Send the response through an output channel. Message m = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer); IChannelFactory<IOutputChannel> channelFactory = binding.BuildChannelFactory<IOutputChannel>(parameters); channelFactory.Open(); IOutputChannel outChannel = channelFactory.CreateChannel(new EndpointAddress( MailUriHelper.CreateUri(channelName, clientAddress))); outChannel.Open(); outChannel.Send(m);
クリーンアップします。
' Clean up. outChannel.Close() channelFactory.Close() listener.Close() inputChannel.Close() binding.Close()
// Clean up. outChannel.Close(); channelFactory.Close(); listener.Close(); inputChannel.Close(); binding.Close();
プロジェクトをビルドして、サーバー アプリケーションを起動します。
エミュレータまたはデバイス上でクライアント アプリケーションを起動します。
例
説明
以下に示す完全なコード例は、Exchange Server の WCF メール トランスポートを使ってメッセージを送受信する方法を示しています。
デバイス (クライアント) 用の完全なコード リスト
Dim str As String = "Hello"
' Create the message.
Dim serializer As New CFMessagingSerializer(GetType(String))
Dim m As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer)
Dim channelName As String = "StoreandFowardMessageHelloWorld"
Dim serverAddress As String = "ServerMailAddress@fabrikam.com"
Dim clientAddress As String = "DeviceMailAddress@fabrikam.com"
' Build the output channel.
Dim binding As New WindowsMobileMailBinding()
Dim parameters As New BindingParameterCollection()
Dim channelFactory As IChannelFactory(Of IOutputChannel)
channelFactory = binding.BuildChannelFactory(Of IOutputChannel)(parameters)
channelFactory.Open()
Dim outChannel As IOutputChannel = channelFactory.CreateChannel(New EndpointAddress(MailUriHelper.Create(channelName, serverAddress)))
outChannel.Open()
' Send the message.
outChannel.Send(m)
' Listen for the response.
Dim listener As IChannelListener(Of IInputChannel)
listener = binding.BuildChannelListener(Of IInputChannel)(MailUriHelper.CreateUri(channelName, clientAddress))
listener.Open()
Dim inputChannel As IInputChannel = listener.AcceptChannel()
inputChannel.Open()
Dim reply As Message = inputChannel.Receive()
' When you receive a response, deserialize the message.
str = reply.GetBody(Of String)(serializer)
MessageBox.Show(str, "Received message")
outChannel.Close()
channelFactory.Close()
listener.Close()
inputChannel.Close()
binding.Close()
End Sub
End Class
static void Main()
{
String str = "Hello";
// Create the message.
CFMessagingSerializer serializer = new CFMessagingSerializer(typeof(string));
Message m = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer);
string channelName = "StoreandFowardMessageHelloWorld";
string serverAddress = "ServerMailAddress@fabrikam.com";
string clientAddress = "DeviceMailAddress@fabrikam.com";
// Build the output channel.
WindowsMobileMailBinding binding = new WindowsMobileMailBinding();
BindingParameterCollection parameters = new BindingParameterCollection();
IChannelFactory<IOutputChannel> channelFactory = binding.BuildChannelFactory<IOutputChannel>(parameters);
channelFactory.Open();
IOutputChannel outChannel = channelFactory.CreateChannel(new EndpointAddress(MailUriHelper.Create(channelName,serverAddress)));
outChannel.Open();
// Send the message.
outChannel.Send(m);
// Listen for the response.
IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel>(MailUriHelper.CreateUri(channelName,clientAddress),parameters);
listener.Open();
IInputChannel inputChannel = listener.AcceptChannel();
inputChannel.Open();
Message reply = inputChannel.Receive();
// When you receive a response, deserialize the message.
str = reply.GetBody<string>(serializer);
MessageBox.Show(str, "Received message");
outChannel.Close();
channelFactory.Close();
listener.Close();
inputChannel.Close();
binding.Close();
}
デスクトップ (サーバー) 用の完全なコード リスト
' Set some global variables.
Dim channelName As String = "StoreandFowardMessageHelloWorld"
Dim serverAddress As String = "ServerMailAddress@fabrikam.com"
Dim serverPWD As String = "MyPassword"
Dim clientAddress As String = "DeviceMailAddress@fabrikam.com"
Dim exchangeServerLocation As String = "http://fabrikam"
' Create the listener. If you are using Windows credentials,
' pass a null value as the second argument to the ExchangeWebServiceMailBinding.
Dim binding As MailBindingBase
binding = New ExchangeWebServiceMailBinding(New Uri(exchangeServerLocation), New System.Net.NetworkCredential(serverAddress, serverPWD))
Dim parameters As New BindingParameterCollection()
Dim listener As IChannelListener(Of IInputChannel)
listener = binding.BuildChannelListener(Of IInputChannel)(MailUriHelper.CreateUri(channelName, serverAddress))
listener.Open()
Dim inputChannel As IInputChannel = listener.AcceptChannel()
inputChannel.Open()
Dim reply As Message = inputChannel.Receive()
Dim serializer As New CFMessagingSerializer(GetType(String))
Dim str As String = ""
str = reply.GetBody(Of String)(serializer)
' Process the message.
str += ", World!"
' Send the response through an output channel.
Dim m As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer)
Dim channelFactory As IChannelFactory(Of IOutputChannel)
channelFactory = binding.BuildChannelFactory(Of IOutputChannel)(parameters)
channelFactory.Open()
Dim outChannel As IOutputChannel = channelFactory.CreateChannel(New EndpointAddress(MailUriHelper.CreateUri(channelName, clientAddress)))
outChannel.Open()
outChannel.Send(m)
' Clean up.
outChannel.Close()
channelFactory.Close()
listener.Close()
inputChannel.Close()
binding.Close()
End Sub
End Class
static void Main()
{
// Set some global variables.
string channelName = "StoreandFowardMessageHelloWorld";
string serverAddress = "ServerMailAddress@fabrikam.com";
string serverPWD = "MyPassword";
string clientAddress = "DeviceMailAddress@fabrikam.com";
string exchangeServerLocation = "http://fabrikam";
// Create the listener. If you are using Windows credentials,
// pass a null value as the second argument to the ExchangeWebServiceMailBinding.
MailBindingBase binding = new ExchangeWebServiceMailBinding(new Uri(exchangeServerLocation),
new System.Net.NetworkCredential(serverAddress, serverPWD));
BindingParameterCollection parameters = new BindingParameterCollection();
IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel>
(MailUriHelper.CreateUri(channelName, serverAddress), parameters);
listener.Open();
IInputChannel inputChannel = listener.AcceptChannel();
inputChannel.Open();
Message reply = inputChannel.Receive();
CFMessagingSerializer serializer = new CFMessagingSerializer(typeof(string));
string str = "";
str = reply.GetBody<string>(serializer);
// Process the message.
str += ", World!";
// Send the response through an output channel.
Message m = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer);
IChannelFactory<IOutputChannel> channelFactory = binding.BuildChannelFactory<IOutputChannel>(parameters);
channelFactory.Open();
IOutputChannel outChannel = channelFactory.CreateChannel(new EndpointAddress(
MailUriHelper.CreateUri(channelName, clientAddress)));
outChannel.Open();
outChannel.Send(m);
// Clean up.
outChannel.Close();
channelFactory.Close();
listener.Close();
inputChannel.Close();
binding.Close();
}
コードのコンパイル
デバイス上で実行される例では、以下の名前空間への参照が必要です。
デスクトップ上で実行される例では、以下の名前空間への参照が必要です。
セキュリティ
この例では、メール トランスポートのセキュリティが有効になっていません。詳細については、「Exchange Server の WCF メール トランスポート」を参照してください。
参照
処理手順
方法 : Exchange Server の WCF メール トランスポートでメッセージ セキュリティを使用する
その他の技術情報
Windows Communication Foundation (WCF) 開発と .NET Compact Framework