In this Microsoft Graph PowerShell SDK and Exchange Online post, we will show how to use Graph API to send an email.
Microsoft Graph PowerShell allows us to tap into Microsoft Graph API and use all the Microsoft 365 APIs, including Exchange Online.
With the Mail REST API, we can connect to Exchange Online, user or Shared Mailbox and send emails programmatically using Graph’s secure protocols.
Before you can use the API, make sure you follow the following articles and configure an App Registration and a certificate.
Once configured the above, use the following PowerShell code to send an email from your Microsoft Exchange Online infrastructure.
PowerShell Code
Note: The send address needs to be a valid email address in Exchange Online.
Import-Module Microsoft.Graph.Users.Actions
$params = @{
message = @{
subject = "Email from Graph API"
body = @{
contentType = "Text"
content = "This is an automated email from Graph API"
}
toRecipients = @(
@{
emailAddress = @{
address = "Recipient Email"
}
}
)
}
saveToSentItems = "false"
}
$userId = "Sender Email Address"
Send-MgUserMail -UserId $userId -BodyParameter $params
To test the configuration, Add a recipient email address and a sender email address ($userID)