title: Powershell Script to monitoring status and send email results
body: <p>I have a lot of websites to monitor their up/down status, possible errors, ping and the other things that I managed to get with a script. My idea is the following: This script will run with task scheduler, get the results and send us (from the SQA publications) an email. So, I managed to create the script with success, he gets all I need and generates a html file in the C: drive. My problem is that after I get the result the function which sends the email isn't sending the email. I don't get any error messages, debug is fine, SMTP and all configurations are correct. But it won't send the email with the html file attached!</p>
  
  <p>The code is this:</p>
  
  <pre><code>$URLListFile = "C:\URLList.txt"  
  $URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue 
    $Result = @() 
  
  
    Foreach($Uri in $URLList) { 
    $time = try{ 
    $request = $null 
  
    $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri } 
    $result1.TotalMilliSeconds 
    }  
    catch 
    { 
  
     $request = $_.Exception.Response 
     $time = -1 
    }   
    $result += [PSCustomObject] @{ 
    Time = Get-Date; 
    Uri = $uri; 
    StatusCode = [int] $request.StatusCode; 
    StatusDescription = $request.StatusDescription; 
    ResponseLength = $request.RawContentLength; 
    TimeTaken =  $time;  
    } 
  
  } 
  
  if($result -ne $null) 
  { 
      $Outputreport = "&lt;HTML&gt;&lt;TITLE&gt;Website Report Status&lt;/TITLE&gt;&lt;BODY background-color:peachpuff&gt;&lt;font color =""#99000"" face=""Microsoft Tai le""&gt;&lt;H2&gt; Website Report Status &lt;/H2&gt;&lt;/font&gt;&lt;Table border=1 cellpadding=0 cellspacing=0&gt;&lt;TR bgcolor=gray align=center&gt;&lt;TD&gt;&lt;B&gt;URL&lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt; Code &lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt; Status &lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt; Duration &lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;&lt;B&gt; MS (Ping) &lt;/B&gt;&lt;/TD&lt;/TR&gt;" 
      Foreach($Entry in $Result) 
      { 
          if($Entry.StatusCode -ne "200") 
          { 
             $Outputreport += "&lt;TR bgcolor=red&gt;" 
          } 
          else 
          { 
              $Outputreport += "&lt;TR&gt;" 
          } 
          $Outputreport += "&lt;TD&gt;$($Entry.uri)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.StatusCode)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.StatusDescription)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.ResponseLength)&lt;/TD&gt;&lt;TD align=center&gt;$($Entry.timetaken)&lt;/TD&gt;&lt;/TR&gt;" 
      } 
      $Outputreport += "&lt;/Table&gt;&lt;/BODY&gt;&lt;/HTML&gt;" 
  } 
  
  $Outputreport | out-file C:\URLReport.htm 
  Invoke-Expression C:\URLReport.htm   
  
  
  $EmailFrom = "noreply@domain.com"
  $EmailTo = "destinyemail@domain.com"
  $EmailSubject = "URL Report"
  $emailbody = " body message "
  $SMTPServer = "smtpserver.company.com"
  
  $emailattachment = "C:\URLReport.htm"
  
  function send_email {
  $mailmessage = New-Object system.net.mail.mailmessage
  $mailmessage.from = ($emailfrom)
  $mailmessage.To.add($emailto)
  $mailmessage.Subject = $emailsubject
  $mailmessage.Body = $emailbody
  
  $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'html')
    $mailmessage.Attachments.Add($attachment)
  
  
  $mailmessage.IsBodyHTML = $true
  $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
  $SMTPClient.Send($mailmessage)
  }
  </code></pre>
  
  <p><strong>EDIT4:</strong> > ($SmtpServer, 587) Beeing " 587 " the port that our smtp server uses.</p>