ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

c# – SmtpClient.Send不适用于linux环境

2019-07-05 19:51:35  阅读:437  来源: 互联网

标签:c linux net-core asp-net-core asp-net-core-2-0


以下代码,在.net core 2环境中编写,适用于Windows环境,但不适用于Linux环境.

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:

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环境下工作?

解决方法:

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

标签:c,linux,net-core,asp-net-core,asp-net-core-2-0
来源: https://codeday.me/bug/20190705/1390173.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有