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]").
- That did'nt work, Issue type User not in directory - SharePoint external access error
- How to get SharePoint Online user details from person or group column using REST API
- Create SharePoint Site Collection with new Content database in existing web application
- Fix Power BI 404 not found error when connecting SharePoint Online List as Data Source
- How to get the SharePoint Tenant Login URL
- How to wrap column text in SharePoint Online Modern List Grid View using JSON formatting
- SharePoint Server 2016 installation System Hardware requirements
- Cannot load PowerApps form in SharePoint Online due to repeated authentication
- Change SharePoint search results FullTextSqlQuery RowLimit 10000
- How to create SharePoint Document Library
- How to exclude results from SharePoint Search
- SharePoint 2010 DataForm Unable to display this Web Part System.StackOverflowException
- Fix Error 2711 SQL RBS client - The installer has encountered an unexpected error. The specified Feature name ('Docs') not found in Feature table
- How to upload file programmatically to SharePoint Document Library using Server Object Model C# .Net
- SharePoint error cannot connect to the configuration database
- Drag drop files here option missing for SharePoint document library
- How to activate and create SharePoint Spaces feature for mixed reality experience - 2D/3D images and web parts
- [Fix] Office 365 Sharepoint One Drive Error: Something went wrong. This is a temporary issue
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml
- SharePoint List excel import - This table exceeds the maximum number of supported rows
- How to customize SharePoint Modern list form using JSON formatting
- New-SPLogFile PowerShell - create new SharePoint log file
- SharePoint Online REST API not returning all list items and limit to only 100 rows
- How to Get or Set SharePoint Document ID _dlc_DocId using PowerShell
- Get-ADUser PowerShell - Get AD user details using email address
- That did'nt work, Issue type User not in directory - SharePoint external access error - SharePoint
- CSS: Apply opacity only for div background and not text - CSS
- Save current timestamp in MySQL using PHP mysqi binding - PHP
- SharePoint PowerShell Merge-SPLogFile filter by time using StartTime EndTime - SharePoint
- How choose alternate Tab Bar icon in Notepad++ - NotepadPlusPlus
- Detect Data roaming in Android Application - Android
- How to find the Battery Cycle Count on macOS Ventura - MacOS
- How to destroy PHP session() - PHP