To develop an automated notification system that sends PDF reports via email to employees using Microsoft Entra ID (formerly Azure AD) and Microsoft Graph API, you need to configure the app registration, grant necessary API permissions, and implement the logic to send emails with attachments. Below is a detailed step-by-step guide:
Step 1: Register the Application in Microsoft Entra ID
- Log in to the Azure Portal:
- Go to the Azure Portal.
- Register the Application:
- Navigate to Microsoft Entra ID > App registrations > New registration.
- Provide a name for your app (e.g.,
NotificationSystem
). - Select the appropriate Supported account types (e.g., "Accounts in this organizational directory only").
- Leave the Redirect URI blank for now (unless your app requires a callback URL).
- Click Register.
- Note the Application (Client) ID and Tenant ID:
- After registration, note the Application (Client) ID and Directory (Tenant) ID from the app’s overview page. These will be used in your code.
- Generate a Client Secret:
- Go to Certificates & secrets > New client secret.
- Provide a description and set an expiration period.
- Click Add and copy the client secret value (you won’t be able to retrieve it later).
Step 2: Configure API Permissions
- Add Microsoft Graph API Permissions:
- Go to API permissions > Add a permission > Microsoft Graph.
- Select Application permissions (since this is an automated system without user interaction).
- Add the following permissions:
-
Mail.Send
(to send emails). -
Mail.ReadWrite
(to manage emails). -
User.Read.All
(to read employee details).
-
- Click Add permissions.
- Grant Admin Consent:
- Click Grant admin consent for [Your Organization] to approve the permissions.
Step 3: Authenticate the Application
To authenticate the app, use the Client Credentials Flow since this is an automated system.
- Acquire an Access Token:
- Use the following details to request an access token from Microsoft Entra ID:
- Token Endpoint:
https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token
- Request Body:
grant_type=client_credentials &client_id={client-id} &client_secret={client-secret} &scope=https://graph.microsoft.com/.default
- Token Endpoint:
- Example using
curl
:curl -X POST -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=client_credentials&client_id={client-id}&client_secret={client-secret}&scope=https://graph.microsoft.com/.default" \ "https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token"
- The response will include an
access_token
that you can use to call Microsoft Graph API.
- Use the following details to request an access token from Microsoft Entra ID:
Step 4: Send Emails with Attachments Using Microsoft Graph API
- Prepare the Email Payload:
- Construct the email payload with the PDF attachment. Use the
base64
encoded content of the PDF file.
{ "message": { "subject": "Monthly Report", "body": { "contentType": "Text", "content": "Please find the attached monthly report." }, "toRecipients": [ { "emailAddress": { "address": "employee@example.com" } } ], "attachments": [ { "@odata.type": "#microsoft.graph.fileAttachment", "name": "Report.pdf", "contentBytes": "base64-encoded-pdf-content" } ] } }
- Construct the email payload with the PDF attachment. Use the
- Send the Email:
- Use the Microsoft Graph API endpoint to send the email:
POST https://graph.microsoft.com/v1.0/users/{sender-email}/sendMail
- Include the
access_token
in theAuthorization
header:Authorization: Bearer {access-token} Content-Type: application/json
curl
:curl -X POST -H "Authorization: Bearer {access-token}" -H "Content-Type: application/json" \ -d @email-payload.json \ "https://graph.microsoft.com/v1.0/users/{sender-email}/sendMail"
- Use the Microsoft Graph API endpoint to send the email:
Step 5: Handle Large Attachments Securely
For large attachments (greater than 3 MB), use the Upload Session feature of Microsoft Graph API.
- Create an Upload Session:
- Send a POST request to create an upload session:
POST https://graph.microsoft.com/v1.0/users/{sender-email}/messages/{message-id}/attachments/createUploadSession
- Request Body:
{ "attachmentItem": { "attachmentType": "file", "name": "LargeReport.pdf", "size": {file-size-in-bytes} } }
- Send a POST request to create an upload session:
- Upload the File in Chunks:
- Use the
uploadUrl
returned from the upload session to upload the file in chunks (e.g., 4 MB chunks). - Example using
curl
:curl -X PUT -H "Content-Length: {chunk-size}" -H "Content-Range: bytes {start}-{end}/{total-size}" \ --data-binary @chunk-file \ "{upload-url}"
- Use the
- Send the Email:
- Once the file is uploaded, send the email as described in Step 4.
Step 6: Implement Error Handling and Logging
- Handle errors such as token expiration, API rate limits, and attachment upload failures.
- Log all activities for auditing and troubleshooting.
Summary
- Register the app in Microsoft Entra ID and configure API permissions.
- Authenticate using the Client Credentials Flow.
- Use Microsoft Graph API to send emails with attachments.
- For large attachments, use the Upload Session feature.
- Implement error handling and logging for robustness.
This setup ensures secure and efficient email delivery with attachments using Microsoft Entra ID and Microsoft Graph API.