Zum Inhalt

Send E-Mails with PowerShell using Modern Auth. (OAuth)

You are currently using Send-MailMessage with Basic Auth. and want to transition to Modern Authentication?
May this PowerShell Module assists you, without having to rewriting your routine with Microsoft Graph directly.

  • Provides Send-MailMessage translation to Microsoft Graph
  • Supports SMTP with OAuth2 (Modern Auth, SASL XOAUTH2)

Send emails in PowerShell via Graph API, or SMTP (MailKit) with Modern Auth (OAuth) support.

Installation

Send-ModernMailMessage is a modern PowerShell cmdlet for sending emails. It is part of the ModernMailTools module, designed as an improved and more flexible alternative to the legacy Send-MailMessage cmdlet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Install
# > Install the module from the PowerShell Gallery
Install-Module -Name ModernMailTools

# Default Cmdlet
# > After installation, you can start sending emails using
Send-ModernMailMessage

# Enable Alias for Compatibility (Optional)
Enable-MailMessageAlias
Send-MailMessage

Authentication

It supports flexible authentication options to fit different use cases, whether you're sending mail as a user or from an application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# As User (Default)
# > Simple and interactive
Send-ModernMailMessage

# As Application
# > Send mail using an app registration (useful for unattended scenarios)
#Register-ModernMailMessageEntraIDApp -ApplicationName "M365 ModernMailTools"
Send-ModernMailMessage -ClientId [AppId] -CertificateThumbprint [Thumbprint] -TenantId [TenantId]

# Pre-Authentication as User (offload authentication)
# > Use Connect-MgGraph to handle authentication up front and reuse the session
Connect-MgGraph -Scopes "Mail.Send" -NoWelcome
Send-ModernMailMessage

# Pre-Authentication as App
# > Authenticate ahead of time as an app and pass the tokens
Connect-MgGraph -ClientId [AppId] -CertificateThumbprint [Thumbprint] -TenantId [TenantId]
Send-ModernMailMessage

Protocols

Additionally, it supports different protocol options, allowing you to choose between Microsoft Graph API or traditional SMTP, depending on your environment or preference.

1
2
3
4
5
6
7
# Microsoft Graph API (Default)
# > Modern and token-based
Send-ModernMailMessage -From "user01@fabrikam.com" -To "user02@fabrikam.com" -Subject "Test mail"

# SMTP
# > For compatibility with existing setups
Send-ModernMailMessage -From "user01@fabrikam.com" -To "user02@fabrikam.com" -Subject "Test mail" -SmtpServer smtp.office365.com -Port 587 -UseSsl

Summary

Send-ModernMailMessage provides a modern, flexible way to send emails in PowerShell using either SMTP or Microsoft Graph API. While there are other solutions available, some felt too complex or didn’t match my goal of easily switching between methods. It supports both user and app authentication, includes a legacy alias for Send-MailMessage, and is easy to install. Though not every command feature is complete yet, creating this module was a valuable process and leaves room for further improvement.
Wishing you all a good day!


References:

Alternatives:

Kommentare