微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何在Linux环境下获取System.Net.Mail.Smtp?

如何解决我如何在Linux环境下获取System.Net.Mail.Smtp?

因此,我正在.NET Core 3.1上开发一个Web API,该Web API可以在linux服务器(CentOS 8)上运行,并且我需要从中发送交易电子邮件

但是,当我第一次运行该应用程序时,当我的API尝试通过我公司的SMTP服务器发送电子邮件时,它抛出了一个错误,但没什么好说的(在Linux环境下的noob视图中)。

我创建了一个控制台应用程序来复制电子邮件发送例程的基本代码

 class Program
    {
        static async Task Main(string[] args)
        {
            using var smtpClient = new SmtpClient("myhost.com.br")
            {
                UseDefaultCredentials = false,DeliveryMethod = SmtpDeliveryMethod.Network,Credentials = new NetworkCredential("username","password"),EnableSsl = false,Port = 587
            };

            using var mailMessage = new MailMessage();

            mailMessage.To.Add("my_email@email.com");
            mailMessage.From = new MailAddress("my_address@email.com","My nice display name");
            mailMessage.Subject = "Teste";
            mailMessage.Priority = MailPriority.High;
            mailMessage.IsBodyHtml = true;
            mailMessage.Body = "TEST";

            try
            {
                await smtpClient.SendMailAsync(mailMessage);
            }
            catch
            {
                throw;
            }
        }
    }

抛出此异常:


Unhandled exception. System.Net.Mail.SmtpException: Failure sending mail.
 ---> System.ComponentModel.Win32Exception (0x80090020): GSSAPI operation Failed with error - Unspecified GSS failure.  Minor code may provide more information (C
annot find KDC for realm "username").
   at System.Net.Security.NegotiateStreamPal.AcquireCredentialsHandle(String package,Boolean isServer,NetworkCredential credential) in /_/src/Common/src/System/
Net/Security/NegotiateStreamPal.Unix.cs:line 510
   at System.Net.NTAuthentication.Initialize(Boolean isServer,String package,NetworkCredential credential,String spn,ContextFlagsPal requestedContextFlags,Ch
annelBinding channelBinding) in /_/src/Common/src/System/Net/NTAuthentication.Common.cs:line 125
   at System.Net.Mail.SmtpNegotiateAuthenticationModule.Authenticate(String challenge,Object sessionCookie,ChannelBind
ing channelBindingToken) in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpNegotiateAuthenticationModule.cs:line 34
   at System.Net.Mail.SmtpConnection.SetContextAndTryAuthenticate(ISmtpAuthenticationModule module,ContextAwareResult context) in /
_/src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs:line 341
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.Authenticate() in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs:line 753
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.SendEHelloCallback(IAsyncResult result) in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpConn
ection.cs:line 633
--- End of stack trace from prevIoUs location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw(Exception source) in /_/src/System.Private.CoreLib/shared/System/Runtime/ExceptionServices/Exce
ptiondispatchInfo.cs:line 69
   at System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.End(IAsyncResult result) in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpConnection.cs:line 
424
   at System.Net.Mail.SmtpTransport.EndGetConnection(IAsyncResult result) in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpTransport.cs:line 177
   at System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result) in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs:line 986
   --- End of inner exception stack trace ---
   at asdsadasd.Program.Main(String[] args) in /tmp/asdsadasd/Program.cs:line 32
   at asdsadasd.Program.<Main>(String[] args)

相同的代码在Windows环境下运行良好。

这是我的dotnet --info:

SDK do .NET Core (refletindo qualquer global.json):
 Version:   3.1.107
 Commit:    617a3e03f4

Ambiente de runtime:
 OS Name:     centos
 OS Version:  8
 OS Platform: Linux
 RID:         centos.8-x64
 Base Path:   /usr/lib64/dotnet/sdk/3.1.107/

Host (useful for support):
  Version: 3.1.7
  Commit:  fcfdef8d6b

.NET Core SDKs installed:
  3.1.107 [/usr/lib64/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.7 [/usr/lib64/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.7 [/usr/lib64/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download   

此服务器已经允许通过SMTP服务器发送电子邮件

有什么想法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。