Send Email using SharePoint PowerShell command, SMTP server

How to send email from PowerShell using SMTP

Here is a code snippet to send an email from the SharePoint server using a PowerShell script. The code can be modified to change the SMTP Server, From and To Email address, Subject, Body, and other parameters.

Send email using PowerShell
# Configure SMTP server
$smtpServer = "[SMTP SERVER]"
$mailMessage = new-object Net.Mail.MailMessage
$smtpObj = new-object Net.Mail.SmtpClient($smtpServer)

# Set email parameters
$mailMessage.From = "[FROM EMAIL ADDRESS]"
$mailMessage.ReplyTo = "[REPLY TO EMAIL ADDRESS]"
$mailMessage.To.Add("[TO EMAIL ADDRESS 1]")
$mailMessage.To.Add("[TO EMAIL ADDRESS 2]")
$mailMessage.subject = "[MAIL SUBJECT]"
$mailMessage.body = "[MAIL BODY]"

# Send email
$smtpObj.Send($mailMessage)

Do ensure that the SMTP server address is correct.

For adding more recipients, you can add another entry using $mailMessage.To.Add("[TO EMAIL ADDRESS]")


This is not an AI-generated article but is demonstrated by a human.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!