Fix Microsoft 365 Email Sending Error 401 Unauthorized in .NET
Question details
A .NET application encounters a 401 Unauthorized error when attempting to send emails through Microsoft 365.

- Product
- Microsoft 365
- Device & OS
- not provided
- Scenario
- Sending emails programmatically from a .NET or C# application using Microsoft 365 credentials.
- Observed behavior
- The application throws a 401 Unauthorized error during mail submission because basic authentication has been deprecated in favor of Modern Authentication.
Ensure you have administrative access to your Microsoft Entra ID (formerly Azure AD) tenant to register your .NET application and configure the required API permissions.
Migrate to OAuth 2.0 Modern Authentication
Replace the deprecated basic authentication in your .NET application with OAuth 2.0 using the Microsoft identity platform.
Microsoft 365 has permanently disabled basic authentication for Exchange Online to improve security. Applications using standard SMTP with a username and password will fail with a 401 Unauthorized error.
To restore email functionality, you must transition to Modern Authentication (OAuth 2.0) by implementing the Microsoft Authentication Library (MSAL) and using Microsoft Graph API or OAuth-based SMTP submission.
Log in to the Microsoft Entra admin center, navigate to App registrations, and create a new registration for your .NET application.
Go to the API permissions section of your registered app, click 'Add a permission', select Microsoft Graph, and add the 'Mail.Send' permission (either Delegated or Application depending on your architecture).
Navigate to 'Certificates & secrets', create a new client secret, and securely store the value in your application's configuration file.
Install the Microsoft.Identity.Client NuGet package and update your codebase to acquire access tokens using your Tenant ID, Client ID, and Client Secret.
Replace your basic SMTP client code with HTTP POST requests to the Microsoft Graph API endpoint (https://graph.microsoft.com/v1.0/users/{user-id}/sendMail) passing the acquired OAuth token in the Authorization header.

Looking for a Lightweight Alternative to Microsoft Office?
While resolving backend development and authentication errors in Microsoft 365, you might also be looking for a streamlined, hassle-free office suite for your daily document needs. WPS Office offers a free, lightweight, and highly compatible alternative to Microsoft Office.
- 1. Download WPS Office: Visit the official WPS website and click the free download button to get the installer.
- 2. Install the Software: Run the setup file and follow the simple on-screen instructions to install the suite on your computer.
- 3. Open Your Documents: Launch WPS Office and instantly open your existing .docx, .xlsx, or .pptx files with perfect formatting retention.

Frequently Asked Questions
Why did basic authentication suddenly stop working in Microsoft 365?
Microsoft systematically deprecated and disabled basic authentication for Exchange Online to protect users from credential-based attacks. All client applications and scripts are now required to use Modern Authentication (OAuth 2.0).
Can I temporarily re-enable basic authentication to fix the 401 error?
No, Microsoft has permanently turned off basic authentication for Exchange Online protocols. Re-enabling it is no longer supported, meaning applications must be updated to use OAuth 2.0.
Where can I find .NET code examples for Microsoft Graph API?
Official code samples, SDK documentation, and developer support for implementing Microsoft Graph in .NET can be found on the Microsoft Learn platform and the Microsoft Q&A developer forums.




