Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I need some help using Exchange EWS with C#

I need some help using Exchange EWS with C#

Scheduled Pinned Locked Moved C#
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    With this code I keep receiving the error message saying : "

    The request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

    " Does anyone have any ideas on how I can get my C# program to send emails via EWS in my Exchange environment? Thanks

    using System.Net;
    using System.Net.Mail;
    using Microsoft.Exchange.WebServices;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;

        private void emailNotifyUser ()
        {
    

    Microsoft.Exchange.WebServices.Data.ExchangeService service = new Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2010_SP1);
    service.Credentials =
    new Microsoft.Exchange.WebServices.Data.WebCredentials("you@you.com", "password1", "domain.com);
    service.TraceEnabled = true;
    service.TraceFlags = Microsoft.Exchange.WebServices.Data.TraceFlags.All;
    service.Url = new Uri("HTTPS://xxxxxxx/EWS/Exchange.asmx"); // https://xxxxxxx.xxx/autodiscover/autodiscover.xml
    //service.AutodiscoverUrl(To,
    // RedirectionUrlValidationCallback);
    //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback
    //(
    // delegate { return true; }
    //);
    // trust sender
    //System.Net.ServicePointManager.ServerCertificateValidationCallback
    // = ((sender, cert, chain, errors) => cert.Subject.Contains("nsc-cas-01"));
    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);
    Microsoft.Exchange.WebServices.Data.EmailMessage email = new Microsoft.Exchange.WebServices.Data.EmailMessage(service);
    email.ToRecipients.Add(To);
    email.Subject = "HelloWorld";
    email.Body = new Microsoft.Exchange.WebServices.Data.MessageBody("First email using EWS Managed API");
    email.Body.BodyType = Microsoft.Exchange.WebServices.Data.BodyType.HTML;
    email.Send();
    }

    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors poli

    D 1 Reply Last reply
    0
    • T turbosupramk3

      With this code I keep receiving the error message saying : "

      The request failed. The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

      " Does anyone have any ideas on how I can get my C# program to send emails via EWS in my Exchange environment? Thanks

      using System.Net;
      using System.Net.Mail;
      using Microsoft.Exchange.WebServices;
      using System.Security.Cryptography;
      using System.Security.Cryptography.X509Certificates;

          private void emailNotifyUser ()
          {
      

      Microsoft.Exchange.WebServices.Data.ExchangeService service = new Microsoft.Exchange.WebServices.Data.ExchangeService(Microsoft.Exchange.WebServices.Data.ExchangeVersion.Exchange2010_SP1);
      service.Credentials =
      new Microsoft.Exchange.WebServices.Data.WebCredentials("you@you.com", "password1", "domain.com);
      service.TraceEnabled = true;
      service.TraceFlags = Microsoft.Exchange.WebServices.Data.TraceFlags.All;
      service.Url = new Uri("HTTPS://xxxxxxx/EWS/Exchange.asmx"); // https://xxxxxxx.xxx/autodiscover/autodiscover.xml
      //service.AutodiscoverUrl(To,
      // RedirectionUrlValidationCallback);
      //ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback
      //(
      // delegate { return true; }
      //);
      // trust sender
      //System.Net.ServicePointManager.ServerCertificateValidationCallback
      // = ((sender, cert, chain, errors) => cert.Subject.Contains("nsc-cas-01"));
      ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);
      Microsoft.Exchange.WebServices.Data.EmailMessage email = new Microsoft.Exchange.WebServices.Data.EmailMessage(service);
      email.ToRecipients.Add(To);
      email.Subject = "HelloWorld";
      email.Body = new Microsoft.Exchange.WebServices.Data.MessageBody("First email using EWS Managed API");
      email.Body.BodyType = Microsoft.Exchange.WebServices.Data.BodyType.HTML;
      email.Send();
      }

      private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors poli

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Does the code work if you just return true from the ValidateRemoteCertificate method no matter what?

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      T 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Does the code work if you just return true from the ValidateRemoteCertificate method no matter what?

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        T Offline
        T Offline
        turbosupramk3
        wrote on last edited by
        #3

        I will try that tomorrow. Is it as simple as just setting it to true?

        D 1 Reply Last reply
        0
        • T turbosupramk3

          I will try that tomorrow. Is it as simple as just setting it to true?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          I have no idea. This is just a test to narrow down the problem.

          A guide to posting questions on CodeProject

          Click this: Asking questions is a skill. Seriously, do it.
          Dave Kreskowiak

          T 1 Reply Last reply
          0
          • D Dave Kreskowiak

            I have no idea. This is just a test to narrow down the problem.

            A guide to posting questions on CodeProject

            Click this: Asking questions is a skill. Seriously, do it.
            Dave Kreskowiak

            T Offline
            T Offline
            turbosupramk3
            wrote on last edited by
            #5

            It does not work, I received the following error

            An element node 'soap:Envelope' of the type Element was expected, but node 'Autodiscover' of type Element was found.

            T 1 Reply Last reply
            0
            • T turbosupramk3

              It does not work, I received the following error

              An element node 'soap:Envelope' of the type Element was expected, but node 'Autodiscover' of type Element was found.

              T Offline
              T Offline
              turbosupramk3
              wrote on last edited by
              #6

              I double checked my autodiscover url with the following code and then matching that fixed it

              Get-WebServicesVirtualDirectory |fl identity,internalurl,externalurl

              Thanks for helping me to get that error and point me in the right direction!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups