使用PowerShell V2的Send-MailMessage通过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发送邮件很好理解。

刚刚发现这个问题..这里是我的PowerShell Send-MailMessage示例为Gmail ..经testing和工作的解决scheme:

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

也许这是其他遇到这个问题的帮助..

这应该解决您的问题

 $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

我不确定你可以使用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') 

我只是有同样的问题,并遇到这个职位。 它实际上帮助我使用本地Send-MailMessage命令运行它,这里是我的代码:

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

但是,为了使Gmail允许我使用SMTP服务器,我必须login到我的Gmail帐户,并在此链接https://www.google.com/settings/security设置“访问不太安全的应用程序”; “已启用”。 然后最终它的工作!

ciao marco

我使用了Christian的2月12日解决scheme,我也刚开始学习PowerShell。 就附件而言,我正在与Get-Member学习如何工作,并注意到Send()有两个定义…第二个定义需要一个System.Net.Mail.MailMessage对象,它允许附件和更强大和有用的function,如抄送和密件抄送。 这是一个有附件的例子(与上面的例子混合在一起):

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

请享用!

这是一个非常晚的日子,在这里可以帮助别人。

我真的是新来的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 eg -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值更改为您的特定用户并通过。

经过多次testing并长期寻求解决scheme。 我在http://www.powershellmagazine.com/2012/10/25/pstip-sending-emails-using-your-gmail-account/上find了一个function强大且有趣的脚本代码。;

 $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 

请享用

在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 

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

这里是

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

让我知道,如果它可以帮助你也使用发送邮件也在www.techjunkie.tv这种方式也是我认为是更好,纯净的使用

我没有使用PowerShell V2发送邮件消息,但是我已经使用V1中的System.Net.Mail.SMTPClient类将消息发送到gmail帐户进行演示。 这可能是矫枉过正,但运行我的Vista笔记本电脑上的SMTP服务器看到这个链接 ,如果你在一个企业,你将已经有一个邮件依赖服务器,这一步是没有必要的。 有一个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") 

我同意Christian Muggli的解决scheme,尽pipe起初我还是得到了Scott Weinstein报道的错误。 你怎么过去是:

只要首先从机器login到gmail,这将使用指定的帐户运行。 (即使启用了Internet Explorer增强安全configuration,也不需要将任何Google站点添加到“受信任的站点”区域。)

或者,当您第一次尝试时,您会收到错误信息,您的Gmail帐户将收到有关可疑login信息的通知,请按照其说明进行操作,以允许将来运行的计算机进行login。

看看这个post的方式来发送附件与gmail Powershell示例,以便您可以看到如何使用gmail发送附件让我知道,如果它可以帮助你出去斯科特A http://www.techjunkie.tv