I need some help using Exchange EWS with C#
-
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
-
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
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 -
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 KreskowiakI will try that tomorrow. Is it as simple as just setting it to true?
-
I will try that tomorrow. Is it as simple as just setting it to true?
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 -
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 KreskowiakIt 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.
-
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.
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!