Send-MailMessage makes attachment larger

Viewed 219

I have a Powershell script to send emails with zipped files as an attachment. On the server, the zip files are about 16MB. Those emails are rejected by our Exchange server because the attachment is too big. We have 20MB limit. The Exchange server shows the attachments as 21MB. What could be the reason for that?

This is the script:

$baseDir = "\\999.999.999.999\xxxxx\yyyy"

#email settings
$MailServer = "mail.#######.com"
$eMailSender = 'xxx@######.com'
$Recipient = 'yyy@######.com'
$Subject = "Files available"
$BodyBase = "Hello,`n`nattached are the files`n"

$zipFileBaseName = "xyzfiles" + (Get-Date).ToString("yyyyMMdd")
$zipFiles = $baseDir + "\" + $zipFileBaseName + "*"
$NumZips = (Get-ChildItem $zipFiles).count

$i = 0
foreach($zip in Get-ChildItem $zipFiles)
{
    $i++
    $Body = "This is email " + $i + " of " + $NumZips + ". (" + $zip.Name + ")"
    $Body = $BodyBase + $Body

    Send-MailMessage -SmtpServer $MailServer -from $eMailSender -To $Recipient -Subject $Subject -Body $Body -Attachments $zip.Fullname

}

The number of zip files in the folder is different every day. Sometimes the size is just about 10MB. In this case, the email is sent.

Any ideas what adds 5MB to the attachments?

Thanks,
Michael

1 Answers

Not the answer you expect but this is how SMTP works.

This fragment from Wikipedia gives a proper explanation:

Also note that all these size limits are based, not on the original file size, but the MIME-encoded copy. The common Base64 encoding adds about 37% to the original file size, meaning that an original 20MB file could exceed a 25MB file attachment limit.[11] A 10MB email size limit would require that the size of the attachment files is actually limited to about 7MB.

$arr = [byte[]]::new(16Mb)
[math]::Round($arr.Length / 1Mb) # => 16 Mb
$b64 = [Convert]::ToBase64String($arr)
[math]::Round($b64.Length / 1Mb) # => 21 Mb
Related