SmtpCommandException:身份验证数据不正确未知位置身份验证异常:535:身份验证数据不正确

时间:2021-01-10 20:28:24

标签: c# asp.net asp.net-core smtpclient mailkit

原来的帖子被删除了,我想我会修改我的最新一期。我完全理解这与我的用户名和密码有关,但我不确定我还能做什么。我多次重置密码,多次删除并重新建立用户名/电子邮件地址,甚至为 MailKit 方法转储了 .Net SmtpClient,我现在收到此错误。 我想知道这是否与我通过 Bluehost 获得域和 office365 订阅有关。话虽如此,当我开始开发这个应用程序时,我注意到通过 Telnet 我仍然无法建立连接。有人对如何通过 office365/outlook 使用 SMTP(或其他方式)发送电子邮件有任何建议吗? 这是我的代码: 控制器:

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

电子邮件服务:

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

界面:

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

非常感谢任何愿意提供帮助的人!

1 个答案:

答案 0 :(得分:0)

我终于想通了我自己的问题,它甚至没有那么困难。更重要的是,该消息本身非常具有误导性,我在这里为遇到相同问题的人提供一些启示。

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

我的错误绝不适用于实际帐户本身。虽然这可能不直接适用于我的问题,即租户用户名/通行证不是问题,但对于任何人来说,这可能仍然是一个问题。但是,我强烈建议您根据我提出的主机和端口建议准确考虑我的代码在上面所反映的内容。

感谢所有尝试解决此问题的人,如果有人有任何其他问题,我将非常乐意回答。谢谢!