I am maintaining an old .NET Framework application (v4.7.2). It currently uses an in-house solution to send emails and read a mailbox on an Exchange Server, but this solution is being decommissioned and all applications are supposed to move to using Microsoft Graph.
I have used MS Graph (to send emails) in other applications that are written in .NET Core, and it works in all of those. However, I cannot get it to work in this .NET Framework one.
In order to test the code without having to deploy it to the server and then test it, I decided to create a console application with basically the same code as the application. That way I could easily test it, try alternate code in case it doesn't work etc. I have not managed to get this to work, I encounter the same error each time:
Unfortunately, the errors offer little explanation:
MsGraphMailService: ServiceException while sending mail: StatusCode = [0]
MsGraphMailService: ServiceException while sending mail: Error = [Code: generalException
Message: An error occurred sending the request.
]
MsGraphMailService: ServiceException while sending mail: RawResponseBody = []
The only somewhat useful lines in the logs are these ones:
---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: Error 12007 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'The server name or address could not be resolved'.
[...]
at MsGraphTester.MsGraphMailService.d__1.MoveNext() in MsGraphTester\MsGraphTester\MsGraphMailService.cs:line 46
I then decided to create a .NET (Core) 6 console application with virtually the same code (except all the .NET Framework specific code was adapted to work in .NET Core). And that one works.
The authentication succeeds in both versions, I have inspected the token that I get and it contains the correct information.
You can find both projects on Github. Please read the README file in each project, because for obvious reasons I have not uploaded the TenantId etc., which means you need to create the relevant app.config or appsettings.json file yourself; the README files contain examples.
(Honestly, I'm starting to suspect the MS Graph NuGet package (4.54.0) does not work correctly when used in .NET Framework.)