title: Sending mail from powershell
body: <p>I have a task to send some 500 e-mails and I'm exploring the best (easiest) way to do that. I came across some Powershell examples but I can't make it work. I see there are 2 approaches: create a script and then call it from powershell command line, or do it directly from command line. The second approach would be better for me because it would be easier to generate those command lines for 500 e-mails. I found some examples and come to this:</p>
  
  <pre><code>$smtp = New-Object Net.Mail.SmtpClient("smtp.gmail.com", "465")
  $Smtp.EnableSsl = $true
  $Smtp.Credentials = New-Object System.Net.NetworkCredential("myusername","mypass")
  $Smtp.Attachments.Add("C:\bla.txt")
  $smtp.Send("myusername@gmail.com","someusername@gmail.com","Test Email","This is a test")
  </code></pre>
  
  <p>For the first attempt I'm trying to send mail from gmail...</p>
  
  <p>I'm not good in Powershell (better to say, don't know it at all but have some general knowledge about command line from command prompt/MS-DOS which is more familiar to me).</p>
  
  <p>Also will, mail server allow sending 500 e-mails like this or it will recognize it as some attack, spam, whatever...?</p>
  
  <p>I would appreciate any help.
  Thanks! </p>
  
  <p>EDIT:</p>
  
  <p>I have also tried this:</p>
  
  <pre><code>$From = "YourEmail@gmail.com"
  $To = "AnotherEmail@YourDomain.com"
  $Cc = "YourBoss@YourDomain.com"
  $Subject = "Email Subject"
  $Body = "Insert body text here"
  $SMTPServer = "smtp.gmail.com"
  $SMTPPort = "465"
  
  Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential)
  </code></pre>
  
  <p>For $From, $To, $Cc I have put my real e-mail address</p>
  
  <p>Error I got is this:</p>
  
  <pre><code> Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
  At line:9 char:1
  + Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body  ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
      + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
  </code></pre>