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]"
$mailMessage.Attachments.Add($attachment)
# Send email
$smtpObj.Send($mailMessage)
$attachment.Dispose()
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]").
More Posts related to SharePoint,
- SharePoint An unexpected error has occurred - Correlation ID and Merge-SPlogfile
- SharePoint list excel import error - Title is a required filed and can't be empty
- Send Email with attachment using SharePoint PowerShell, SMTP server
- Sharepoint errors were found when compiling the workflow
- How to update SharePoint List Item programmatically
- Merge-SPlogfile PowerShell - SharePoint Correlation ID error
- Get-ADUser PowerShell - Get AD user details using email address
- Restore deleted Office 365 SharePoint group site
- [Solved] SharePoint Access Denied error editing Document Name
- How to migrate SharePoint Designer 2010 workflow to Power Automate FLOW (Microsoft Office 365)
More Posts:
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error - Android
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files - Java
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later. - Android
- How to repeat tile background image in Android Activity - Android
- How to change Android Button Color using xml attribute and programatically using java - Android
- Maven : java.lang.ClassNotFoundException: Xmx512m - Android
- How to Get or Set SharePoint Document ID _dlc_DocId using PowerShell - SharePoint
- How to find AUTO_INCREMENT fileds value in mysql table - MySQL
- Add Animated Scrolling to Html Page Title Script - JavaScript
- JavaScript : Get url protocol HTTP, HTTPS, FILE or FTP - JavaScript
- Unable to connect to the Internet : Google Chrome - Chrome
- java.io.IOException: Unable to locate resource handler.properties - Java
- Redirect page using jQuery - jQuery
- Android Studio : Build Tools 23.0.0 rc1 is deprecated - Android-Studio
- Eclipse : This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in - Eclipse