Azure Mail sent issue

S, Krishnamurthy 20 Reputation points
2025-04-06T19:07:29.5733333+00:00

I am currently developing an application that needs to send emails using the Microsoft Graph API without requiring user interaction. My objective is to authenticate using the client secret and obtain a new access token each time, ensuring that no user sign-in or consent is necessary. I intend to utilize application permissions with the client credentials flow to achieve this functionality and i have only deleget permisison

User's image

Microsoft Security | Microsoft Entra | Microsoft Entra ID

1 answer

Sort by: Most helpful
  1. Anonymous
    2025-04-09T09:29:55.7866667+00:00

    Hello S, Krishnamurthy,

    I understand you want to send mails on behalf of users that is, use client credential flow, but you have only delegated API permissions granted to the Microsoft Entra ID application.

    To send emails using Microsoft Graph API with the client credentials flow (without user interaction), you need application permissions rather than delegated permissions. Since you are making use of delegated permissions you are getting 403 error.

    • Application permissions are used for scenarios where the application acts as itself (i.e., not on behalf of a user).
    • For sending emails without user interaction, you need to use the Mail.Send application permission.
    • There is no way to use delegated API permission and send mail via client credential flow.
    • If you want to use delegated API permission then you have to make use of any user interactive flow only.

    Delegated permissions require a signed-in user and allow the app to act on behalf of that user. To send emails without user interaction, you need application permissions (like Mail.Send), which allow the app to act independently of a user and perform actions across the organization.

    Initially I granted delegated API permission to send mail using client credential flow and got the same error:

    enter image description here

    To resolve the error, either you need to switch to any user interactive flow, or you need to grant Mail.Send application permission:

    User's image

    I am able to send mail successfully:

    
    tenant_id = 'TenantID'
    
    client_id = 'ClientID'
    
    client_secret = 'Secret'
    
    scope = 'https://graph.microsoft.com/.default'
    
    grant_type = 'client_credentials'
    
    token_url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
    
    def get_access_token():
    
        headers = {
    
            'Content-Type': 'application/x-www-form-urlencoded'
    
        }
    
        payload = {
    
            'client_id': client_id,
    
            'client_secret': client_secret,
    
            'scope': scope,
    
            'grant_type': grant_type
    
        }
    
        response = requests.post(token_url, headers=headers, data=payload)
    
        if response.status_code == 200:
    
            token = response.json().get('access_token')
    
            return token
    
        else:
    
            print(f"Error fetching access token: {response.text}")
    
            return None
    
    def send_email(access_token, recipient, subject, body):
    
        send_mail_url = 'https://graph.microsoft.com/v1.0/users/UserID/sendMail'
    
        send_mail_headers = {
    
            'Authorization': f'Bearer {access_token}',
    
            'Content-Type': 'application/json'
    
        }
    
        email_data = {
    
            "message": {
    
                "subject": subject,
    
                "body": {
    
                    "contentType": "Text",
    
                    "content": body
    
                },
    
                "toRecipients": [
    
                    {
    
                        "emailAddress": {
    
                            "address": recipient
    
                        }
    
                    }
    
                ]
    
            }
    
        }
    
        response = requests.post(send_mail_url, headers=send_mail_headers, json=email_data)
    
        
    
        if response.status_code == 202:
    
            print('Email sent successfully!')
    
        else:
    
            print(f"Error sending email: {response.status_code}, {response.text}")
    
    if __name__ == "__main__":
    
        access_token = get_access_token()
    
        
    
        if access_token:
    
            recipient = "******@xxx.onmicrosoft.com"
    
            subject = "Test Email"
    
            body = "This is a test email sent using Microsoft Graph API"
    
            
    
            send_email(access_token, recipient, subject, body)
    
    

    enter image description here

    Reference:

    user: sendMail - Microsoft Graph v1.0 | Microsoft

    Hope this helps!


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can be beneficial to other community members.

    User's image

    If you have any other questions or still running into more issues, let me know in the "comments" and I would be happy to help you.

    Was this answer helpful?

    1 person found this answer helpful.

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.