Microsoft 365 – PowerShell – Options to send mail – part1 – exploring Send-MailMessage PowerShell CMDLET
Greetings for the day!!! Today new learning 🙂
Recently I got requirement to send an email using PowerShell. Since I never tried, bit googled and I found couple of options. So sharing. SHARING IS CARING 🙂
Option 1 – Using PowerShell CMDLET – Send-MailMessage
Send-MailMessage [-Attachments <String>] [-Bcc <String>] [[-Body] <String>] [-BodyAsHtml] [-Encoding <Encoding>] [-Cc <String>] [-DeliveryNotificationOption <DeliveryNotificationOptions>] -From <String> [[-SmtpServer] <String>] [-Priority <MailPriority>] [-ReplyTo <String>] [[-Subject] <String>] [-To] <String> [-Credential <PSCredential>] [-UseSsl] [-Port <Int32>] [<CommonParameters>]
- We must specify a Simple Mail Transfer Protocol (SMTP) server or the Send-MailMessage command fails
- Example – Send mail from one user to another user
#get the credentials $cred = Get-Credential #using Send-MailMessage CMDLET Send-MailMessage -From 'firstname.lastname@example.org' -To 'email@example.com' -Subject 'LIFE IS BEAUTIFUL' -SmtpServer "smtp.office365.com" -UseSsl -Port 587 -Credential $cred
- Here for Microsoft 365,
- SMTP Server is – smtp.office365.com
- Port – 587
- User – firstname.lastname@example.org (Prasham Sabadra) sending mail to user – email@example.com (Prasham1 Sabadra)
- We need credential of the user who has permission to perform this action – to send an email.
- Following are the snaps of receivers inbox and senders sent items
Senders sent items
- Here I’ll like to highlight one good option to this CMDLET – DeliveryNotificationOption
- Following are the possible values for “DeliveryNotificationOption”
None: No notification.
OnSuccess: Notify if the delivery is successful.
OnFailure: Notify if the delivery is unsuccessful.
Delay: Notify if the delivery is delayed.
Never: Never notify.
- We could specify the options by separating with comma – ‘,’ as
#using Send-MailMessage CMDLET exploring - DeliveryNotificationOption Send-MailMessage -DeliveryNotificationOption OnFailure, OnSuccess -From 'firstname.lastname@example.org' -To 'email@example.com' -Subject 'LIFE IS BEAUTIFUL' -SmtpServer "smtp.office365.com" -UseSsl -Port 587 -Credential $cred
- As mail sent successfully, sender will receive success notification email as shown in below snap
However, there is Warning from Microsoft
The Send-MailMessage cmdlet is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use
We will see other alternative is PnP CMDLET to send mail in our next article
We have some very good articles related to Mail and Microsoft 365
- Microsoft 365 : Exchange Online / PowerShell – PowerShell – Resolving error – Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail – https://knowledge-junction.in/2023/02/07/microsoft-365-exchange-online-powershell-resolving-error-send-mailmessage-the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated-the-server-response-was-5-7-57-c/
- M365: Microsoft Graph – Part 10 – Send Email using Graph API from Console Application (Background Job) – https://knowledge-junction.in/2020/03/15/m365-microsoft-graph-part-10-send-email-using-graph-api-from-console-application-background-job/
- Share Multiple Attachments of SharePoint List via Email using Power Automate – https://knowledge-junction.in/2021/02/13/share-multiple-attachments-of-sharepoint-list-via-email-using-power-automate/
- Power Platform : Power Automate – sending email to SharePoint group (all users / members of the SharePoint group) – https://knowledge-junction.in/2022/07/13/power-platform-power-automate-sending-email-to-sharepoint-group-all-users-members-of-the-sharepoint-group/
Thanks for reading !!! HAVE a FANTASTIC LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