SmtpClient.Send不适用于linux环境

时间:2017-10-09 06:09:33

标签: c# linux asp.net-core .net-core asp.net-core-2.0

以下代码在$Data=file_get_contents('php://input'); $Data=json_decode($Data, true); $Adress=$Data['adress']; $Sent=$Data['sent']; 环境中编写,适用于.net core 2,但不适用于the windows environment

the linux environment

例外:string host = "10.99.99.10"; int port = 25; string userName = "user@user.com"; 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);

InnerExcepiton:

Failure sending mail.

堆栈跟踪:

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)

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

这是一款控制台应用。 为什么不在linux环境下工作?

1 个答案:

答案 0 :(得分:1)

我认为这是一个代码错误,因为我有错误,但事实并非如此。当我在邮件服务器端给出relay authority时,我的问题就解决了。我认为它是在Windows环境中自动执行此操作。