This documentation is archived and is not being maintained.

MailSecurity Class

Visual Studio 2008

Contains methods for configuring channel security options for the mail binding.

Namespace:  Microsoft.ServiceModel.Channels.Mail
Assembly:  Microsoft.ServiceModel.Channels.Mail (in Microsoft.ServiceModel.Channels.Mail.dll)

public sealed class MailSecurity

This class is used for classes that are derived from the MailBindingBase object. Use the Security property to return the MailSecurity object.

NoteNote:

This class is included with the .NET Compact Framework version 3.5, but it requires either the .NET Compact Framework 3.5 or the .NET Framework version 3.0 or later at run time.

The following example shows how to send a message with message security enabled for the mail binding.

The custom serializer is not specific to the Exchange Server mail transport and is not included in this example.

class Program
{
    private static string ChannelName = "Channel1";
    private static string ServiceEmailAddress = "service@fabrikam.com";

    private static CFMessagingSerializer serializer = new CFMessagingSerializer(typeof(string));

    static void Main(string[] args)
    {
        IChannelFactory<IOutputChannel> factory;
        IOutputChannel output;
        BindingParameterCollection bpc;
        Message message;
        ExchangeWebServiceMailBinding binding;
        Uri clientEmailServer = new Uri("http://mail.example.com");
        string clientEmailAddress = "client@example.com";
        string password = "password";

        binding = new ExchangeWebServiceMailBinding(clientEmailServer,
                                                   new NetworkCredential(clientEmailAddress, password));
        binding.Security.Mode = MailSecurityMode.Message;
        bpc = new BindingParameterCollection();

        ClientCredentials cc = new ClientCredentials();

        cc.ClientCertificate.SetCertificate(
              System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,
        System.Security.Cryptography.X509Certificates.StoreName.My,
        System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,
              "example.com");
        cc.ServiceCertificate.Authentication.CertificateValidationMode =
            System.ServiceModel.Security.X509CertificateValidationMode.ChainTrust;
        cc.ServiceCertificate.SetDefaultCertificate(System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,
        System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,
              "fabrikam.com");

        ChannelProtectionRequirements cpr = new ChannelProtectionRequirements();
        // Sign and encrypt body for *all* messages
        cpr.OutgoingSignatureParts.AddParts(new MessagePartSpecification(true), "*");
        cpr.IncomingSignatureParts.AddParts(new MessagePartSpecification(true), "*");
        cpr.OutgoingEncryptionParts.AddParts(new MessagePartSpecification(true), "*");
        cpr.IncomingEncryptionParts.AddParts(new MessagePartSpecification(true), "*");

        bpc.Add(cc);
        bpc.Add(cpr);

        factory = binding.BuildChannelFactory<IOutputChannel>(bpc);

        factory.Open();

        EndpointAddress sendAddress = new EndpointAddress(MailUriHelper.CreateUri(ChannelName,
            ServiceEmailAddress), EndpointIdentity.CreateX509CertificateIdentity(cc.ServiceCertificate.DefaultCertificate));
        output = factory.CreateChannel(sendAddress);

        message = Message.CreateMessage(MessageVersion.Default, "urn:Test", "Hello, World!", serializer);
        output.Open();
        output.Send(message);

        output.Close();
        factory.Close();

        binding.Close();


        // Receiving message on the server side 
        //

        IChannelListener<IInputChannel> listener;
        IInputChannel input;
        Uri serviceEmailServer = new Uri("http://mail.fabrikam.com");

        binding = new ExchangeWebServiceMailBinding(serviceEmailServer,
                                                   new NetworkCredential(ServiceEmailAddress, password),
                                                   MailSecurityMode.Message);

        // Create credential for the listening side
        ServiceCredentials sc = new ServiceCredentials();
        sc.ServiceCertificate.SetCertificate(
            System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,
            System.Security.Cryptography.X509Certificates.StoreName.My,
            System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName,
            "fabrikam.com");
        sc.ClientCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;

        bpc = new BindingParameterCollection();
        bpc.Add(sc);
        bpc.Add(cpr);

        listener = binding.BuildChannelListener<IInputChannel>(MailUriHelper.CreateUri(ChannelName, ""), bpc);
        listener.Open();

        input = listener.AcceptChannel();
        input.Open();

        message = input.Receive();
        string data = message.GetBody<string>(serializer);

        input.Close();
        listener.Close();

        binding.Close();

    }
}

System.Object
  Microsoft.ServiceModel.Channels.Mail.MailSecurity

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Compact Framework

Supported in: 3.5
Show: