May 9, 2024 at 2:43 pm
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,n
nPlease find attached a report .n
nThank 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)
May 10, 2024 at 3:10 pm
Thanks for posting your issue and hopefully someone will answer soon.
This is an automated bump to increase visibility of your question.
May 11, 2024 at 9:14 am
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 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy