2017-06-15 102 views
2

我試圖從PowerShell腳本發送一封電子郵件,但我似乎無法讓它正確驗證我的SMTP服務器。爲什麼SmtpClient不能在我的powershell腳本中進行身份驗證?

$fromEmail = '[email protected]'; 
$toEmail = '[email protected]'; 
$mailUser = 'myUsername'; 
$mailPassword = 'myPassword'; 
$smtpServer = 'smtp.example.com'; 
$smtpPort = 587; 
$content = 'Email message content'; 
$subject = 'Email message subject'; 

$credentials = New-Object System.Net.NetworkCredential $mailUser, $mailPassword; 

$server = New-Object System.Net.Mail.SmtpClient $smtpServer, $smtpPort; 
$server.UseDefaultCredentials = $false; 
$server.EnableSSL = $true; 
$server.Credentials = $credentials 
$server.Send($fromEmail, $toEmail, $subject, $content); 

當我嘗試在PowerShell提示符下運行上述內容時,它會導致指定中繼訪問被拒絕的錯誤。

Exception calling "Send" with "4" argument(s): "Client does not have permission to submit mail to this server. The 
server response was: 4.7.1 <[email protected]>: Relay access denied" 
At line:1 char:1 
+ $server.Send($fromEmail, $toEmail, $subject, $content); 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : SmtpException 

如果我嘗試發送使用Thunderbird或PHP的Swiftmailer和相同的服務器和憑證設置的電子郵件,它工作正常。從查看後綴日誌看來,好像SmtpClient甚至不嘗試進行身份驗證,而只是試圖匿名發送郵件。

這是雷鳥取得了良好的日誌條目的樣子:

Jun 14 21:17:42 services postfix/submission/smtpd[16653]: connect from unknown[xxxx] 
Jun 14 21:17:43 services postfix/submission/smtpd[16653]: 59074F96E: client=unknown[xxxx], sasl_method=PLAIN, [email protected] 

而由SmtpClient該數據項的樣子:

Jun 14 22:11:16 services postfix/submission/smtpd[16824]: connect from unknown[xxxx] 
Jun 14 22:11:16 services postfix/submission/smtpd[16824]: NOQUEUE: reject: RCPT from unknown[xxxx]: 454 4.7.1 <[email protected]>: Relay access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<WTFv2> 

作爲測試,我也嘗試通過Gmail地址發送郵件使用我的gmail憑證,並且由於沒有驗證也失敗了。

Exception calling "Send" with "4" argument(s): "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 line:1 char:1 
+ $server.Send($fromEmail, $toEmail, $subject, $content); 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : SmtpException 

我在這裏錯過了什麼?爲什麼.net SmtpClient不發送驗證以允許郵件通過?

+0

可能重複的[如何將憑證傳遞給Send-MailMessage命令以發送電子郵件](https://stackoverflow.com/questions/12460950/how-to-pass-credentials-to-the-send-mailmessage-命令發送電子郵件) - 特別是你可能需要使用SSL,或者你需要把你的參數放在新對象的$ param中:$ credentials = New-Object System.Net.NetworkCredential($ mailUser,$ mailPassword) ' – TessellatingHeckler

+0

我啓用了SSL。添加括號沒有任何區別。 – kicken

回答

1

看來有導致此至少兩個問題問題。我最終嘗試了很多事情,我不完全確定最終解決問題的組合是什麼。

首先,我確定問題是由於我的服務器只提供PLAIN身份驗證方法。看起來SmtpClient即使在啓用TLS的連接上也不會使用PLAIN。如果SmtpClient不支持任何提供的身份驗證方法,那麼它顯然只是在沒有身份驗證的情況下繼續進行。

我試圖找到支持的身份驗證方法的列表,但無法執行。 This blog post導致我嘗試使用NTLM和LOGIN。有了NTLM,它試圖進行身份驗證,但無法成功。 LOGIN最終正常工作。

問題的第二部分,我不完全確定底層問題是什麼。啓用登錄最初並不起作用,因此我決定將代碼轉換爲C#並編譯並在那裏運行測試程序。這個計劃立即起作用。我回到了Powershell,再次嘗試了我的代碼,看看我是否能找到差異,並且它也突然開始工作。

以某種方式編譯並運行C#版本會導致Powershell版本也能正常工作。如果有人能解釋爲什麼這可能是我想知道的。

1

你可以試試這個方法,這是一個我使用Gmail時,我用System.Net.NetworkCredential的憑據(從我的C#代碼來):

$smtpServer = "smtp.gmail.com" 
$smtpServerport = "587" 
$emailSmtpUser = "[email protected]" 
$emailSmtpPass = "toto.123" 

$emailMessage = New-Object System.Net.Mail.MailMessage 
$emailMessage.From = "[email protected]" 
foreach ($addr in $to) 
{ 
    $emailMessage.To.Add($addr) 
} 
foreach ($attachment in $attachments) 
{ 
    $emailMessage.Attachments.Add($attachment) 
} 
$emailMessage.Subject = "The 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) 
+0

這也似乎不適用於我。我試了我的服務器和Gmail,都失敗了與問題相同的錯誤。 – kicken

相關問題