Send Email with attachment using SharePoint PowerShell, SMTP server

Below is a code snippet to send an email with an attachment from the SharePoint server using PowerShell script. The attachment can be referred to any location or generated from the script itself.

The below code can be modified to change SMTP Server, From and To Email address, Subject, Body, Attachment, and other parameters.

Send an email with attachment via PowerShell
# File to be attached in email
$attachment = new-object Net.Mail.Attachment("[FILE PATH]")

# 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
The attachment can be any kind of file, but take care of few points -

- SMTP Server address is correct

- File path exists

- A firewall is not blocking file type

- Size of file is within allowed limits

If you want to add more recipients, just add another entry for $mailMessage.To.Add("[TO EMAIL ADDRESS]").

Try Out Code2care Dev Tools:


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, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.