尝试发送电子邮件时出错

维韦坎德潘迪

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

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

我收到以下错误。

新对象:找不到“附件”和参数计数的重载:“4”。
在第 18 行 char:11 
+ $attach = new-object Net.Mail.Attachment($attachment) 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException 
    +fullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

异常调用“添加” “1”参数:“值不能为空。
参数名称:项目”
在第19行字符:1 
+ $message.Attachments.Add($attach) 
+~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    +FullyQualifiedErrorId:ArgumentNullException

使用“1”参数调用“Send”的异常:“发送邮件失败。” 
在第 22 行字符:1 
+ $smtp.Send($message) 
+ ~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException 
    + FullQualifiedErrorId : SmtpException
詹姆斯 C.

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

$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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

发送电子邮件时出错:引发SMTPAuthenticationError(代码,分别)

通过Office365 smtp使用nodemailer发送电子邮件时出错(MEANjs支架)

Cakephp 3尝试发送Gmail电子邮件时出错

在GoDaddy服务器上发送电子邮件时出错:Laravel 5.1

通过Gmail发送电子邮件时出错

通过mailgun发送电子邮件时出错

使用邮政MVC发送电子邮件时出错?

Laravel 5:发送电子邮件时出错

使用nodemailer发送电子邮件时出错

通过Sendgrid API发送电子邮件时出错

尝试在Laravel中发送电子邮件时出错

C#-从Outlook发送电子邮件时出错

在Google AppMaker中发送电子邮件时出错

注册用户时发送电子邮件时出错

从Google App脚本发送电子邮件时出错

使用Mandrill(php)发送电子邮件时出错

尝试从python发送电子邮件

当我尝试在Android中使用Mandrill SMTP发送电子邮件时出错

通过Java Mail API发送电子邮件时出错?

从C#代码发送电子邮件时出错

在 Codeigniter 2 中发送电子邮件时出错

使用 Laravel 使用 Mailgun 发送电子邮件时出错

尝试向 aws count 发送电子邮件时,电子邮件帐户不存在

使用 nodejs 和 mailgun 发送电子邮件时出错

尝试在 python 3.8.3 中使用 smtplib 发送电子邮件时出错

使用 Nodemailer 发送电子邮件时出错

Python:尝试删除为发送电子邮件附件而生成的文件时出错

尝试在 asp.net 核心应用程序中使用 SmtpClient 发送电子邮件时出错

由于 smtp 在 Lambda (python) 中发送电子邮件时出错