尝试发送电子邮件时出错

时间:2017-05-30 13:07:19

标签: powershell powershell-v2.0

我正在尝试发送电子邮件,但我收到语法错误:

$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) 

我收到以下错误。

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 个答案:

答案 0 :(得分:0)

怎么样:

$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 
$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) 

您的上一个错误可能与身份验证有关。最好搜索"通过telnet发送电子邮件"做快速测试。使用How to send email using simple SMTP commands via Gmail?作为参考。

附上代码以包含身份验证:

----> 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

答案 1 :(得分:0)

我更喜欢使用Send-MailMessage,因为它具有更好的语法Net.Mail,您还可以使用Splatting使其更易于阅读:

$messageParameters = @{                        
    From = "abc@a.com"
    To = "def@a.com>", "ghi@a.com>"
    Bcc = "sl@a.com"
    #Cc = "la@a.com>"

    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

您可能还需要为身份验证和/或SSL指定凭据,具体取决于您的SMTP服务器。