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 PowerShell script. The code can be modified to change 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]")

|ADsADsAdss|



Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.