Send email in background using modern authentication from personal account

Anonymous
2023-07-26T10:09:25.8666667+00:00

Hi
I'd like to send background email from my personal account e.g. 'outlook.com' using modern authentication in C# but I get this error : 'The remote server returned an error: (401) . '.

I need even send background email from my Gmail account.

Could you please help me and send me a sample code for it ?

Thanks

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,742 questions
{count} votes

1 answer

Sort by: Most helpful
  1. James Hamil 24,661 Reputation points Microsoft Employee
    2023-08-02T19:15:41.1166667+00:00

    Hi @Anonymous ,you don't necessarily need to create an Office 365 or Microsoft 365 account for organizations to send emails with modern authentication. Azure Communication Services (ACS) allows you to send emails using Azure Communication Resource. You can configure email with two types of domains: Azure Managed Domains and Custom Domains.

    Azure Managed Domains provide a one-click setup, and you can send emails using mail from domains like donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net Custom Domains allow you to add a domain that you already own, verify the ownership, and configure the required authentication support.

    To register users in Azure Active Directory, you can use the Microsoft Graph API or other partner-driven connectors. For C#, you can use the Microsoft Graph SDK to interact with the API and register users. Here's an example of how to create a user using the Microsoft Graph SDK in C#:

    using Microsoft.Graph;
    using Microsoft.Identity.Client;
    
    // ...
    
    public async Task CreateUserAsync()
    {
        var confidentialClientApplication = ConfidentialClientApplicationBuilder
            .Create("<client-id>")
            .WithTenantId("<tenant-id>")
            .WithClientSecret("<client-secret>")
            .Build();
    
        var authProvider = new ClientCredentialProvider(confidentialClientApplication);
        var graphClient = new GraphServiceClient(authProvider);
    
        var user = new User
        {
            AccountEnabled = true,
            DisplayName = "John Doe",
            MailNickname = "johndoe",
            UserPrincipalName = "johndoe@yourdomain.com",
            PasswordProfile = new PasswordProfile
            {
                ForceChangePasswordNextSignIn = true,
                Password = "your-password"
            }
        };
    
        await graphClient.Users
            .Request()
            .AddAsync(user);
    

    Replace <client-id>, <tenant-id>, <client-secret>, and other placeholders with your actual values. This example assumes you have the necessary permissions configured for your application in Azure AD.

    Please let me know if you have any questions and I can help you further.

    If this answer helps you please mark "Accept Answer" so other users can reference it.

    Thank you,

    James

    1 person found this answer helpful.

Your answer

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