-1

See this question's first two answers (have tried both, both generate the below error):

Code (changed necessary pieces):

$EmailFrom = "notifications@somedomain.com"
$EmailTo = "me@earth.com" 
$Subject = "Notification from XYZ" 
$Body = "this is a notification from XYZ Notifications.." 
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Other way (changed necessary pieces):

$credentials = new-object Management.Automation.PSCredential “mailserver@yourcompany.com”, (“password” | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl

I'm getting this error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

On the first script, the port is explicitly specified, while it's not using PS's built in function (though I haven't had problems with this function in the past).

Thanks!

Community
  • 1
  • 1
jea392048
  • 27
  • 1
  • 3
  • I think [this](http://stackoverflow.com/questions/20906077/gmail-error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not) could be helpful. – Alexander Obersht Sep 26 '15 at 11:46

2 Answers2

1

Try this function to send an email via Gmail:

Function  batchsend-mailmessage ($to, $subject, $body, $attachments)
{
  $smtpServer = "smtp.gmail.com"
  $smtpServerport = "587"
  $emailSmtpUser = "TheBatch@gmail.com"
  $emailSmtpPass = "PasswordOfTheBatch"

  $emailMessage = New-Object System.Net.Mail.MailMessage
  $emailMessage.From = $emailSmtpUser
  foreach ($addr in $to)
  {
    $emailMessage.To.Add($addr)
  }
  foreach ($attachment in $attachments)
  {
    $emailMessage.Attachments.Add($attachment)
  }
  $emailMessage.Subject = $subject
  $emailMessage.IsBodyHtml = $true
  $emailMessage.Body = $body

  $smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer , $smtpServerport)
  $smtpClient.EnableSsl = $true
  $smtpClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser , $emailSmtpPass);

  $SMTPClient.Send( $emailMessage )
}

Like this :

batchsend-mailmessage -to $DesAdressesDest -subject $Subject -body $body -attachments $xlsFile
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
1

We send mail with Powershell through Office365. This is what we use (works perfectly). Remember that the user you authenticate with must have "send as"-permissions of the from-address:

$PSEmailServer = "smtp.office365.com"
$credentials = new-object Management.Automation.PSCredential “UserLogonName@domain.com”, (“password” | ConvertTo-SecureString -AsPlainText -Force)
$enc  = New-Object System.Text.utf8encoding
$from = "FromAddress"
$to = "ToAddress","ToAdress2"
$body = "Test"
$subject = "Test"

Send-MailMessage -port 587 -From $from -BodyAsHtml -Encoding $enc -To $to -Subject $subject -Body $body -UseSsl -Credential $credentials
Oggew
  • 366
  • 1
  • 9