MailKit Wont Authenticate when trying to send email via SMTP

Tyler Crane 96 Reputation points
2021-01-11T23:51:31.593+00:00

I am developing an app that prompts a user to enter input to be emailed from a contact form. Unfortunately I am unable to authenticate the email address I am using (535 code). However, I have unsubscribed and resubscribed the account name and changed the password multiple time to absolutely no avail. I am not sure what else to do.
Here is a snippet of the code:

public class SendEmailService : ISendEmail
        {
            private string _host;
            private string _from;
            private string _pwd;
            public SendEmailService(IConfiguration configuration)
            {
                //TODO: Collect SMTP Configuration Settings
                var smtpSection = configuration.GetSection("SMTP");
                _host = smtpSection.GetSection("Host").Value;
                _from = smtpSection.GetSection("From").Value;
                _pwd = smtpSection.GetSection("Pwd").Value;


            }

            public async Task SendEmailAsync(string fromName, string fromEmail, string subject, string message)
            {
                //TODO: Build MailMessage Object
                MimeMessage mailMessage = new MimeMessage();
                mailMessage.From.Add(new MailboxAddress(fromName, fromEmail));
                mailMessage.To.Add(new MailboxAddress("App Admin", "tyler.crane@odin-development.com"));
                mailMessage.Subject = subject;
                BodyBuilder bodyBuilder = new BodyBuilder
                {
                    HtmlBody = message
                };

                //TODO: Build SmtpClient Object and NetworkCredential Object
                SmtpClient smtp = new SmtpClient();
                smtp.ServerCertificateValidationCallback = (sender, certificate, certChainType, errors) => true;
                smtp.AuthenticationMechanisms.Remove("XOAUTH2");
                await smtp.ConnectAsync(_host, 587, SecureSocketOptions.StartTls).ConfigureAwait(false);
                await smtp.AuthenticateAsync(new NetworkCredential(_from, _pwd)).ConfigureAwait(false);
                await smtp.SendAsync(mailMessage).ConfigureAwait(false);
            }
        }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,134 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,195 questions
{count} votes

Accepted answer
  1. Tyler Crane 96 Reputation points
    2021-01-18T16:03:59.53+00:00

    For anyone who is encountering this issue, here is my solution that corrected the authentication issue.

    The error above did not properly define what my issue was and upon further investigating from multiple sources was able to isolate to the ServerCertificateValidationCallback. I changed smtp.ServerCertificateValidationCallback = (sender, certificate, certChainType, errors) => true; to smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;. See above code for more clarification. With that said, I was able to keep the host name smtp.office365.com and the port 587 with the same credentials. Once this change was made, the email went through no problem.

    So, was it an authentication issue with the tenant username/pass? No. The issue at hand was what was being passed into the ServerCerticateValidationCallback. With all of this said, ensure you heed my advice and proceed. IMPORTANT: this does not mean your credentials are completely clean. I wouldn't dismiss the fact it could still be a typo, that you should drop and recreate the tenant with the license or maybe just change the password like others did.

    I hope this helps and if you have any other questions related to this, I will be more than happy to answer. Thanks!

    0 comments No comments

0 additional answers

Sort by: Most helpful