2015-09-26 81 views
-1

See this question's first two answers(曾經嘗試都,都產生下面的錯誤):PowerShell中發送電子郵件

代碼(根據需要更改件):

$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("username", "password"); 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 

其他方式(根據需要更改件):

$credentials = new-object Management.Automation.PSCredential 「[email protected]」, (「password」 | ConvertTo-SecureString -AsPlainText -Force) 
Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl 

我得到這個錯誤:

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

在第一個腳本中,端口是明確指定的,而不是使用PS的內置函數(儘管過去我沒有遇到這個函數的問題)。

謝謝!

+0

我認爲[這](http://stackoverflow.com/questions/20906077/gmail-error-the -smtp-server-requires-a-secure-connection-or-the-client-was-not)可能會有幫助。 –

回答

0

試試這個功能通過Gmail發送電子郵件:

Function batchsend-mailmessage ($to, $subject, $body, $attachments) 
{ 
    $smtpServer = "smtp.gmail.com" 
    $smtpServerport = "587" 
    $emailSmtpUser = "[email protected]" 
    $emailSmtpPass = "PasswordOfTheBatch" 

    $emailMessage = New-Object System.Net.Mail.MailMessage 
    $emailMessage.From = $emailSmtpUser 
    foreach ($addr in $to) 
    { 
    $emailMessage.To.Add($addr) 
    } 
    foreach ($attachment in $attachments) 
    { 
    $emailMessage.Attachments.Add($attachment) 
    } 
    $emailMessage.Subject = $subject 
    $emailMessage.IsBodyHtml = $true 
    $emailMessage.Body = $body 

    $smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer , $smtpServerport) 
    $smtpClient.EnableSsl = $true 
    $smtpClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser , $emailSmtpPass); 

    $SMTPClient.Send($emailMessage) 
} 

像這樣:

batchsend-mailmessage -to $DesAdressesDest -subject $Subject -body $body -attachments $xlsFile 
0

我們使用PowerShell通過Office365發送郵件。這是我們使用的(完美的作品)。請記住,你與驗證,用戶必須具有「發送爲」從地址的-permissions:

$PSEmailServer = "smtp.office365.com" 
$credentials = new-object Management.Automation.PSCredential 「[email protected]」, (「password」 | ConvertTo-SecureString -AsPlainText -Force) 
$enc = New-Object System.Text.utf8encoding 
$from = "FromAddress" 
$to = "ToAddress","ToAdress2" 
$body = "Test" 
$subject = "Test" 

Send-MailMessage -port 587 -From $from -BodyAsHtml -Encoding $enc -To $to -Subject $subject -Body $body -UseSsl -Credential $credentials