Sending email with binary attachments

Welcome Forums General PowerShell Q&A Sending email with binary attachments

Viewing 1 reply thread
  • Author
    Posts
    • #242456
      Participant
      Topics: 1
      Replies: 0
      Points: -6
      Rank: Member

      Hi, I am trying to develop an automation script that will send an Excel file (.xlsx) to our team. However, during my tests, it seems that the attachments are being converted into text file. As this is my first time trying to use PowerShell to send email, I am not sure whether this is due to our SMTP server setting, or this is the Send-MailMessage cmdlet default.

      The test command I have been testing so far:

      $mailcred = Get-Credential myusername
      $myfile = "C:\Temp\testfile.xlsx"
      Send-MailMessage -From "sender@company.com" -to "recipient@company.com" -Subject "Test email from PowerShell cmdlet with attachment" -Body "This is a test email" -Attachments $myfile -Credential $mailcred -SmtpServer 'smtpserver.com' -Port 25 -UseSsl

      Are there additional steps I need to do first to be able to send binary file attachments using the Send-MailMessage cmdlet? So far, what I have seen mostly examples in the web are attachments with .txt and .csv files.

      Thanks for the advise in advance!

       

       

      • This topic was modified 2 months, 1 week ago by Roose Chua.
    • #242561
      Participant
      Topics: 15
      Replies: 1775
      Points: 3,214
      Helping Hand
      Rank: Community Hero

      Roose, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      The cmdlet doesn’t do any conversions. The code can be used to attach any type of file that I’m aware of just as you would do in a mail client. If the attachment is being stripped, then it’s most likely a SPAM filter or something. The examples of csv and txt are typically supplementing reporting information from Powershell. While you can use HTML to make a nice report email, if there is a list of 100 users, then you would typically attach that as a CSV or TXT.

Viewing 1 reply thread
  • You must be logged in to reply to this topic.