Entra / Microsoft 365 · Exchange Online
Upload many attachments to a message
Uploading and sending many attachments with a mail message.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes "Mail.Send", "Mail.ReadWrite" -NoWelcome
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Connect-MgGraph -Scopes "Mail.Send", "Mail.ReadWrite" -NoWelcome# Customize where the attachment files are stored$AttachmentsFolder = "c:\Temp\Attachments"[array]$InputAttachments = Get-ChildItem -Path $AttachmentsFolderIf (!($InputAttachments)) {Write-Host "No attachments found in $AttachmentsFolder"Break}$FileSizeThreshold = 146800640 # 140 MB in bytes$TotalFileSize = ($InputAttachments | Measure-Object -Sum Length).Sum$FoundSizeMB = [math]::Round($TotalFileSize / 1MB, 2)If ($TotalFileSize -gt $FileSizeThreshold) {Write-Host ("Total size of attachments is {1} MB. Maximum size for an Outlook message is 140 MB. Please remove some attachments and try again." -f $TotalFileSize, $FoundSizeMB)Break}[array]$MsgAttachments = $nullWrite-Host ("Processing {0} attachments..." -f $InputAttachments.Count)[array]$MsgAttachments = $nullForEach ($File in $InputAttachments.Name) {$FullFileName = $AttachmentsFolder + "\" + $File$ConvertedContent = [Convert]::ToBase64String([IO.File]::ReadAllBytes($FullFileName))$FileExtension = [System.IO.Path]::GetExtension($FullFileName)Switch ($FileExtension) {".pdf" {$ContentType = "application/pdf"}".docx" {$ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}".xlsx" {$ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}".pptx" {$ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"}".jpg" {$ContentType = "image/jpeg"}".png" {$ContentType = "image/png"}default {$ContentType = "application/octet-stream"}}$AttachmentDetails = @{"@odata.type" = "#microsoft.graph.fileAttachment"Name = $FileContentType = $ContentTypeContentBytes = $ConvertedContent}$MsgAttachments += $AttachmentDetails}Write-Host ("Sending a message with {0} attachments. Total size of attachments: {1} MB" -f $InputAttachments.Count, $FoundSizeMB)$MsgSubject = "Lots of attachments"$MsgFrom = (Get-MgContext).Account$ToRecipients = @{}$ToRecipients.Add("emailAddress",@{'address'='hans.geering@office365itpros.com'})[array]$MsgTo = $ToRecipients$HtmlBody = "Some important reading for you... Please note the large number of attachments and the size of some files!"$MsgBody = @{}$MsgBody.Add('Content', "$($HtmlBody)")$MsgBody.Add('ContentType','html')$Message = @{}$Message.Add('subject', $MsgSubject)$Message.Add('toRecipients', $MsgTo)$Message.Add('body', $MsgBody)$Message.Add('attachments', $MsgAttachments)$Params = @{}$Params.Add('message', $Message)$Params.Add('saveToSentItems', $true)$Params.Add('isDeliveryReceiptRequested', $true)Try {Send-MgUserMail -UserId $MsgFrom -BodyParameter $Params -ErrorAction StopWrite-Host "Mesaage sent"} Catch {Write-Host ("Error sending message: {0}" -f $_.Exception.Message)}
Attribution
Author
Office365itpros