逐步解說:使用 WCF Exchange Server Mail Transport

更新:2007 年 11 月

您可以建立 Windows Communication Foundation (WCF) Exchange Server 郵件傳輸為基礎的應用程式,使用執行 Microsoft Exchange Server 的電腦做為媒介傳送和接收所指定輸入/輸出通道上的訊息。桌面和裝置上都支援以 WCF Exchange Server 郵件傳輸為基礎的應用程式。

本逐步解說將介紹 WCF Exchange Server 郵件傳輸的功能。其中將說明下列工作:

  • 設定您的電腦。

  • 建立裝置 (用戶端) 的應用程式。

  • 建立桌面 (伺服器) 的應用程式。

如需這些程序所示範的程式碼完整清單,請參閱本逐步解說最後的範例一節。

注意事項:

請勿在實際執行程式碼中使用這個範例。

必要條件

這個範例需要 .NET Compact Framework 3.5 版。

設定您的電腦

在這個程序中,務必確定正確設定您的開發環境以執行範例。

設定您的電腦以執行範例

  1. 安裝 Windows Mobile 5.0 版或 Windows Mobile 6 軟體開發套件 (SDK)。

    Windows Mobile 6 Professional SDK 包含 ActiveSync Always-Up-To-Date (AUTD) 功能,裝置上的郵件傳輸會使用該功能將 [收件匣] 保持在最新狀態。如需詳細資訊,請參閱 Windows Mobile 網站

  2. 確認網路上執行 Microsoft Exchange Server 2007 的電子郵件伺服器能夠使用。

    如果您無法使用 Exchange Server 2007,可以使用 Virtual PC 執行的試用版。如需詳細資訊,請參閱 Exchange Server 網站

  3. 從您的電腦執行 Outlook Web Access (OWA) 並連接到電子郵件帳戶,確認您的電腦能夠連接到新的 Exchange 電子郵件伺服器。

  4. 確認 Exchange 電子郵件伺服器上的 Exchange Web 服務已啟用。

    確認的其中一種方式是使用下列其中一個 URL 存取電子郵件伺服器上的 exchange.asmx 網頁:http://server-address/ews/exchange.asmx 或 https://server-address/ews/exchange.asmx。

  5. 如果您使用模擬器,請提供網路功能給模擬器。

    注意事項:

    ActiveSync 正在提供網路連線時,ActiveSync AUTD 功能不會執行。您可以使用 Windows Mobile 6 Professional SDK 隨附的 Device Emulator 2.0 或是另外下載,藉由設定模擬器屬性將您的 NE2000 PCMCIA 卡繫結至主機網路卡。

  6. 當模擬器開始執行時,設定 ActiveSync 與您的 Exchange 電子郵件伺服器進行通訊。如需詳細資訊,請參閱 Microsoft TechNet 網站上,使用 Microsoft Exchange Server 2007 部署 Windows Mobile 6 支援的裝置 (英文) 的「步驟 5:設定及管理 Exchange Server 上的行動裝置存取」。

建立裝置的應用程式

在這個程序中,您將建置代表用戶端之裝置的應用程式,然後使用郵件傳輸建立並傳送訊息至伺服器。

建立裝置的應用程式

  1. 在 Visual Studio 中建立新的智慧型裝置專案。

  2. 將下列參考加入至專案:

    • Microsoft.ServiceModel.Mail.dll

    • Microsoft.ServiceModel.Mail.WindowsMobile.dll

    • System.ServiceModel.dll

    • System.Runtime.Serialization.dll

  3. 加入 HOW TO:序列化 WCF 應用程式中的訊息 中所述的CFMessagingSerializer 類別。

    在桌面上,您可以使用自訂序列化程式或屬性來建立序列化資料。不過,建議您在裝置和桌面上使用同一個序列化程式。

  4. 建置訊息。

    Dim str As String = "Hello"
    
    String str = "Hello";
    
  5. 建立訊息並加以序列化。

    ' 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);
    
  6. 建立變數,代表通道名稱、裝置電子郵件地址及伺服器電子郵件地址。

    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";
    
  7. 建置輸出通道。

    ' 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();
    
  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(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");
    
  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. 加入 HOW TO:序列化 WCF 應用程式中的訊息 中所述的CFMessagingSerializer 類別。

  4. 建立一些變數,其中部分必須對應來自裝置專案的值。

    ' 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";
    
  5. 建立接聽程式。

    如果您使用 Windows 認證,請將 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);
    
  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 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();
}

編譯程式碼

裝置上執行的範例需要參考下列命名空間:

桌面上執行的範例需要參考下列命名空間:

安全性

這個範例中的郵件傳輸安全性並未啟用。如需詳細資訊,請參閱 WCF Exchange Server Mail Transport

請參閱

工作

HOW TO:搭配 WCF Exchange Server Mail Transport 使用訊息安全性

其他資源

Windows Communication Foundation (WCF) 開發和 .NET Compact Framework