다음을 통해 공유


연습: WCF Exchange Server 메일 전송 사용

업데이트: 2007년 11월

WCF(Windows Communication Foundation) 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 AUTD(Always-Up-To-Date) 기능이 포함되어 있습니다. 자세한 내용은 Windows Mobile 웹 사이트을 참조하십시오.

  2. Microsoft Exchange Server 2007을 실행하는 전자 메일 서버를 네트워크에서 사용할 수 있는지 확인합니다.

    Exchange Server 2007이 없는 경우에는 가상 PC에서 실행되는 평가판을 사용할 수 있습니다. 자세한 내용은 Exchange Server 웹 사이트을 참조하십시오.

  3. 컴퓨터에서 OWA(Outlook Web Access)를 실행하고 전자 메일 계정에 연결하여 새 Exchange 전자 메일 서버에 연결할 수 있는지 확인합니다.

  4. Exchange 전자 메일 서버에서 Exchange Web Services를 사용할 수 있는지 확인합니다.

    이를 확인하는 방법 중 하나는 http://server-address/ews/exchange.asmx 또는 https://server-address/ews/exchange.asmx의 두 URL 중 하나를 사용하여 전자 메일 서버의 exchange.asmx 페이지에 액세스하는 것입니다.

  5. 에뮬레이터를 사용하는 경우에는 에뮬레이터에 네트워크 기능을 제공합니다.

    참고:

    ActiveSync에서 네트워크 연결을 제공할 때는 ActiveSync AUTD 기능이 실행되지 않습니다. Windows Mobile 6 Professional SDK에 포함되어 있으며 별도로 다운로드할 수도 있는 Device Emulator 2.0을 통해 에뮬레이터 속성을 구성하여 NE2000 PCMCIA 카드를 호스트 네트워크 어댑터에 바인딩할 수 있습니다.

  6. 에뮬레이터가 실행 중인 경우 ActiveSync가 Exchange 전자 메일 서버와 통신하도록 구성합니다. 자세한 내용은 Microsoft TechNet 웹 사이트의 Deploying Windows Mobile 6 Powered Devices with Microsoft Exchange Server 2007에서 "Step 5: Configure and Manage Mobile Device Access on the Exchange Server"를 참조하십시오.

장치용 응용 프로그램 만들기

다음 절차에서는 클라이언트를 나타내는 장치에 대해 응용 프로그램을 빌드한 다음 메일 전송을 사용하여 메시지를 서버로 보냅니다.

장치용 응용 프로그램을 만들려면

  1. Visual Studio에서 새 스마트 장치 프로젝트를 만듭니다.

  2. 프로젝트에 다음 참조를 추가합니다.

    • Microsoft.ServiceModel.Mail.dll

    • Microsoft.ServiceModel.Mail.WindowsMobile.dll

    • System.ServiceModel.dll

    • System.Runtime.Serialization.dll

  3. 방법: WCF 응용 프로그램의 메시지 serialize에 설명되어 있는 CFMessagingSerializer 클래스를 추가합니다.

    데스크톱에서는 사용자 지정 serializer 또는 특성을 사용하여 serialize된 데이터를 만들 수 있습니다. 그러나 장치와 데스크톱에는 같은 serializer를 사용하는 것이 좋습니다.

  4. 메시지를 작성합니다.

    Dim str As String = "Hello"
    
    String str = "Hello";
    
  5. 메시지를 만들어 serialize합니다.

    ' 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. 서버에서 응답을 받으면 응답을 deserialize하고 결과를 사용자에게 표시합니다.

    ' 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. 방법: WCF 응용 프로그램의 메시지 serialize에 설명되어 있는 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 메일 전송을 참조하십시오.

참고 항목

작업

방법: 메시지 보안을 WCF Exchange Server 메일 전송에 사용

기타 리소스

WCF(Windows Communication Foundation) 개발 및 .NET Compact Framework