1
votes

I am trying to send an email, but I am getting syntax errors:

$fromaddress = "[email protected]" 
$toaddress = @('[email protected]>', '[email protected]>')
$bccaddress = @('[email protected]') 
#$CCaddress = @('[email protected]>') 
$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
2
What if you attach a single file. Does it still work?Anthony Horne
Normally Net.Mail requires you to add attachments one by one, that's why I thought you should start with a single file - it is complaining about no function existing for attachment with 4 parameters.Anthony Horne
This works for me but error in smtp server...which is not related to this script. Thanksvivekanand pandey

2 Answers

0
votes

I prefer to use Send-MailMessage as it has a nicer syntax the Net.Mail, you can also use Splatting to make it even easier to read:

$messageParameters = @{                        
    From = "[email protected]"
    To = "[email protected]>", "[email protected]>"
    Bcc = "[email protected]"
    #Cc = "[email protected]>"

    Subject = "BAKUP REPORT FOR Instances"
    Body = Get-Content C:\body.txt
    Attachments = "C:\result.text", "C:\result.csv", "C:\object.text", "C:\object.csv"
    SmtpServer = "smtp.a.com"
}                        
Send-MailMessage @messageParameters -BodyAsHtml

Depending on your SMTP Server, you might also need to specify Credentials for authentication and/or SSL as well.

0
votes

What about:

$fromaddress = "[email protected]" 
$toaddress = @('[email protected]>', '[email protected]>')
$bccaddress = @('[email protected]') 
#$CCaddress = @('[email protected]>') 
$Subject = "BAKUP REPORT FOR Instances " 
$body = get-content C:\body.txt 
$attachment1 = @("C:\result.text") 
$attachment2 = @("C:\result.csv",) 
$attachment3 = @("C:\object.text") 
$attachment4 = @("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($attachment1) 
$message.Attachments.Add($attach) 
    $attach = new-object Net.Mail.Attachment($attachment2) 
$message.Attachments.Add($attach) 
    $attach = new-object Net.Mail.Attachment($attachment3) 
$message.Attachments.Add($attach) 
    $attach = new-object Net.Mail.Attachment($attachment4) 
$message.Attachments.Add($attach) 

$message.body = $body 
$smtp = new-object Net.Mail.SmtpClient($smtpserver) 
$smtp.Send($message) 

Your last error may be related to authentication. Best to search for "send email by telnet" to do a quick test. use How to send email using simple SMTP commands via Gmail? as reference.

Attached the code to include authentication:

----> ADD AFTER >> $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, 
      $Password);

PFE: https://community.spiceworks.com/topic/621669-powershell-mail-script