使用PowerShell V2的Send-MailMessage通过Gmail发送邮件

时间:2009-08-09 21:28:03

标签: security powershell gmail

我正在尝试弄清楚如何将PowerShell V2 Send-MailMessage与gmail一起使用。

这是我到目前为止所拥有的。

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

我收到以下错误

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

我做错了什么,或者Send-MailMessage还没有完全出炉(我正在使用CTP 3)?

一些额外的限制

  1. 我希望这是非交互式的,因此get-credential无效
  2. 用户帐户不在Gmail域名上,而是谷歌应用程序注册域名
  3. 对于这个问题,我只对Send-MailMessage cmdlet感兴趣,通过正常的.Net API发送邮件是很好理解的。

14 个答案:

答案 0 :(得分:45)

刚发现这个问题..这是我的用于Gmail的PowerShell Send-MailMessage示例。经过测试和运行的解决方案:

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

只需在$ SMTPClient.Credentials中更改$ EmailTo和用户名/密码..不要在您的用户名中包含@ gmail.com ..

也许这可以帮助其他人遇到这个问题。

答案 1 :(得分:13)

这应该可以解决您的问题

$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

时使用凭证

答案 2 :(得分:7)

我不确定您是否可以使用Send-MailMessage更改端口号,因为gmail可以在端口587上运行。无论如何,这里是如何使用.NET SmtpClient通过gmail发送电子邮件:

$smtpClient = new-object system.net.mail.smtpClient 
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID) 
$smtpClient.Send('GmailUserID@gmail.com','yourself@somewhere.com','test subject', 'test message')

答案 3 :(得分:7)

我遇到了同样的问题并遇到了这篇文章。它实际上帮助我使用本机Send-MailMessage命令运行它,这是我的代码:

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

但是,为了让Gmail允许我使用SMTP服务器,我必须登录我的Gmail帐户,并在此链接https://www.google.com/settings/security下设置&#34;访问不太安全的应用程序&#34;到&#34;启用&#34;。最后它确实有效!!

侨 马可

答案 4 :(得分:4)

我使用了Christian的2月12日解决方案,我也刚刚开始学习PowerShell。至于附件,我正在寻找Get-Member学习它是如何工作的,并注意到Send()有两个定义......第二个定义采用System.Net.Mail.MailMessage对象,允许附件和更强大的功能和Cc和Bcc等有用的功能。这是一个附件的例子(与上面的例子混合):

# append to Christian's code above --^
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
$emailMessage.To.Add($EmailTo)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\Test.txt")
$SMTPClient.Send($emailMessage)

享受!

答案 5 :(得分:3)

这是一个非常晚的日期,可以在这里讨论,但也许这可以帮助其他人。

我是PowerShell的新手,我正在寻找PS的gmailing。我接受了你们上面所做的事情,并对其进行了一些修改,并提出了一个脚本,该脚本将在添加附件之前检查附件,并且还可以获取一组收件人。我稍后会添加一些错误检查和更多内容,但我认为在这里发布可能已经足够好了(并且足够基本)。

## Send-Gmail.ps1 - Send a gmail message
## By Rodney Fisk - xizdaqrian@gmail.com
## 2 / 13 / 2011

# Get command line arguments to fill in the fields
# Must be the first statement in the script
param(
    [Parameter(Mandatory = $true,
                    Position = 0,
                    ValueFromPipelineByPropertyName = $true)]
    [Alias('From')] # This is the name of the parameter e.g. -From user@mail.com
    [String]$EmailFrom, # This is the value [Don't forget the comma at the end!]

    [Parameter(Mandatory = $true,
                    Position = 1,
                    ValueFromPipelineByPropertyName = $true)]
    [Alias('To')]
    [String[]]$Arry_EmailTo,

    [Parameter(Mandatory = $true,
                    Position = 2,
                    ValueFromPipelineByPropertyName = $true)]
    [Alias( 'Subj' )]
    [String]$EmailSubj,

    [Parameter(Mandatory = $true,
                    Position = 3,
                    ValueFromPipelineByPropertyName = $true)]
    [Alias( 'Body' )]
    [String]$EmailBody,

    [Parameter(Mandatory = $false,
                    Position = 4,
                    ValueFromPipelineByPropertyName = $true)]
    [Alias( 'Attachment' )]
    [String[]]$Arry_EmailAttachments

)

