Share via

Cannot send emails using MS Graph in .NET Framework application

Anonymous
2023-08-11T11:06:00.99+00:00

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.)

Microsoft Security | Microsoft Graph
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-08-11T15:52:50.1266667+00:00

    Hello BJM dot Infra,

    Thanks for reaching out. For Graph SDK related queries please post them on the Graph Github DotNet SDK forum or the Graph Feedback Forum.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    Thanks.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.