gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:38:11 26 4
gpt4 key购买 nike

以下代码,在.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);

异常:发送邮件失败。

内部异常:

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

最佳答案

因为我的错误,我以为是代码错误,其实不是。当我在邮件服务器端授予 relay authority 时,我的问题就解决了。我认为它会在 Windows 环境中自动执行此操作。

编辑:在接收连接器/Exchange 服务器中添加 IP 地址并允许匿名用户

关于c# - SmtpClient.Send 不适用于 linux 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46639787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com