How can i get System.Net.Mail.Smtp work on linux enviroment?

0

So, i'm developing an Web API on .NET Core 3.1 that should run on a linux server (CentOS 8) and i need to send transactional e-mails from it.

But the first time i ran the app, when i my API tries to send an e-mail via my company SMTP server it throws an error with not much to say (in my noob view on linux enviroments).

I created an console app to replicate the basiscs of the email sending routine:

 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;
            }
        }
    }

That throws this exception:


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, NetworkCredential credential, Object sessionCookie, String spn, ChannelBind
ing channelBindingToken) in /_/src/System.Net.Mail/src/System/Net/Mail/SmtpNegotiateAuthenticationModule.cs:line 34
   at System.Net.Mail.SmtpConnection.SetContextAndTryAuthenticate(ISmtpAuthenticationModule module, NetworkCredential credential, 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)

The same code runs fine on windows enviroment.

Here is me 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   

This server already have permitions to send e-mails via the SMTP server.

Any thoughts?

c#
linux
.net-core
smtpclient
centos8
asked on Stack Overflow Oct 30, 2020 by MestreDosMagros

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0