PowerShell issue with Send-MailMessage -Attachments txt size different

Daniel Ovadia 121 Reputation points
2021-03-15T12:24:36.62+00:00

Hey,

I have a strange issue...

Send-MailMessage -to 'mail@maildomain.de' -from 'mail2@maildomain.de' -Attachments "\uncpath\testfile.txt" -Subject "test" -SmtpServer mysmtphost

The File Size of my txt is 19kb, when sending by outlook client it keeps 19kb.

When sending by Powershell script the filesize is 18kb.

I tried with params like utf8 encoding, bodyashtml and so on..but everytime the txt file was send by PS it changes something.

How can I tell Powersehll sending the file attachment without making any changes like outlook client?

thanks
Regards
Daniel

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,354 questions
{count} votes

Accepted answer
  1. Daniel Ovadia 121 Reputation points
    2021-03-19T11:45:11.987+00:00

    Actually I found a workaround. Sharing is caring :)
    I use a part of robocopy, which is for logfiles but can send txt aswell.
    Howeever this is my script

    Send E-mail message with log file attachment

    $Message = New-Object Net.Mail.MailMessage($EmailFrom, $EmailTo, $EmailSubject, $EmailBody)
    $Attachment = New-Object Net.Mail.Attachment($Logfile, 'text/plain')
    $Message.Attachments.Add($Attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
    If (-not $SMTPPort -eq "25"){
    $SMTPClient.EnableSsl = $true;
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
    }
    $SMTPClient.Send($Message)

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,476 Reputation points Microsoft Vendor
    2021-03-16T05:30:41.723+00:00

    Hi @Daniel Ovadia ,

    I sent a txt file using the Send-MailMessage cmdlet and the hash values of the two files are identical. Can you check what the differences are between your two files?

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.