1

I'm using this code to send mail with PowerShell:

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "user@gmail.com"
$Password = "pass"
$to = "mail"   
$subject = "TRans LOG"
$body = "TRANS log info"   
$message = New-Object System.Net.Mail.MailMessage
$message.Subject = $subject
$message.Body = $body
$message.To.Add($to)
#$message.Cc.Add($cc)
$message.From = $username   
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($message)
Write-Host "Mail Sent successfully using GMAIL"

But I get an error:

Exception calling "Send" with "1" argument(s): "The SMTP server requires a
secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required.
+ $smtp.Send($message)

What am I doing wrong?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    Try this:https://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage Seems to be the same problem. – Buttergipfeli May 15 '19 at 11:21
  • 2
    Possible duplicate of [Send mail via gmail with PowerShell V2's Send-MailMessage](https://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage) – Theo May 15 '19 at 11:25
  • I've seen this,but it gets the same error. –  May 15 '19 at 11:39
  • 3
    Please read the comment below the answer by [jens](https://stackoverflow.com/users/4071843/jens) _"There is some gmail configuration you must perform"_ – Theo May 15 '19 at 12:08
  • This answer (direct link)- https://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage#comment62088150_1252335 – xxxvodnikxxx May 15 '19 at 13:29
  • I manage to get it to run. Now I have a different problem: I want to use external vairable to show error, but + or '''' double quations don't work: SET @ErrMsg = ERROR_MESSAGE() + ' - ' + XACT_STATE(); exec xp_cmdshell 'powershell -file "C:\Schedule jobs\SendTransactionsFailerMail.ps1" -errorMsg "Error occured - check !"' This Error occured - check! should be replaced with ErrMsg . How? –  May 16 '19 at 07:05

0 Answers0