azureazure-active-directorymicrosoft-graph-apioutlook-restapi

Not Able to Send Email on behalf of user using Microsoft Graph API ( Tenant GUID Not Found Error)


We are creating an app to provide email services. The user can schedule the email sending as well.

For proof of concept using Microsoft Graph API on Java we created an app on Microsoft Azure, set up the permissions on App and added the users. I am writing JAVA POC for this using msalj and Microsoft Graph API.

IAuthenticationProvider iAuthProvider = new TokenCredentialAuthProvider(clientSecretCredential());
        
GraphServiceClient<Request> graphClient = GraphServiceClient
                .<Request>builder()
                .authenticationProvider(iAuthProvider)
                .buildClient();

//Creating msg body for Email
Message message = new Message();
graphClient.users(senderUserEmail).sendMail(UserSendMailParameterSet
        .newBuilder()
        .withMessage(message)
        .withSaveToSentItems(saveToSentItems)
        .build()).buildRequest()
    .post();

I am using clientCredentialAuth for getting token. I do have all the values ClientID, ClientSecret, TenantId. Method clientSecretCredential() is defined as below.

    private ClientSecretCredential clientSecretCredential() {

        return new ClientSecretCredentialBuilder()
          .clientId(clientId)
          .tenantId(tenantId)
          .clientSecret(clientSecret)
          .build();
        }

I get the error

{
  "error": {
    "code": "OrganizationFromTenantGuidNotFound",
    "message": "The tenant for tenant guid {tenantId} does not exist.",
    "innerError": {
      "errorUrl": "https://aka.ms/autherrors#error-InvalidTenant",
      "date": "2024-02-04T11:18:24"
    }
  }
}

Update: So what @Rukmini is suggesting that is what we are doing but at Java side. It seems it is not working because we don't have license of Office 365. I am working on getting it to try again.


Solution

  • To send mail on behalf of the user, check the below:

    Create a Microsoft Entra ID application and grant API permissions:

    enter image description here

    I generated access token via Client Credential flow via Postman:

    https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
    
    client_id:ClientID
    client_secret:ClientSecret
    grant_type:client_credentials
    scope:https://graph.microsoft.com/.default
    

    enter image description here

    Using the above access token, I am able to send the mail successfully:

    POST https://graph.microsoft.com/v1.0/users/UserID/sendMail
    Content-type: application/json
    
    {
      "message": {
        "subject": "Meet for lunch?",
        "body": {
          "contentType": "Text",
          "content": "The new cafeteria is open."
        },
        "toRecipients": [
          {
            "emailAddress": {
              "address": "frannis@contoso.onmicrosoft.com"
            }
          }
        ],
        "ccRecipients": [
          {
            "emailAddress": {
              "address": "danas@contoso.onmicrosoft.com"
            }
          }
        ]
      },
      "saveToSentItems": "false"
    }
    

    enter image description here

    The error "OrganizationFromTenantGuidNotFound" usually occurs if the user doesn't have assigned Office 365 license. Make sure to subscribe Office 365 license to the tenant.

    enter image description here

    If still the issue persists, then check the below: