I am trying to send an email, but I am getting syntax errors:
$fromaddress = "abc@a.com"
$toaddress = @('def@a.com>', 'ghi@a.com>')
$bccaddress = @('sl@a.com')
#$CCaddress = @('la@a.com>')
$Subject = "BAKUP REPORT FOR Instances "
$body = get-content C:\body.txt
$attachment = @("C:\result.text", "C:\result.csv", "C:\object.text", "C:\object.csv")
$smtpserver = "smtp.a.com"
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
#$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
I am getting the below errors.
new-object : Cannot find an overload for "Attachment" and the argument count: "4".
At line:18 char:11
+ $attach = new-object Net.Mail.Attachment($attachment)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "Add" with "1" argument(s): "Value cannot be null.
Parameter name: item"
At line:19 char:1
+ $message.Attachments.Add($attach)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At line:22 char:1
+ $smtp.Send($message)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException