我正在尝试使用.net通过Powershell发送和发送电子邮件。
我遇到以下错误:
Exception calling "Send" with "4" argument(s): "Failure sending mail."
At line:1 char:1
+ $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException
使用以下脚本:
$EmailFrom = "myemail@gmail.com"
$EmailTo = "anotheremail@gmail.com"
$Subject = "test"
$Body = @"
test
"@
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myemail@gmail.com", "mypassword");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
什么可能导致此异常?谢谢!
答案 0 :(得分:0)
这有效:
$EmailTo = "myself@gmail.com"
$EmailFrom = "me@mydomain.com"
$Subject = "Test"
$Body = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$SMTPClient.Send($SMTPMessage)