Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SmtpClient.Send not working on linux environment

The following code, written in the .net core 2 environment, works in the windows environment but not in the linux environment.

string host = "10.99.99.10";
int port = 25;
string userName = "[email protected]";
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);

Exception: Failure sending mail.

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)

StackTrace:

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

This is a console app. Why is not working in linux environment?

like image 899
Cer Avatar asked Nov 07 '22 15:11

Cer


1 Answers

I thought it was a code error because of the error I had, but it was not. When I gave the relay authority on the mail server side my problem was solved. I think it is doing this automatically in windows environment.

Edit: Add ip address and allow anonymous user in Receive connectors / Exchange server

like image 121
Cer Avatar answered Nov 14 '22 22:11

Cer