Send result of ls command using System.Web.Mail.MailMessage

Welcome Forums General PowerShell Q&A Send result of ls command using System.Web.Mail.MailMessage

Viewing 2 reply threads
  • Author
    Posts
    • #195197
      Participant
      Topics: 1
      Replies: 0
      Points: 11
      Rank: Member

      Hi,

      Im trying to write a script to send the result of variable ($content) as the body :

      $content = ls -Path $folder | Sort-Object LastWriteTime -Descending

      I use [System.Web.Mail.SmtpMail]::Send($mail) to send messages

      It works except, i get only the filenames on the same line, i wish to get the same layout (with the columns…)

      $mail = New-Object System.Web.Mail.MailMessage
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpserver”, $server)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”, $serverPort)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpusessl”, $true)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendusername”, $credentials.UserName)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendpassword”, $credentials.Password)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout”, $timeout / 1000)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendusing”, 2)
      $mail.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”, 1)
      $mail.From = $from
      $mail.To = $to
      $mail.Subject = $subject
      $mail.Body = $content

      Perhaps i need to change the type of $mail.Body ?

      $mail.Body.GetType()
      $content.GetType()

      I get :

      $mail.Body => name : String , BaseType : System.Object
      $content => name : Object[] , BaseType : System.Array

      Any idea ?

       

      Thank you so much guys!

    • #195239
      Moderator
      Topics: 5
      Replies: 245
      Points: 1,394
      Helping Hand
      Rank: Community Hero

      If it is a single object returned, then you will see the exact type name, whereas if there are multiple objects returned then the type will be array irrespective of any type.

      If you are writing a PowerShell script then you can use Send-MailMessage CmdLet, and you need to format the content in the body as per your requirement.

      Thank you.

    • #195257
      Participant
      Topics: 17
      Replies: 1958
      Points: 4,020
      Helping Hand
      Rank: Community Hero

      As you’ve seen, the mail body needs to be text. Plain text is just that, plain, no colors, formatting, tables, headers. To answer your question, to convert to a string you can use Out-String, an example:

      to use in email:

      To get full flexibility in formatting, you should use HTML. This can be as simple as:

      If you use HTML, you can format it any that you want. Take a look at the free resources on HTML reporting. As Kiran also stated, you can also use Send-MailMessage rather than manually creating the message.

Viewing 2 reply threads
  • The topic ‘Send result of ls command using System.Web.Mail.MailMessage’ is closed to new replies.