# From Christian @ StackOverflow.com
$SMTPServer = "smtp.gmail.com" 
$SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, 587 )  
$SMTPClient.EnableSSL = $true 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( "GMAIL_USERNAME", "GMAIL_PASSWORD" ); 

# From Core @ StackOverflow.com
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
foreach ( $recipient in $Arry_EmailTo )
{
    $emailMessage.To.Add( $recipient )
}
$emailMessage.Subject = $EmailSubj
$emailMessage.Body = $EmailBody
# Do we have any attachments?
# If yes, then add them, if not, do nothing
if ( $Arry_EmailAttachments.Count -ne $NULL ) 
{
    $emailMessage.Attachments.Add()
}
$SMTPClient.Send( $emailMessage )

当然,将GMAIL_USERNAME和GMAIL_PASSWORD值更改为您的特定用户并传递。

答案 6 :(得分:3)

经过多次测试和长期寻找解决方案。我在http://www.powershellmagazine.com/2012/10/25/pstip-sending-emails-using-your-gmail-account/找到了功能性和有趣的脚本代码。

$param = @{
    SmtpServer = 'smtp.gmail.com'
    Port = 587
    UseSsl = $true
    Credential  = 'you@gmail.com'
    From = 'you@gmail.com'
    To = 'someone@somewhere.com'
    Subject = 'Sending emails through Gmail with Send-MailMessage'
    Body = "Check out the PowerShellMagazine.com website!"
    Attachments = 'D:\articles.csv'
}

Send-MailMessage @param

享受

答案 7 :(得分:2)

在Windows 8.1计算机上,我Send-MailMessage使用以下脚本通过GMail发送带附件的电子邮件:

$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

答案 8 :(得分:2)

使用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)

答案 9 :(得分:1)

这是

$filename = “c:\scripts_scott\test9999.xls”
$smtpserver = “smtp.gmail.com”
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer )
$smtp.EnableSsl = $True
$smtp.Credentials = New-Object System.Net.NetworkCredential(“username”, “password_here”); # Put username without the @GMAIL.com or – @gmail.com
$msg.From = “username@gmail.com”
$msg.To.Add(”boss@job.com”)
$msg.Subject = “Monthly Report”
$msg.Body = “Good MorningATTACHED”
$msg.Attachments.Add($att)
$smtp.Send($msg)

让我知道它是否有助于你San还使用send-mailmessage Www.techjunkie.tv 对于那种方式,我认为使用

更好更纯粹

答案 10 :(得分:0)

我没有使用过PowerShell V2 send-mailmessage,但我在V1中使用了System.Net.Mail.SMTPClient类将消息发送到gmail帐户以进行演示。这可能有点矫枉过正,但在我的Vista笔记本电脑上运行smtp服务器,请参阅this link,如果您在企业中,您已经拥有邮件依赖服务器,则无需执行此步骤。拥有一个smtp服务器,我可以使用以下代码向我的Gmail帐户发送电子邮件:

$smtpmail = [System.Net.Mail.SMTPClient]("127.0.0.1")
$smtpmail.Send("myacct@gmail.com", "myacct@gmail.com", "Test Message", "Message via local smtp")

答案 11 :(得分:0)

我同意Christian Muggli的解决方案,尽管起初我仍然得到了Scott Weinstein报告的错误。你是如何克服的:

首先使用指定的帐户从计算机上首次登录gmail。 (即使启用了Internet Explorer增强安全配置,也无需将任何Google站点添加到“受信任的站点”区域。)

或者,在您第一次尝试时,您将收到错误,并且您的gmail帐户会收到有关可疑登录的通知,因此请按照他们的说明允许将来运行的计算机登录。

答案 12 :(得分:0)

让这些脚本中的任何一个在Powershell中发送邮件时都遇到了很大的问题。原来,您需要为您的gmail帐户创建一个应用密码才能在脚本中进行身份验证。现在它可以完美地工作了!

答案 13 :(得分:-2)

查看此帖子,了解使用gmail Powershell Examples发送附件的方式,以便您了解如何使用gmail发送附件 如果它可以帮助你,请告诉我 斯科特A. www.techjunkie.tv