【问题标题】:C# SMTP MailMessage receiving error "5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM"C# SMTP MailMessage 接收错误“5.7.57 SMTP;客户端未经过身份验证以在 MAIL FROM 期间发送匿名邮件”
【发布时间】:2019-09-15 04:52:33
【问题描述】:

我目前拥有的代码是:

    public void SendEmail(string to, string cc, string bcc, string subject, string body, string attachmentPath = "", System.Net.Mail.MailPriority emailPriority = MailPriority.Normal, BodyType bodyType = BodyType.HTML)
    {
        try
        {
            var client = new System.Net.Mail.SmtpClient();
            {
                client.Host = "smtp-mail.outlook.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.EnableSsl = true;

                client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                client.Credentials = new NetworkCredential("[my company email]", "[my password]");
                client.Timeout = 600000;
            }

            MailMessage mail = new MailMessage("[insert my email here]", to);
            mail.Subject = subject;
            mail.Body = body;

            client.Send(mail);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

我尝试发送到的电子邮件地址托管在 Office 365 的 Outlook 上。稍后我们可能必须更改特定地址,但它们的配置可能相同。

但是,每当我尝试运行 client.Send(mail); 命令时,都会收到相同的错误。错误全文为:

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 

我尝试了一些不同的方法,例如在 25 和 587 之间切换端口,将主机更改为 Office365,或者将 UseDefaultCredentialsEnableSssl 切换为 true 或 false。但我总是看到同样的错误。我还缺少什么吗?

【问题讨论】:

    标签: c# email outlook smtp


    【解决方案1】:

    我在本网站的其他地方找到了一个示例代码块,并用它替换了我所拥有的所有内容,这有所作为。

    函数名称和参数是相同的,但这是我替换它的主体的内容。

     var _mailServer = new SmtpClient();
     _mailServer.UseDefaultCredentials = false;
     _mailServer.Credentials = new NetworkCredential("my email", "my password");
     _mailServer.Host = "smtp.office365.com";
     _mailServer.TargetName = "STARTTLS/smtp.office365.com"; 
     _mailServer.Port = 587;
     _mailServer.EnableSsl = true;
    
    var eml = new MailMessage();
    eml.Sender = new MailAddress("my email");
    eml.From = eml.Sender;
    eml.To.Add(new MailAddress(to));
    eml.Subject = subject;
    eml.IsBodyHtml = (bodyType == BodyType.HTML);
    eml.Body = body;
    
    _mailServer.Send(eml);
    

    我不确定,但我认为用 smtp Office 365 链接而不是 Outlook 链接替换主机值,以及记住添加我以前没有的目标名称,两者都成功了并解决了授权问题(我之前已经确认这不是我们技术支持的凭据问题)。

    【讨论】:

      猜你喜欢
      • 2015-08-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      • 2018-08-02
      • 1970-01-01
      • 2017-10-17
      相关资源
      最近更新 更多