C#SMTP MailMessage收到错误“ 5.7.57 SMTP;客户端未通过身份验证以在MAIL FROM期间发送匿名邮件”

时间:2019-04-25 17:24:17

标签: c# email outlook smtp

我当前拥有的代码是:

    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。但是我总是看到相同的错误。还有其他我想念的东西吗?

1 个答案:

答案 0 :(得分:0)

我在该站点的其他地方找到了一个示例代码块,并用它替换了所有内容。

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

 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链接替换Host值,并记得添加一个我以前没有的目标名称,两者都成功了并解决了授权问题(我之前已经确认我们的技术支持不是凭证问题)。

相关问题