チュートリアル: WCF Exchange Server のメール トランスポートを使用します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

Windows Communication Foundation (WCF) Microsoft Exchange Server メディエイターとして実行しているコンピューターを使って、指定の入出力チャネルにメッセージを送受信するメール トランスポートを Exchange Server に基づくアプリケーション作成できます。 WCF の Exchange Server 電子メール トランスポートに基づくアプリケーションは、デスクトップとデバイスの両方にサポートされています。

このチュートリアルでは、WCF の Exchange Server 電子メール トランスポートの機能を紹介します。 これは、次のタスクを示します。

  • コンピューターを設定します。

  • デバイス (クライアント) のアプリケーションを構築します。

  • デスクトップ (サーバー) は、アプリケーションを構築します。

これらの手順に示すコードの一覧については このチュートリアルの最後に「 の例を参照してください。

注意

次の使用例を実稼働コードで使わないでください。

前提条件

この例では、.NET Compact Framework Version 3.5 必要があります。

コンピューターの設定

この手順で確認する開発環境が正しくセットアップされた例を実行します。

この例を実行するようコンピューターを設定するには

  1. Windows Mobile Version 5.0 または Windows Mobile 6 ソフトウェア開発キット (SDK) をインストールします。

    Windows Mobile 6 には最新の の受信トレイにデバイス上には、メール トランスポートによって使用される ActiveSync 常に最大-累計 (AUTD) 機能、Professional SDK。 詳細については、Windows Mobile の Web サイトの を参照してください。

  2. Microsoft Exchange Server 2007 を実行している、電子メール サーバーがネットワーク上で利用できることを確認します。

    Exchange Server 2007 利用がない場合 Virtual PC で実行される試用版を使用できます。 詳細については、Exchange サーバーの Web サイトの を参照してください。

  3. コンピューターから Outlook Web Access (OWA) を実行して、電子メール アカウントへの接続コンピューター、新しい Exchange 電子メール サーバーに接続できることを確認します。

  4. Exchange Web サービスは、Exchange の電子メール サーバーに有効になっていることを確認します。

    1 つ方法をこの確認は次の URL のいずれかを使用して電子メール サーバー上の exchange.asmx ページにアクセスことです。http://サーバー アドレス/ews/exchange.asmx または"https://"でサーバー アドレス/ews/exchange.asmx。

  5. エミュレーターを使用している場合は、エミュレーター ネットワーク機能を提供します。

    注意

    ActiveSync AUTD 機能は、ActiveSync は、ネットワーク接続を提供するときに実行されません。デバイス エミュレーター、2. 0 Professional SDK の Windows Mobile 6 とは別のダウンロード、含まれるを使用してエミュレーターのプロパティを構成して、NE2000 PCMCIA カードをホスト ネットワーク アダプターにバインドすることができます。

  6. 場合、エミュレーターをしたし、ActiveSync Exchange 電子メール サーバーと通信する構成を実行します。 詳細についてを参照してください"ステップ 5。構成し、Exchange サーバー上のモバイル デバイス アクセスの管理」を展開する Windows Mobile 6 Powered デバイスでマイクロソフト TechNet Web サイト上の Microsoft Exchange Server 2007 とします。

デバイス用アプリケーションの作成

この手順では、クライアントを表すデバイスのアプリケーションをビルドおよび作成し、メール トランスポートを使用してサーバーにメッセージを送信します。

アプリケーション、デバイスを作成するには

  1. Visual Studio で新しいスマート デバイス プロジェクトを作成します。 

  2. プロジェクトに以下の参照設定を追加します:

    • Microsoft.ServiceModel.Mail.dll

    • Microsoft.ServiceModel.Mail.WindowsMobile.dll

    • System.ServiceModel.dll

    • System.Runtime.Serialization.dll

  3. CFMessagingSerializer に記載されている 方法 : WCF アプリケーション内のメッセージをシリアル化します。 クラスを追加します。

    デスクトップで、シリアル化されたデータを作成するのにカスタム シリアライザーをまたは属性使用できます。 ただし、デバイスとデスクトップの両方に対して同じシリアライザーを使用することをお勧めします。

  4. メッセージを作成します。

                                  Dim str AsString = "Hello"
    
    String str = "Hello";
    
  5. メッセージを作成し、シリアル化します。

                                  ' Create the message.
                                  Dim serializer AsNew 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);
    
  6. チャネル名、デバイスの電子メール アドレス、およびサーバーの電子メール アドレスを表す変数を作成します。

                                  Dim channelName AsString = "StoreandFowardMessageHelloWorld"Dim serverAddress AsString = "ServerMailAddress@fabrikam.com"Dim clientAddress AsString = "DeviceMailAddress@fabrikam.com"
    
                                  string channelName = "StoreandFowardMessageHelloWorld";
    string serverAddress = "ServerMailAddress@fabrikam.com";
    string clientAddress = "DeviceMailAddress@fabrikam.com";
    
  7. ビルドの出力チャネル。

                                  ' Build the output channel.
                                  Dim binding AsNew WindowsMobileMailBinding()
    Dim parameters AsNew 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();
    
  8. メッセージを送信するためコードを追加します。

                                  ' Send the message.
    outChannel.Send(m)
    
                                  // Send the message.
    outChannel.Send(m);
    
  9. リスナーをビルドし、応答を待機するコード追加します。

    リスナー ブロックのコード実行します。 したがって、実稼働コードで別のスレッドで、リスナーを実行することをお勧めします。 この例では、リスナー コードを送信者コードの後に追加します。

                                  ' 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();
    
  10. サーバーから応答が受信したと、応答を逆シリアル化をユーザーに、結果を提示します。

                                  ' When you receive a response, deserialize the message.
    str = reply.GetBody(OfString)(serializer)
    
    MessageBox.Show(str, "Received message")
    
                                  // When you receive a response, deserialize the message.
    str = reply.GetBody<string>(serializer);
    
    MessageBox.Show(str, "Received message");
    
  11. 後処理を実行します。

    outChannel.Close()
    channelFactory.Close()
    
    listener.Close()
    inputChannel.Close()
    binding.Close()
    
    outChannel.Close();
    channelFactory.Close();
    
    listener.Close();
    inputChannel.Close();
    binding.Close();
    
  12. クライアント アプリケーションをビルドし、エミュレーターまたはデバイスを構成した Exchange 電子メール サーバーと同期に展開します。

