2015-03-13 158 views
0

我想通過使用powershell腳本的Gmail郵件服務器發送電子郵件。但我不能夠發送電子郵件..我用下面的腳本 -使用powershell發送電子郵件與gmail

$EmailFrom = "[email protected]" 

$EmailTo = "[email protected]" 

$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("bttpm", "Password"); 

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 
+0

你檢查了連接嗎? – 2015-03-13 10:08:14

+0

是的,我可以telnet端口 – 2015-03-13 10:15:34

回答

2

這爲我工作:

$SMTPServer = "smtp.gmail.com" 
$SMTPPort = "587" 
$Username = "[email protected]" 
$Password = "" 

$to = "[email protected]" 
$cc = "[email protected]" 
$subject = "Email Subject" 
$body = "Insert body text here" 
$attachment = "C:\test.txt" 

$message = New-Object System.Net.Mail.MailMessage 
$message.subject = $subject 
$message.body = $body 
$message.to.add($to) 
$message.cc.add($cc) 
$message.from = $username 
$message.attachments.add($attachment) 

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); 
$smtp.EnableSSL = $true 
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); 
$smtp.send($message) 
write-host "Mail Sent" 

但是,您可能會收到此錯誤信息:

​​

這是因爲Gmail的默認安全設置會阻止連接,正如Google提供的自動消息所示。因此,請按照郵件中的說明進行操作,並啓用「訪問不太安全的應用程序」。您自擔風險。 :)

這裏更多的信息:http://petermorrissey.blogspot.ro/2013/01/sending-smtp-emails-with-powershell.html

+0

我使用的是PowerShell版本4。 運行此腳本後,我得到了以下錯誤----- 異常調用帶有「1」參數的「發送」:「SMTP服務器需要安全連接或客戶端未通過身份驗證,服務器響應爲:5.5 .1 需要身份驗證。瞭解更多信息:「在線:23 char:1 + $ smtp.send($ message) + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:SmtpException – 2015-03-13 12:13:30

+0

@kamleshbhatt編輯我的回答 – 2015-03-13 12:24:47

+0

感謝它的工作:) – 2015-03-13 12:50:06

1

發送電子郵件附件中使用PowerShell -

$EmailTo = "[email protected]" // [email protected] 
    $EmailFrom = "[email protected]" //[email protected] 
    $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("[email protected]", "xxxxxxxx"); // xxxxxx-password 
    $SMTPClient.Send($SMTPMessage) 
1

「SMTP服務器要求安全連接或客戶端未通過身份驗證服務器響應:5.5.1需要驗證。「

我最近遇到了這個問題,試圖通過我的Gmail帳戶自動化郵件。我不喜歡允許「訪問不太安全的應用程序」的選項。我想要前瞻性思維安全。所以我繼續了我的搜索。

我的解決方案是啓用「兩步」驗證。這提供了一個稍微更安全的解決方案,因爲它爲您的腳本訪問您的帳戶提供了備用密碼。

登錄使用App密碼:
https://support.google.com/accounts/answer/185833

Stefan的鏈接也有這個解決辦法,但它埋在評論和我最初沒有發現,直到後,我發現它在我的通過搜索我的Gmail帳戶。這就是我在這裏發佈的原因。