0

The original post was removed and I thought I would revise my latest issue. I understand completely that it has something to do with my username and password but am not sure of what else I can do. I have rest passwords multiple times, deleted and reestablished the username/email address multiple times and even dumped the .Net SmtpClient for the MailKit approach which I am now getting this error. I am wonder if it has anything to do with me going through Bluehost for my domain and office365 subscription. With that said, as I began developing this application, I have noticed through Telnet I am still unable to establish a connection. Does anybody have any advice on how to send an email with SMTP (or anyway) through office365/outlook? Here is my code: Controller:

[HttpPost]
    public async Task<IActionResult> SendContactEmail(ContactCardModel contact)
    {
        string emailSubject = $"Inquiry from {contact.name} from {contact.organization}";
        await _emailSender.SendEmailAsync(contact.name, contact.email, emailSubject, contact.message);
        ViewBag.ConfirmMsg = "Sent Successful";
        return View("Contact");
    }

Email Service:

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);
    }
}

Interface:

 public interface ISendEmail 
{
    Task SendEmailAsync(
        string fromName,
        string fromEmail,
        string subject,
        string message
    );
}

Greatly appreciate anybody willing to help!

  • Have you read this SO post - (https://stackoverflow.com/questions/30342884/5-7-57-smtp-client-was-not-authenticated-to-send-anonymous-mail-during-mail-fr)? – Ryan Wilson Jan 10 '21 at 20:42
  • Port 465 maybe? – Charlieface Jan 10 '21 at 21:17
  • Does this help? https://answers.microsoft.com/en-us/msoffice/forum/msoffice_outlook-mso_winother-mso_o365b/client-was-not-authenticated-to-send-anonymous/d405bcb0-f40c-42fa-b1b2-477597100123 – Nick.Mc Jan 10 '21 at 21:23
  • Ryan Wilson- I have seen this post and have tried all of the recommendations. – Tyler Crane Jan 11 '21 at 00:46
  • Nick McDermaid- I have seen this post as well and tried everything recommended with no success. – Tyler Crane Jan 11 '21 at 00:47
  • Charlieface- when I changed port 587 to 465 it resulted in this: ExtendedSocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. [::ffff:52.96.32.178]:465 – Tyler Crane Jan 11 '21 at 00:50
  • So you're sending to a host ending in `mail.protection.outlook.com`? – Nick.Mc Jan 11 '21 at 12:44
  • Nick McDermaid- I have tried that yes and it isn't working either. – Tyler Crane Jan 11 '21 at 12:59
  • What is your from address? This must be a valid addres with the account you are sending the mail from... – Poiter Jan 11 '21 at 16:25
  • Poiter- I have created and licensed a donotreply@odin-development.com address. It sends perfectly fine from the outlook app. I'm currently in TELNET trying to understand the issue with the EHLO/HELO and nothing is blatantly obvious as we speak. – Tyler Crane Jan 11 '21 at 17:53

1 Answers1

0

I finally figured out my own issue and it wasn't even in the slightest bit that difficult. More importantly, the message itself was very misleading and I am here to shed some light for those who are encountering the same issue.

        SmtpClient smtp = new SmtpClient();
        smtp.ServerCertificateValidationCallback = (s, c, h, e) => true;
        // The above Certificate Validation Callback has to be exactly as I show it.
        // I, for some reason, had invalid options applied and can assure anyone who
        // has followed any tutorial whatsoever, what they have inputted is wrong or for dummy 
        // testing purposes. Once you have this established, host has to be exactly as
        // follows: smpt.office365.com and port 587 ONLY(25 is not longer supported).
        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);

In no way shape or form did my error apply to the actual account itself. Although this may not directly apply to my issue where the tenant username/pass were not the issue, it may still be an issue for anyone. However, I do highly suggest you consider exactly what my code reflects above with the host and port suggestions I have made.

Thank you all who attempted to try and solve this and if anyone has any additional questions, I would be more than happy to answer them. Thanks!