2
votes

I have a bit of powershell to send an e-mail:

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

This works perfectly on every machine I've ever tried it on until today. I have one machine where I get this error message:

Exception calling "Send" with "4" argument(s): "Failure sending mail." At R:\sqlrestore\scripts\Utilities\Check-Lag.ps1:15 char:12 + $smtp.Send <<<< ($emailFrom, $emailTo, $subject, $body) + CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : DotNetMethodException

3
We'll need to know what values you are passing in as parameters please: $smtpServer, $emailFrom, $emailTo, $subject, and $body.Richard

3 Answers

4
votes

The error message is ambiguous. It turns out the issue is that the SMTP server was inaccessible because outbound SMTP traffic was not allowed.

3
votes

By the way, if you're using PowerShell 2.0 then you may want to try the Send-MailMessage cmdlet:

Send-MailMessage -SmtpServer YourServerName -From $emailFrom -To $emailTo -Subject $subject -Body $body
0
votes

I have just come across the same problem, turns out it was because of two factor authentication. I created a 'app specific password' in Google security settings and all works fine now.