runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Can not send email with SmtpClient on Ubuntu 18.4 LTS

Open shakeri opened this issue 8 years ago • 11 comments

I'm trying to send email using Google SMTP server but its throwing exception:

An unhandled exception occurred while processing the request.
Win32Exception: GSSAPI operation failed with error - An invalid status code was supplied (Unknown error).

System.Net.Security.NegotiateStreamPal.AcquireCredentialsHandle(string package, bool isServer, NetworkCredential credential)
SmtpException: Failure sending mail.

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

At Startup Class:

 services.AddScoped<SmtpClient>((provider) =>
            {
                var config = provider.GetRequiredService<IConfiguration>();
                return new SmtpClient()
                {
                    Host = config.GetValue<String>("MailService:Smtp"),
                    Port = config.GetValue<int>("MailService:Port"),
                    Credentials = new NetworkCredential(
                            config.GetValue<String>("MailService:Username"),
                            config.GetValue<String>("MailService:Password")
                        )
                };
            });

And in Controller:

        private readonly SmtpClient _smtpClient;

        public ValuesController(
            SmtpClient smtpClient
        )
        {
            _smtpClient = smtpClient;
        }

        [HttpGet]
        public async Task<IActionResult> Get()
        {
            await _smtpClient.SendMailAsync(new MailMessage(
                from: "[email protected]",
                to: "[email protected]",
                subject: "Test message subject",
                body: "Test message body"
                        ));
            return Ok();
        }

shakeri avatar Sep 01 '18 04:09 shakeri

Hello, it may not help, but can you please confirm this reproes on 2.1 (or master)? 2.0 is about to go out of support.

danmoseley avatar Sep 02 '18 05:09 danmoseley

@danmosemsft thanks for reply. I upgraded this project to version 2.1, but there is still a similar error:

An unhandled exception occurred while processing the request.
Win32Exception: GSSAPI operation failed with error - An invalid status code was supplied (Unknown error).

System.Net.Security.NegotiateStreamPal.AcquireCredentialsHandle(string package, bool isServer, NetworkCredential credential)
SmtpException: Failure sending mail.

shakeri avatar Sep 03 '18 10:09 shakeri

Thanks. Do you have a different machine to try? Not much to go on.

danmoseley avatar Sep 03 '18 15:09 danmoseley

I tested this project on Windows Server 2012 R2 on the same network and it worked without problems.

shakeri avatar Sep 04 '18 06:09 shakeri

I suspect a problem with configuration on Linux - similar to dotnet/runtime#25885 @tmds @wfurt any tips & tricks for troubleshooting?

karelz avatar Sep 04 '18 16:09 karelz

I would suggest packet capture with wireshark or tcpdump. I would also suggest to install gss-ntlmssp package (or verify that it is in place) Just some background @shakeri: Windows do have all the necessary parts needed for NTLM and Kerberos. On Unix, this varies based on distribution and configuration.

wfurt avatar Sep 04 '18 16:09 wfurt

@wfurt I installed gss-ntlmssp package and test again.It worked. Thanks.

shakeri avatar Sep 05 '18 18:09 shakeri

Closing as answered.

karelz avatar Sep 05 '18 18:09 karelz

@karelz I wonder if it would make sense for the exceptoin message to suggest installing it.

danmoseley avatar Sep 05 '18 19:09 danmoseley

we talk about it while back @danmosemsft. I think that would be good idea.

wfurt avatar Sep 05 '18 20:09 wfurt

we talk about it while back @danmosemsft. I think that would be good idea.

It's not a trivial problem to solve. The error message we get back when doing NTLM operations is not always specific enough to determine that the problem is configuration (i.e. gss-ntlmssp not installed).

davidsh avatar Sep 05 '18 20:09 davidsh