2026 Deprecation Notice for Exchange Online: Microsoft is actively retiring Exchange Web Services (EWS) (https://outlook.office365.com/EWS/Exchange.asmx) in Exchange Online, and Basic Authentication (WebCredentialswith username/password) is permanently disabled.
- For Exchange Online / Microsoft 365: Use Method 1 (Send-MgUserMail/ Microsoft Graph REST API) with Certificate-Based App-Only Authentication below.
- For On-Premises Exchange Server (2016 / 2019 / SE): You can continue using Method 2 (EWS Managed APIMicrosoft.Exchange.WebServices.dll) against your internal EWS endpoint.
Architecture: Legacy EWS EmailMessage.SendAndSaveCopy() vs. Modern Graph Send-MgUserMail
PowerShell Automated Email Delivery Architecture
Exchange Online: Retiring
Legacy EWS Managed API
- Requires local
Microsoft.Exchange.WebServices.dll - Uses broad
ApplicationImpersonationrole - Blocked in Exchange Online EWS retirement
Least-Privilege Security
EXO RBAC for Applications
- Grant Graph
Mail.Sendor EXOApplication Mail.Send - Scope to specific sender mailbox via
New-ManagementScope - Authenticate with local X.509 Certificate
Exchange Online: Recommended
Microsoft Graph (Send-MgUserMail)
POST /v1.0/users/{sender}/sendMail- Supports HTML bodies, CC/BCC, and file attachments
SaveToSentItems = $trueparity with EWS
Method 1 (Exchange Online / M365): Send Email Using Microsoft Graph PowerShell (Send-MgUserMail)
In Exchange Online, the official replacement for Microsoft.Exchange.WebServices.Data.EmailMessage and SendAndSaveCopy() is Send-MgUserMail (from the Microsoft.Graph.Users.Actions module) or a direct Invoke-RestMethod call to https://graph.microsoft.com/v1.0/users/{id}/sendMail.
Prerequisites:
- An Entra ID App Registration granted the
Mail.SendApplication permission. - (Recommended) An Exchange Online RBAC for Applications management scope so the app can only send mail from your designated notification/shared mailbox rather than any mailbox in the tenant.
PowerShell Script (Send-MgUserMail with Unattended Certificate Auth):
POWERSHELL
# Connect to Microsoft Graph using Certificate-Based App-Only Authentication
Connect-MgGraph `
-ClientId "c841ae33-4093-4385-8a90-6a530761f9b9" `
-TenantId "ab85c7a1-366d-44ba-acc6-924fa0950045" `
-CertificateThumbprint "BEAC429DE404B46FF030811C0C9DE00DE701C027" `
-NoWelcome
$SenderMailbox = "[email protected]"
# Construct the message payload (equivalent to EWS EmailMessage + SendAndSaveCopy)
$EmailParams = @{
Message = @{
Subject = "[LAB 365] Automated Report via Microsoft Graph API"
Body = @{
ContentType = "HTML"
Content = @
<p>Hello Team,</p>
<p>This automated email was sent from <strong>$SenderMailbox</strong> using <code>Send-MgUserMail</code> (replacing the legacy EWS Managed API).</p>
@
}
ToRecipients = @(
@{ EmailAddress = @{ Address = "[email protected]" } }
@{ EmailAddress = @{ Address = "[email protected]" } }
)
}
SaveToSentItems = $true
}
# Send the email and save a copy in Sent Items
Send-MgUserMail -UserId $SenderMailbox -BodyParameter $EmailParams
Write-Host "[+] Email sent successfully from $SenderMailbox via Microsoft Graph!" -ForegroundColor Green
Zero-Module Alternative: Pure Invoke-RestMethod (No Graph SDK Required)
If you want to send email from a lightweight scheduled task without installing the Microsoft.Graph module:
POWERSHELL
$MailJson = @{
message = @{
subject = "[LAB 365] Zero-Module Graph REST API Email"
body = @{
contentType = "HTML"
content = "<p>Sent directly via <code>POST /v1.0/users/$SenderMailbox/sendMail</code>.</p>"
}
toRecipients = @(
@{ emailAddress = @{ address = "[email protected]" } }
)
}
saveToSentItems = $true
} | ConvertTo-Json -Depth 6
Invoke-RestMethod `
-Method Post `
-Uri "https://graph.microsoft.com/v1.0/users/$SenderMailbox/sendMail" `
-Headers @{ Authorization = "Bearer $AccessToken" } `
-ContentType "application/json" `
-Body $MailJson
Method 2 (On-Premises Exchange Server 2016 / 2019 / SE): Send Email Using the EWS Managed API
If you are running on-premises Exchange Server Subscription Edition (SE), Exchange 2019, or Exchange 2016, EWS remains fully supported for internal automation.
Prerequisites:
- The service account must hold the
ApplicationImpersonationmanagement role (New-ManagementRoleAssignment -Name "EWS-Impersonation" -Role ApplicationImpersonation -User "[email protected]"). - Install the Exchange Web Services (EWS) Managed API 2.2 (
Microsoft.Exchange.WebServices.dll) on the automation host.
Complete EWS Managed API PowerShell Script:
POWERSHELL
param(
[string]$Mailbox = "[email protected]",
[string]$UserName = "[email protected]",
[string]$Password = "YourServiceAccountPassword",
[string]$EwsUrl = "https://mail.yourdomain.com/EWS/Exchange.asmx"
)
# Enforce TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Import the EWS Managed API 2.2 assembly
$EWSServicePath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"
Import-Module $EWSServicePath
# Create the ExchangeService object and bind credentials
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$Service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $UserName, $Password
$Service.Url = [uri]$EwsUrl
# Configure EWS Impersonation for the target sender mailbox
$ImpUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId(
[Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,
$Mailbox
)
$Service.ImpersonatedUserId = $ImpUserId
# Construct and send the EmailMessage
$Message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $Service
$Message.Subject = "Automated Message Sent via EWS Managed API - Sunil Chauhan"
$Message.From = $Mailbox
$Message.ToRecipients.Add("[email protected]")
$Message.ToRecipients.Add("[email protected]")
$Message.Body = New-Object Microsoft.Exchange.WebServices.Data.MessageBody(
[Microsoft.Exchange.WebServices.Data.BodyType]::HTML,
"<p>This is a test message created by <strong>Sunil Chauhan</strong> using the EWS Managed API client.</p>"
)
# Send message and save a copy in the sender's Sent Items folder
$Message.SendAndSaveCopy()
Write-Host "[+] EWS message sent and saved to Sent Items for $Mailbox." -ForegroundColor Green