如何在 dotnet core 中使用 godaddy office365 发送电子邮件?

How to send emails using godaddy office365 in dotnet core?

每次我尝试使用我的 SMTP 凭据从 godaddy 邮件发送电子邮件时,我都会收到此错误消息,即使我已通过身份验证

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous
mail during MAIL FROM [YQBPR0101CA0107.CANPRD01.PROD.OUTLOOK.COM]

这是我用来发送电子邮件的代码:

var client = new SmtpClient(
    config.GetValue<string>("EmailSettings:MailServer"), 
    config.GetValue<int>("EmailSettings:MailPort")) 
{
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(config.GetValue<string>("EmailSettings:Sender"), config.GetValue<string>("EmailSettings:Password")),
    TargetName = config.GetValue<string>("EmailSettings:TargetName"),
    EnableSsl = config.GetValue<bool>("EmailSettings:EnableSSL"),
    DeliveryMethod = SmtpDeliveryMethod.Network,
};

try
{
    return client.SendMailAsync(
        new MailMessage(
            from: config.GetValue<string>("EmailSettings:Sender"), 
            to: email, 
            subject: subject, 
            body: htmlMessage) 
        {
            IsBodyHtml = isBodyHtml,
            BodyEncoding = System.Text.Encoding.UTF8,
            SubjectEncoding = System.Text.Encoding.UTF8,
        }
    );
}
catch(Exception exception)
{
    client.Dispose();
    throw exception;
}

我在 appsettings.json 中添加了一个新部分来提供配置

"EmailSettings": {
  "MailServer": "smtp.office365.com",
  "MailPort": 587,
  "Sender": "user@mydomain.ca",
  "Password": "Passw0rd!",
  "EnableSSL": true,
  "TargetName": "STARTTLS/smtp.office365.com"
}

如果我的身份验证正确,什么会导致此问题?

就像大多数 SMTP 提供商一样,从经过身份验证的匿名计算机发送外发邮件的身份验证存在限制,因为 godaddy 使用它自己的 re****** 版本的 office365 管理面板,您根本不能更改此设置。你得打电话给godaddy的客服,希望在等了一个小时后,代表能真正知道你在说什么,然后再挂断你。