I'm trying to figure out how to use PowerShell V2's Send-MailMessage
with gmail.
Here's what I have so far.
$ss = new-object Security.SecureString
foreach ($ch in "password".ToCharArray())
{
$ss.AppendChar($ch)
}
$cred = new-object Management.Automation.PSCredential "uid@domain.com", $ss
Send-MailMessage -SmtpServer smtp.gmail.com -UseSsl -Credential $cred -Body...
I get the following error
Send-MailMessage : 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
At foo.ps1:18 char:21
+ Send-MailMessage <<<< `
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Am I doing something wrong, or is Send-MailMessage
not fully baked yet (I'm on CTP 3)?
Some additional restrictions
- I want this to be non-interactive, so
get-credential
won't work - The user account isn't on the gmail domain, but an google apps registered domain
- For this question, I'm only interested in the
Send-MailMessage
cmdlet, sending mail via the normal .Net API is well understood.
This should fix your problem
Then use the credential in you call to
Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl
On a Windows 8.1 Machine I got
Send-MailMessage
to send an email with an attachment through GMail using the following script:Just found this question .. here's my PowerShell Send-MailMessage Sample for Gmail.. Tested and working solution:
Just change $EmailTo, and username/password in $SMTPClient.Credentials.. do not include @gmail.com in your username..
maybe this is a help for other coming across this question ..
check out this post for the way to send attachments with gmail Powershell Examples so that you can see how to send attachments using gmail Let me know if it helps you out Scott A www.techjunkie.tv
Send email with attachment using powershell -
i just had the same problem and run into this post. It actually helped me to get it running with the native Send-MailMessage command-let and here is my code:
However, in order to have Gmail allowing me to use the SMTP server, i had to login into my gmail account and under this link https://www.google.com/settings/security set the "Access for less secure apps" to "Enabled". Then finally it did work!!
ciao marco