I am trying to set up a batch file which will call a Powershell and ultimately send a file on the users computer to a given email address. While I can get the email to send, it always sends without the attachment included.
The batch file code i have is:
powershell.exe -executionpolicy bypass -file C:\Users\attachment2.ps1
and the Powershell code I have is:
$attachment = "C:\Users\SearchResults\37.csv"
$EmailFrom = "david@gmail.com"
$EmailTo = "john@gmail.com"
$Subject = "The subject of your email"
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$Body = "This is just a test mail to verify the working of CMD"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxx", "xxxx");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $attach, $Body)
If anyone knows as to why the attachment is not being included it would be of great help. I have looked at other examples on stackoverflow as well as other sites but have been not able to fix my own code. Any help would be greatly appreciated.