2017-10-09 116 views
3

以下代碼在.net core 2環境中編寫,適用於the windows environment,但不適用於the linux environmentSmtpClient.Send不適用於Linux環境

string host = "10.99.99.10"; 
int port = 25; 
string userName = "[email protected]"; 
string password = "password"; 
string from = userName; 

var client = new SmtpClient 
{ 
    Host = host, 
    Port = port, 
    EnableSsl = false, 
    DeliveryMethod = SmtpDeliveryMethod.Network, 
    UseDefaultCredentials = false, 
    Credentials = new NetworkCredential(userName, password) 
}; 

MailMessage mailMessage = new MailMessage(); 
mailMessage.From = new MailAddress(from); 
mailMessage.To.Add(userName); 
mailMessage.Body = "This is test mail."; 
mailMessage.Subject = "Testing";     
client.Send(mailMessage); 

例外:Failure sending mail.

InnerExcepiton:

System.ComponentModel.Win32Exception (0x80004005): GSSAPI operation failed with error - An invalid status code was supplied (Unknown error). 
    at System.Net.Security.NegotiateStreamPal.AcquireCredentialsHandle(String package, Boolean isServer, NetworkCredential credential) 
    at System.Net.NTAuthentication.Initialize(Boolean isServer, String package, NetworkCredential credential, String spn, ContextFlagsPal requestedContextFlags, ChannelBinding channelBinding) 
    at System.Net.Mail.SmtpNtlmAuthenticationModule.Authenticate(String challenge, NetworkCredential credential, Object sessionCookie, String spn, ChannelBinding channelBindingToken) 
    at System.Net.Mail.SmtpConnection.SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, ContextAwareResult context) 
    at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) 
    at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) 
    at System.Net.Mail.SmtpClient.GetConnection() 
    at System.Net.Mail.SmtpClient.Send(MailMessage message) 

堆棧跟蹤:

at System.Net.Mail.SmtpClient.Send(MailMessage message) 
    at MyProject.Helper.Utils.SendMail() in C:\Test\MyProject\MyProject.Helper\Utils.cs:line 146 

Linux操作系統:Ubuntu 16.04.3 LTS

這是一個控制檯應用程序。 爲什麼不在linux環境下工作?

+0

您的SMTP服務器可能需要驗證。嘗試'telnet 10.99.99.10 25'並嘗試使用SMTP命令發送測試電子郵件。 http://www.samlogic.net/articles/smtp-commands-reference.htm –

+0

@SameerNaik Telnet正在工作(10.99.99.10 25) – Cer

+0

0x80004005錯誤代碼的原因似乎是一個損壞的安裝。我會嘗試重新安裝.net核心。 https://support.microsoft.com/en-gb/help/914232/you-may-receive-error-code-0x80004005-or-other-error-codes-when-you-tr –

回答

0

我以爲這是一個代碼錯誤,因爲我有錯誤,但事實並非如此。當我在郵件服務器端給relay authority解決問題時。我認爲這是在Windows環境中自動執行此操作。

+0

郵件服務器上的中繼權限究竟是什麼? –

相關問題