MailKit.Net.Smtp.SmtpClient SMTP server does not support authentication
Asked Answered
B

2

9

I'd like to use MailKit to send an email through our Exchange server, using the credentials of the process.

Building up a System.Net.Mail.SmtpClient and NetworkCredential with domain/username/password works, but while using MailKit.Net.Smtp.SmtpClient and NetworkCredential does not work. Throw exception like

Exception Message :The SMTP server does not support authentication. Trace Message : at MailKit.Net.Smtp.SmtpClient.d__73.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken) at MailKit.MailService.Authenticate(ICredentials credentials, CancellationToken cancellationToken) at SMTP_EmailCheck.Program.SendMail_MailKit_WithDomain() in D:\Work\SMTP_EmailCheck\SMTP_EmailCheck\Program.cs:line 123

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var mailMessage = new MimeMessage();
            mailMessage.From.Add(new MailboxAddress(fromMailAddress));
            mailMessage.To.Add(new MailboxAddress(toMailAddress));
             mailMessage.Subject = "SendMail_MailKit_WithDomain";
            mailMessage.Body = new TextPart(TextFormat.Plain)
            {
                Text = "Hello"
            };

            using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
            {
                smtpClient.Connect("MailServer", 25, MailKit.Security.SecureSocketOptions.None);                   
                var creds = new NetworkCredential("UserName", "Password", "Domain");
                smtpClient.Authenticate(creds);                    
                smtpClient.Send(mailMessage);
                smtpClient.Disconnect(true);
            }

Thanks in advance

Bovine answered 19/3, 2021 at 7:49 Comment(3)
Check the server's settings. Do you need to use TLS? Port 25 is usually used for unencrypted connections. It may be disabled. Not sure though, if that would cause this exception.Qintar
Hard to say for me from here. If I were in your shoes, I'd go through every setting in the server. After all, what's a Server good for, that doesn't allow to use the service ... that doesn't make sense. My first suspicion was that TLS is being enforced. Now that you are sure that isn't the case, well ... Server settings, Firewall, some flag in MailKit ... no idea really (or actually: too many ideas). Sorry.Qintar
Not all SMTP servers require authentication before allowing you to send mail. In fact, authentication for SMTP is an after-thought extension for SMTP that came years later.Underline
U
9

The "The SMTP server does not support authentication." exception means that your server doesn't support authentication. In other words, it does not accept a username and password. You need to use it anonymously.

Even though you supplied some NetworkCredentials to the System.Net.Mail.SmtpClient, it doesn't mean that the SmtpClient used them. You've just been supplying information to the System.Net.Mail.SmtpClient that you didn't need to.

TL;DR: Don't bother with calling client.Authenticate (creds);

Change your code to this:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var mailMessage = new MimeMessage();
mailMessage.From.Add(new MailboxAddress(fromMailAddress));
mailMessage.To.Add(new MailboxAddress(toMailAddress));
mailMessage.Subject = "SendMail_MailKit_WithDomain";
mailMessage.Body = new TextPart(TextFormat.Plain)
{
    Text = "Hello"
};

using (var smtpClient = new MailKit.Net.Smtp.SmtpClient())
{
    smtpClient.Connect("MailServer", 25, MailKit.Security.SecureSocketOptions.None);                   
    smtpClient.Send(mailMessage);
    smtpClient.Disconnect(true);
}
Underline answered 19/3, 2021 at 15:19 Comment(4)
The part Dont bother with call client.Authenticate is not true. with all your settings it did not work util I selected authenticate and SecureSocketOptions.StartTls. ThanksGiralda
Then you had a different issue than the original poster did.Underline
@Underline I switched my System.Net.Mail.SmtpClient class to your MailKit.Net.Smtp.SmtpClient class and before operating the switch my Smtp Client were supporting authentication. I also rolled back to use System.Net.Mail.SmtpClient and it worked as initial. Any idea why this behavior happened?Dudek
@Dudek there's a myriad of possibilities. System.Net.Mail supports a different subset of authentication mechanisms than MailKit. It could be that MailKit doesn't support the same authentication mechanism that System.Net.Mail is using. It could be that you mistyped the username or password (if I had a dollar for every time someone was sure they had that info correct and later found out they mistyped it, I'd be as wealthy as Elon Musk).Underline
P
0

This message may be misleading - as @MindRoasterMir mentioned in comments - authentication is usually the very first call to the server it can be an issue with connection itself.

smtpClient.Connect("MailServer", 25, SecureSocketOptions.None); setup to server which requires TLS will fail with the described error as well.

Solution for me was to enable TLS if possible (as I need to support both kinds of servers): smtpClient.Connect("MailServer", 25, SecureSocketOptions.StartTlsWhenAvailable);

Paramatta answered 25/7, 2023 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.