Authentication failure when sending message - email

I am trying to send a message with PowerShell but I can not send the message. The error says:
The server SMTP requires a connection safe or the user didn't authenticate.
I don't know where is my error and I don't know what more I can try.
This is my code:
$EmailTo = "prueba#gmail.com"
$EmailFrom = "pacopepe#gmail.com"
$Subject = "Test"
$Body = "Test Body"
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("pacopepe#gmail.com", "pacopaco");
$SMTPClient.Send($SMTPMessage)
Note: In credentials, I put $emailfrom and the password of his mail.

Related

PowerShell - Send-MailMessage with AWS SES - Intermittent

I've been using the below script to send email and sometimes it works, sometimes it does not. I'm not sure exactly why it is intermittent and with this error:
"ERROR: The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"
This is the script I'm using:
$AWS_ACCESS_KEY = "KEYYYYYYYYYY"
$AWS_SECRET_KEY = "SECRET_KEYYYYYYYYYYYYYY"
$SECURE_KEY = $(ConvertTo-SecureString -AsPlainText -String $AWS_SECRET_KEY -Force)
$creds = $(New-Object System.Management.Automation.PSCredential ($AWS_ACCESS_KEY, $SECURE_KEY))
$smtp = "email-smtp.us-east-1.amazonaws.com"
$from = "noreply#domain.com"
$to = "to_you#domain.com"
$bcc = "its_me#domain.com"
$subject = "Hello There"
$body = "Hey Friend!<br><br>"
$body += "Body<br><br>"
$body += "Regards,<br>"
$body += "Bye<br>"
#Sending email
Sleep 2
Send-MailMessage -From $from -To $to -Bcc $bcc -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtp -Credential $creds -UseSsl -Port 587
What could be the issue? Has anyone seen this before? Any log in AWS console that I could look at?
Thanks!
I also faced a similar issue managed to resolve with the below codes.
$smtpServer = "email-smtp.us-east-1.amazonaws.com"
$smtpPort = 587
$username = "************"
$password = "***********************"
$from = "alerts#test.com"
$to = "recipient#domain.com"
$subject = "Amazon SES Test Email"
$body = "Body Amazon SES Test Email"
$cc = "xyz#domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
$msg = new-object Net.Mail.MailMessage
$msg.From = $from
$msg.To.Add($to)
$msg.CC.Add($cc)
$msg.Subject = $subject
$smtp.Send($msg)
$msg.Body = $body

I am trying to send mail via powershell using send-mailmessage which is throwing an exception

I tried the following
send-mailmessage -subject "hi" -to "abc#abc.com" -from "abc#abc.com" -UseSsl -SmtpServer "smtp.office365.com" -port 25 -Credential (Get-Credential)
I even tried with different ports but it is throwing the below exception
send-mailmessage : The SMTP server requires a secure connection or the client was
not authenticated. The server response was: 5.7.57 SMTP; Client was not
authenticated to send anonymous mail during MAIL FROM
[XXXXXXXXXXX.YYYYYY.PROD.OUTLOOK.COM]
then I even tried System.Net.Mail.SmtpClient:SmtpClient method but even this is throwing the same error
Try it like this :
$emailSmtpServer = "mail"
$emailSmtpServerPort = "587"
$emailSmtpUser = "user"
$emailSmtpPass = "P#ssw0rd"
$emailFrom = "from"
$emailTo = "to"
$emailcc="CC"
$emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
$emailMessage.cc.add($emailcc)
$emailMessage.Subject = "subject"
#$emailMessage.IsBodyHtml = $true #true or false depends
$emailMessage.Body = "body"
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $False
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
$SMTPClient.Send( $emailMessage )

Unable to send email using Gmail SMTP in Powershell. Error: smtpServer requires secure connection or client was not authenticated. How to fix? [duplicate]

i am trying to send emails though gmail smtp server using powershell script. but i am not able to send emails .. i am using below script-
$EmailFrom = "bttpm#gmail.com"
$EmailTo = "kamlesh.bhatt#domain.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("bttpm", "Password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
This worked for me:
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "username#gmail.com"
$Password = ""
$to = "user1#domain.com"
$cc = "user2#domain.com"
$subject = "Email Subject"
$body = "Insert body text here"
$attachment = "C:\test.txt"
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($to)
$message.cc.add($cc)
$message.from = $username
$message.attachments.add($attachment)
$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"
However, you may get this error message:
"The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required. "
This is because the default security settings of Gmail block the connection, as suggested by the auto message from Google. So just follow the instructions in the message and enable "Access for less secure apps". At your own risk. :)
More info here: http://petermorrissey.blogspot.ro/2013/01/sending-smtp-emails-with-powershell.html
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."
I recently ran into this issue with attempting to automate messages through my Gmail account. I do not like the option of allowing "access for less secure apps". I want forward thinking security. So I continued my search.
My solution was to enable "2-Step" verification. This provides a slightly more secure solution as it provides an alternate password for your script to access your account.
Sign in using App Passwords:
https://support.google.com/accounts/answer/185833
Stefan's link also has this solution, but it's buried in the comments and I didn't originally find it there until after I found it on my own through searching my Gmail account. That's why I am posting it here.
Send email with attachment using powershell -
$EmailTo = "udit043.ur#gmail.com" // abc#domain.com
$EmailFrom = "udit821#gmail.com" //xyz#gmail.com
$Subject = "zx" //subject
$Body = "Test Body" //body of message
$SMTPServer = "smtp.gmail.com"
$filenameAndPath = "G:\abc.jpg" //attachment
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("udit821#gmail.com", "xxxxxxxx"); // xxxxxx-password
$SMTPClient.Send($SMTPMessage)

Adding a CC to a email being sent from powershell

I'm sending a report out via email in Powershell and I would like to add a CC, but everything I've tried thusfar isnt working.
heres my sendmail code...
$ExportedReport = "$WorkFolder\InactiveUserReport.csv"
$EmailTo = "myemail#domain.com"
$EmailFrom = "reportsemail#domain.com"
$Subject = "REPORT BLAH BLAH"
$Body = "blah blah blah."
$SMTPServer = "mysmtpserver"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attachment = New-Object System.Net.Mail.Attachment($ExportedReport)
$SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("myusername", "mypassword");
$SMTPClient.Send($SMTPMessage)
Any help on how I can modify this to add a CC would be wonderful!
Your MailMessage has a CC property.
Use the following:
$SMTPMessage.cc.Add("cc.address#domain.tld")

VMware PowerShell scripting - automated gmail

This works, but...
$EmailFrom = "vsphere#nasa.gov"
$EmailTo = "userwithOLDsnapshot#nasa.gov"
$Subject = "Notification"
$Body = "this is a notification for your snapshot"
$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)
Is there anyway of doing this with a credential store? Embedding creds in scripts in clear text is a no no.
The following will create an encrypted string. You can save this output to some file and read it in later:
$securePWord=Read-Host "Enter some password" -AsSecureString
This will convert a SecureString into usable text:
$pWord=[System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($securePWord))