我想弄清楚如何使用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)?
一些附加限制
- 我想这是非交互式的,所以
get-credential
将无法正常工作 - 用户帐户是不是在Gmail的域名,但注册域名的谷歌应用程序
- 对于这个问题,我只对感兴趣的
Send-MailMessage
cmdlet时,通过正常的.NET API发送邮件是很好理解的。
Answer 1:
刚刚发现这个问题..这里是我的PowerShell的发送,样品MAILMESSAGE的Gmail ..测试和工作的解决方案:
$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)
只要改变$ EmailTo,并在$ SMTPClient.Credentials用户名/密码..不包括@ gmail.com在您的用户名..
也许这是跨越这个问题未来其他帮助..
Answer 2:
这应该可以解决你的问题
$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
Answer 3:
我不知道,你可以更改端口号与发送,因为MAILMESSAGE上的Gmail端口587的工作无论如何,这里是如何通过Gmail发送电子邮件使用.NET SmtpClient:
$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')
Answer 4:
我只是有同样的问题,遇到这样的帖子。 它实际上帮助我得到它与天然发送-运行MAILMESSAGE命令让这里是我的代码:
$cred = Get-Credential
Send-MailMessage ....... -SmtpServer "smtp.gmail.com" -UseSsl -Credential $cred -Port 587
然而,为了有Gmail允许我使用SMTP服务器,我不得不登录到我的Gmail帐户,在这个环节https://www.google.com/settings/security设置为“不够安全的应用访问” “已启用”。 后来终于它的工作!
你好马尔科
Answer 5:
我用基督教的2月12日解决方案,我也刚开始学习的PowerShell。 至于附件,我是用GET-会员学习它是如何工作和两个定义打探发现,发送()已...第二个定义需要System.Net.Mail.MailMessage对象,它允许附件和很多更强大和有用的功能,如抄送和密送。 下面是一个带有附件(与他上面的例子可以混合)的例子:
# 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)
请享用!
Answer 6:
这是一个非常晚的日期在这里附和,但也许这可以帮助别人。
我真的很新的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值的特定用户和通。
Answer 7:
经过多次试验和长期寻求解决办法。 我发现了一个功能性的和有趣的脚本代码在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
请享用
Answer 8:
在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
Answer 9:
发送电子邮件使用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)
Answer 10:
这里是
$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)
让我知道,如果它可以帮助你圣也使用send-也MAILMESSAGE在Www.techjunkie.tv对于这样也是我认为是更好的方式和纯粹使用
Answer 11:
我没有使用过的PowerShell V2发送-MAILMESSAGE,但我已经使用在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")
Answer 12:
我同意基督教Muggli的解决方案,虽然起初我还有斯科特温斯坦报告的错误。 如何你过去是:
无论是第一次登录到从机器这将运行在Gmail,使用指定的帐户。 (这是没有必要的任何谷歌站点添加到可信站点区域,即使Internet Explorer增强安全配置被启用。)
或者,在你的第一次尝试,你会得到错误,和您的Gmail ACCT会得到一个可疑的登录通知,所以按照他们的指示,以允许从该机器将运行以后登录。
Answer 13:
看看这个职位发送与Gmail附件的方式PowerShell的例子 ,这样你可以看到如何使用Gmail发送附件,让我知道,如果它可以帮助你斯科特www.techjunkie.tv
文章来源: Send mail via gmail with PowerShell V2's Send-MailMessage