SMTP – mail sending error from powershell script

 …this post was published on 2014 January 26.

Today working to a project , I’ve found an issue. I tried to set a scheduled  powershell script for email sendi​ng, as I wrote in a similar post

This is the code, but SURPRISE !

  <# prepare de details #>
   $EmailFrom = “your@email.address”
   $EmailTo = destination@email.address@email.address
   $EmailSubject = “Number of items in list: “+$my_list
   $emailbody = ” This list contain “+$list_items+” items”
   $SMTPServer = “SMTP server address”
   <# send the results #>
   $mailmessage = New-Object system.net.mail.mailmessage
   $mailmessage.from = $emailfrom
   $mailmessage.To.add($emailto)
   $mailmessage.Subject = $emailsubject
   $mailmessage.Body = $emailbody
   $mailmessage.IsBodyHTML = $true
   $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer)
   $SMTPClient.Send($mailmessage)

an error message appeared! This is the  error message:

Exception calling “Send” with “1” argument(s): “Failure sending mail.”
At line:17 char:20
+    $SMTPClient.Send <<<< ($mailmessage)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

The cause isn’t so clear. I fix it checking how the SMTP server is configured. I saw that my server’s IP address aren’t permitted to access the mail server.  So below are the steps to configure that issue.

 After these steps, it’s ok!

This entry was posted in email, Powershell. Bookmark the permalink.