Send mail via gmail with PowerShell V2's Send-

2019-01-10 06:35发布

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

  1. I want this to be non-interactive, so get-credential won't work
  2. The user account isn't on the gmail domain, but an google apps registered domain
  3. For this question, I'm only interested in the Send-MailMessage cmdlet, sending mail via the normal .Net API is well understood.

13条回答
欢心
2楼-- · 2019-01-10 06:57

This should fix your problem

$credentials = new-object Management.Automation.PSCredential “mailserver@yourcompany.com”, (“password” | ConvertTo-SecureString -AsPlainText -Force)

Then use the credential in you call to Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl

查看更多
不美不萌又怎样
3楼-- · 2019-01-10 07:02

On a Windows 8.1 Machine I got Send-MailMessage to send an email with an attachment through GMail using the following script:

$EmFrom = "user@gmail.com"    
$username = "user@gmail.com"    
$pwd = "YOURPASSWORD"    
$EmTo = "recipient@theiremail.com"    
$Server = "smtp.gmail.com"    
$port = 587    
$Subj = "Test"    
$Bod = "Test 123"    
$Att = "c:\Filename.FileType"    
$securepwd = ConvertTo-SecureString $pwd -AsPlainText -Force    
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $securepwd    
Send-MailMessage -To $EmTo -From $EmFrom -Body $Bod -Subject $Subj -Attachments $Att -SmtpServer $Server -port $port -UseSsl  -Credential $cred
查看更多
来,给爷笑一个
4楼-- · 2019-01-10 07:03

Just found this question .. here's my PowerShell Send-MailMessage Sample for Gmail.. Tested and working solution:

$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)

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 ..

查看更多
▲ chillily
5楼-- · 2019-01-10 07:04

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

查看更多
太酷不给撩
6楼-- · 2019-01-10 07:05

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)
查看更多
Evening l夕情丶
7楼-- · 2019-01-10 07:08

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:

$cred = Get-Credential
Send-MailMessage ....... -SmtpServer "smtp.gmail.com" -UseSsl -Credential $cred -Port 587 

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

查看更多
登录 后发表回答