classProgram
{
staticvoidMain(string[] args)
{
//note that there no option for exchange server 2016 (my exchange online use exchange server 2016), so i use the default option
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new WebCredentials("user@domain.com", "myPassword");
service.UseDefaultCredentials = false;
//for log purpose
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("user@domain.com", RedirectionUrlValidationCallback);
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("user2@domain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API.");
email.Send();
}
privatestaticboolRedirectionUrlValidationCallback(string redirectionUrl)
{
// The default for the validation callback is to reject the URL.bool result = false;
Uri redirectionUri = new Uri(redirectionUrl);
// Validate the contents of the redirection URL. In this simple validation// callback, the redirection URL is considered valid if it is using HTTPS// to encrypt the authentication credentials. if (redirectionUri.Scheme == "https")
{
result = true;
}
return result;
}
}
AutoDiscoveryURL中有一个例外,如下所示:
Microsoft.Exchange.WebServices.Data.ServiceXmlDeserializationException: 'The
expected XML node type was XmlDeclaration, but the actual type is Element.'