#Microsoft #Graph #Exchange #API #PowerShell #Security
>[!info] Reading User Messages with Microsoft Graph
>This guide demonstrates how to use Microsoft Graph PowerShell to access and read user emails securely. For application setup, see [[App Registration|how to register your application]].
>[!related]
>Related documentation:
>- [[Microsoft Graph API Permissions]] - Required permissions
>- [[App Registration]] - Application setup
>- [[Microsoft Graph mgUser Command Guide]] - Additional commands
>- [[Azure RMS]] - Rights Management Services
## Prerequisites
>[!warning] Required Permissions
>Ensure your application has the following permissions:
>- `Mail.Read` or `Mail.ReadWrite` - For reading user emails
>- `User.Read` - For accessing user information
>
>See [[Microsoft Graph API Permissions]] for detailed permission information.
## Security Requirements
>[!danger] Critical Security Guidelines
>Never store credentials in your scripts. Choose one of these secure methods:
>
>1. **Environment Variables** (Development)
> - Store in `.env` file
> - Add to `.gitignore`
> - Use secure environment management
>
>2. **Azure Key Vault** (Production)
> - Store secrets securely
> - Manage access with RBAC
> - Rotate credentials regularly
>
>3. **Managed Identities** (Azure Services)
> - Use System or User Assigned
> - No credential management needed
> - Automatic credential rotation
### Option 1: Environment Variables Setup
1. **Create `.env` file**:
```plaintext
# .env
AZURE_TENANT_ID=your-tenant-id
AZURE_CLIENT_ID=your-client-id
AZURE_CLIENT_SECRET=your-client-secret
```
2. **Add to `.gitignore`**:
```plaintext
# .gitignore
.env
*.env
secrets/
```
3. **Load Environment Variables**:
```powershell
# Load environment variables securely
function Load-EnvFile {
param([string]$EnvPath)
if (-not (Test-Path $EnvPath)) {
throw "Environment file not found at: $EnvPath"
}
Get-Content $EnvPath | ForEach-Object {
if ($_ -match '^([^=]+)=(.*)