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