Reputation: 193
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
Upvotes: 9
Views: 29437
Reputation: 2497
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);
Upvotes: 0
Reputation: 38528
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);
}
Upvotes: 9