I have made some progress, and stuck with the attachment. Below script now sends an email but with not an attachment
$sub = "APERAK/INVRPT Report "
$to="[email protected]"
$folder = "C:\script\APERAK"
$files = Get-ChildItem "C:\script\APERAK"
$tstmp = Get-Date -UFormat "%H%M"
$dstamp = Get-Date -UFormat "%Y%m%d"
$from="[email protected]"
$smtpserver = "mail.XXXXXXXX.com"
for ($i=0; $i -lt $files.Count; $i++) {
$subject= $sub + $dstamp #+ " " +$tstmp
$filename = $files[$i].FullName
$abpath = $folder + $files[$i].FullName
$attachment = new-object Net.Mail.Attachment($abpath)
$body= Get-Content $filename
$SMTP = new-object Net.Mail.SmtpClient($smtpserver)
$MSG = new-object Net.Mail.MailMessage($from, $to, $subject, $body)
$MSG.attachments.add($attachment)
$SMTP.send($msg)
}
Exception calling "Add" with "1" argument(s): "Value cannot be null. Parameter name: item" At C:\GentranScripts\APERAK_REPORT_EMAIL1.ps1:22 char:21 + $MSG.attachments.add <<<< ($attachment) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
for some reason, the script is entering into the for loop.... so what is the problem here ?$files.Counton its own and look at the output.