Send-MailMessage Error

I am new in powershell, i was trying to send a mail through powershell scripting and i am able to send only for gmail, ymail etc but not for private sector companies mail where i work like comanies mail domain is

I have SMTP server for those mails but it thows an error again and again

Exception calling “Send” with “4” argument(s): “Failure sending mail.”
At line:9 char:17

  • $SMTPClient.Send <<<< ($EmailFrom, $EmailTo, $Subject, $Body)
    • CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException
    • FullyQualifiedErrorId : DotNetMethodException

I was tried lots of methods but it not works.
the powershell worsion on my system(os: win 7) is 1.0
Also i tried to execute this script from server end(win server 2008 r2 and 2012)

$EmailFrom = ""
$EmailTo = ""
$Subject = "Notification from XYZ"
$Body = "this is a notification from XYZ Notifications…"
$SMTPServer = ""
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("", "************");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

So, you didn’t format your code for the forums, making it hard to read.

It looks like you are trying to use SMTP server It is most likely that the server doesn’t want to allow you to authenticate from a remote, unknown location. Most private SMTP servers have very strict rules about who can log on to send, and from where, because what you’re doing is exactly what illegal spammers do when they want to hijack a server and relay spam mail.

You should talk to the administrator of the SMTP server to see what their requirements are.

Thanks, for help Don.

I have one QQ.
I was trying automate some manual process on server. We have some logs that we need to monitor so sometimes we get some error and we miss them. So i was trying to make a script on powershell to trigger mail whenever we get an error in this logs.

Can you please suggest is there any way to do this through task schedular or anything else.
I tried with tasks schedular but the issue was it throws a mail whenever the script run it never check the script ran successfully or not.
Please suggest what can i do here appreciate for help!!!.

There should hopefully be more information about why the mail couldn’t be sent in your error object. “Failure sending mail” is incredibly unhelpful. After you run the script, try these commands to see if we can get a bit more info:

$error[0].Exception | Format-List * -Force
$error[0].Exception.InnerException | Format-List * -Force

Thanks I got it. the SMTP server name was wrong.
The issue resolved.