Microsoft 365 – PowerShell – Options to send mail – part1 – exploring Send-MailMessage PowerShell CMDLET

Microsoft 365 - PowerShell - exploring Send-MailMessage PowerShell CMDLET
Microsoft 365 - PowerShell - exploring Send-MailMessage PowerShell CMDLET

Hi All,

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

Details

  • Syntax
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 'prasham@knowledgejunction1.onmicrosoft.com' -To 'prasham1@knowledgejunction1.onmicrosoft.com' -Subject 'LIFE IS BEAUTIFUL' -SmtpServer "smtp.office365.com" -UseSsl -Port 587 -Credential $cred

  • Here for Microsoft 365,
    • SMTP Server issmtp.office365.com
    • Port – 587
  • User – prasham@knowledgejunction1.onmicrosoft.com (Prasham Sabadra) sending mail to user – prasham1@knowledgejunction1.onmicrosoft.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

Receiver inbox

Microsoft 365 - PowerShell - exploring Send-MailMessage PowerShell CMDLET
fig : Microsoft 365 – PowerShell – exploring Send-MailMessage PowerShell CMDLET – Inbox of user – prasham1@knowledgejunction1.onmicrosoft.com (Prasham1 Sabadra) – mail received successfully

Senders sent items

Microsoft 365 - PowerShell - exploring Send-MailMessage PowerShell CMDLET
fig : Microsoft 365 – PowerShell – exploring Send-MailMessage PowerShell CMDLET – “Sent Items” of user – prasham@knowledgejunction1.onmicrosoft.com (Prasham Sabadra) – mail sent successfully
  • 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 'prasham@knowledgejunction1.onmicrosoft.com' -To 'prasham1@knowledgejunction1.onmicrosoft.com' -Subject 'LIFE IS BEAUTIFUL' -SmtpServer "smtp.office365.com" -UseSsl -Port 587 -Credential $cred

fig : Microsoft 365 - PowerShell - exploring Send-MailMessage PowerShell CMDLET - exploring attribute -DeliveryNotificationOption
fig : Microsoft 365 – PowerShell – exploring Send-MailMessage PowerShell CMDLET – exploring attribute –DeliveryNotificationOption
  • As mail sent successfully, sender will receive success notification email as shown in below snap
fig : Microsoft 365 - PowerShell - exploring Send-MailMessage PowerShell CMDLET - received success mail notification based on options specified to attribute - DeliveryNotificationOption
fig : Microsoft 365 – PowerShell – exploring Send-MailMessage PowerShell CMDLET – received success mail notification based on options specified to attribute – DeliveryNotificationOption

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 Send-MailMessage.

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

REFERENCES

Thanks for reading !!! HAVE a FANTASTIC LEARNING AHEAD 🙂 LIFE IS BEAUTIFUL 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

1 Response

  1. August 20, 2023

    […] In last article we discussed – how to send email using Send-MailMessage PowerShell CMDLET – Microsoft 365 – PowerShell – Options to send mail – part1 – exploring Send-MailMessage Power… […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading