get all txt files $filenameAndPath = code please help

  • Hello

    I need to get txt files from directory and send email, when I put name of the file my code is working, but I need to get all txt files from directory and dont know which to use,please help me thank you here is my code: This code is working,just not sure how select all txt and email

    # Email the TXT

    $EmailFrom = "test@yahoo.com"

    $EmailTo ="john1@yahoo.com"

    $Subject = "Testing"

    $Body = "Hello,nnPlease find attached a report .nnThank You,"

    $SMTPServer = "smtp.office365.com"

    $SMTPPort = "111"

    $filenameAndPath = "\\John\Data\test\Test1.txt"

     

    $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)

    $smtp.EnableSSL = $true

    $smtp.Credentials = New-Object System.Net.NetworkCredential('yyy@yahoo.com' , 'yyy');

    $message = New-Object System.Net.Mail.MailMessage

    $message.Subject = $Subject

    $message.Body = $Body

    $message.From = New-Object System.Net.Mail.MailAddress($EmailFrom)

    $message.To.Add($EmailTo)

    $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)

    $message.Attachments.Add($attachment)

    $smtp.Send($message)

     

  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • As PowerShell can have problems with UNC paths try something like:

    New-PSDrive -Name "TxtFiles" -PSProvider "FileSystem" -Root "\\John\Data" | Out-Null
    $Files = Get-ChildItem TxtFiles:\test\*.txt
    Foreach ($File in $Files) {
    $attachment = New-Object System.Net.Mail.Attachment("TxtFiles:\test\$($File.Name)", 'text/plain')
    #$attachment = New-Object System.Net.Mail.Attachment($File.FullName, 'text/plain')
    $message.Attachments.Add($attachment)
    }
    Remove-PSDrive -Name "TxtFiles"

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply