The following code works fine on windows but on debian is returning a 401 - anonymous request disallowed. It is using DotNet core 2.0.0. If that helps. I think it may be because the machine on windows is somehow discovering the domain while the debian machine is not. I'm not sure. Any help is appreciated.
using System;
using Microsoft.Exchange.WebServices.Data;
namespace Test
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService();
service.EnableScpLookup = false;
service.TraceEnabled = true;
service.UseDefaultCredentials = false;
service.PreAuthenticate = false;
service.TraceFlags = TraceFlags.All;
try
{
service.Url = new Uri("https:/domain/ews/Exchange.asmx");
}
catch (Exception ex) {
throw new Exception(string.Format("webService Uri:" + ex));
}
try
{
service.Credentials = new WebCredentials("username", "pass","domain");
}
catch (Exception ex) {
throw new Exception(string.Format("Credentials:" + ex));
}
try
{
EmailMessage email = new EmailMessage(service);
email.ToRecipients.Add("user@domain.com");
email.Subject = "HelloWorld";
email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
email.Save();
email.Send();
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
}
Edit 1: If it helps I can put the trace of the soap and the exception with email ommited:
<trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2018-07-20 09:31:08Z">
POST /ews/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: ExchangeServicesClient/15.00.0913.015
Accept-Encoding: gzip,deflate
</Trace>
<Trace Tag="EwsRequest" Tid="1" Time="2018-07-20 09:31:09Z"
Version="15.00.0913.015">
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:CreateItem MessageDisposition="SaveOnly">
<m:Items>
<t:Message>
<t:Subject>HelloWorld</t:Subject>
<t:Body BodyType="HTML">This is the first email I've sent by using the EWS Managed API</t:Body>
<t:ToRecipients>
<t:Mailbox>
<t:EmailAddress>**********@******.com</t:EmailAddress>
</t:Mailbox>
</t:ToRecipients>
</t:Message>
</m:Items>
</m:CreateItem>
</soap:Body>
</soap:Envelope>
</Trace>
<Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2018-07-20 09:31:12Z">
HTTP/1.1 401 Anonymous Request Disallowed
Server: Microsoft-IIS/8.5
request-id: 13c632cd-7642-4e26-ad5d-48dce4b52d35
WWW-Authenticate: Negotiate, NTLM
X-Powered-By: ASP.NET
X-FEServer: FTLPEX02CAS02
Date: Fri, 20 Jul 2018 09:31:11 GMT
Content-Length: 0
</Trace>