Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MailKit.Net.Smtp.SmtpClient SMTP server does not support authentication

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

like image 390
Ezhumalai Avatar asked May 23 '26 09:05

Ezhumalai


1 Answers

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);
}
like image 83
jstedfast Avatar answered May 24 '26 23:05

jstedfast