デスクトップのアプリケーションの作成

この手順では、デスクトップ コンピューターで次の使用例でサーバーを表すアプリケーションを構築します。 サーバーは、クライアントからのメッセージを処理し、追加、クライアントに送り返します。

デスクトップのアプリケーションを作成

  1. 新しい Windows コンソール アプリケーションを作成します。

  2. プロジェクトに以下の参照設定を追加します:

    • Microsoft.ServiceModel.Mail.dll

    • Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.dll

    • System.ServiceModel.dll

    • System.Runtime.Serialization.dll

  3. CFMessagingSerializer に記載されている 方法 : WCF アプリケーション内のメッセージをシリアル化します。 クラスを追加します。

  4. デバイス プロジェクトから値に対応する必要がありますのいくつか変数を作成します。

                                  ' Set some global variables. 
                                  Dim channelName AsString = "StoreandFowardMessageHelloWorld"Dim serverAddress AsString = "ServerMailAddress@fabrikam.com"Dim serverPWD AsString = "MyPassword"Dim clientAddress AsString = "DeviceMailAddress@fabrikam.com"Dim exchangeServerLocation AsString = "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";
    
  5. リスナーを作成します。

    Windows の資格情報を使用している場合、NULL 値を 2 番目の引数として 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 AsNew 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 AsNew CFMessagingSerializer(GetType(String))
    
    Dim str AsString = ""
    str = reply.GetBody(OfString)(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);
    
  6. コード メッセージの処理を追加します。

                                  ' Process the message.
    str += ", World!"
    
                                  // Process the message.
    str += ", World!";
    
  7. コード、出力チャネルを通じて、返信を追加します。

                                  ' 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);
    
  8. 後処理を実行します。

                                  ' Clean up.
    outChannel.Close()
    channelFactory.Close()
    
    listener.Close()
    inputChannel.Close()
    binding.Close()
    
                                  // Clean up.
    outChannel.Close();
    channelFactory.Close();
    
    listener.Close();
    inputChannel.Close();
    binding.Close();
    
  9. プロジェクトをビルドし、サーバー アプリケーションを起動します。

  10. エミュレーターまたはデバイスには、クライアント アプリケーションを起動します。

使用例

説明

次の完全な例、WCF の Exchange Server のメール トランスポートを使用してメッセージを送受信する方法を示します。

デバイス (クライアント) 用のコード リストを完了します。

                          Dim str AsString = "Hello"        ' Create the message.Dim serializer AsNew CFMessagingSerializer(GetType(String))
        Dim m As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:test", str, serializer)
        Dim channelName AsString = "StoreandFowardMessageHelloWorld"Dim serverAddress AsString = "ServerMailAddress@fabrikam.com"Dim clientAddress AsString = "DeviceMailAddress@fabrikam.com"        ' Build the output channel.Dim binding AsNew WindowsMobileMailBinding()
        Dim parameters AsNew 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(OfString)(serializer)

        MessageBox.Show(str, "Received message")
        outChannel.Close()
        channelFactory.Close()

        listener.Close()
        inputChannel.Close()
        binding.Close()
    EndSubEndClass
                          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 AsString = "StoreandFowardMessageHelloWorld"Dim serverAddress AsString = "ServerMailAddress@fabrikam.com"Dim serverPWD AsString = "MyPassword"Dim clientAddress AsString = "DeviceMailAddress@fabrikam.com"Dim exchangeServerLocation AsString = "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 AsNew 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 AsNew CFMessagingSerializer(GetType(String))

        Dim str AsString = ""
        str = reply.GetBody(OfString)(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()
    EndSubEndClass
                          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();
}

コードのコンパイル

デバイス上で実行する例を示しますが、次の名前空間への参照を必要です。

デスクトップで実行されるサンプルは、次の名前空間への参照を要求します。

セキュリティ

メール トランスポートのセキュリティは、この例では無効です。 詳細については、「WCF Exchange Server のメール トランスポート」を参照してください。

参照

処理手順

方法 : WCF Exchange Server のメール トランスポートでメッセージ セキュリティを使用します。

その他の技術情報

Windows 通信基盤 (WCF) の開発と、.NET Framework を最適化します。