C# 在服务器出现异常时,SMTP服务器需要安全连接,或者客户端未通过身份验证

C# 在服务器出现异常时,SMTP服务器需要安全连接,或者客户端未通过身份验证,c#,asp.net,.net,email,authentication,C#,Asp.net,.net,Email,Authentication,我有一个发送邮件的代码。当我在本地主机上发送邮件时,邮件可以轻松发送,但当我将代码上传到服务器时。我得到了这个例外 System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at at Sys

我有一个发送邮件的代码。当我在本地主机上发送邮件时,邮件可以轻松发送,但当我将代码上传到服务器时。我得到了这个例外

   System.Net.Mail.SmtpException: The SMTP server requires a secure
connection or the client was not authenticated. The server response
was: 5.5.1 Authentication Required. Learn more at at
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,
String response) at System.Net.Mail.MailCommand.Send(SmtpConnection
conn, Byte[] command, MailAddress from, Boolean allowUnicode) at
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender,
MailAddressCollection recipients, String deliveryNotify, Boolean
allowUnicode, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message) at
Admin_test.Page_Load(Object sender, EventArgs e) in
d:\hostingspaces\abd9009\ihmsgr.domainNames.net\wwwroot\info\test.aspx.cs:line
29
我的代码在本地机器和其他服务器上运行良好,但是 无法在服务器上工作的情况如下:

  protected void Page_Load(object sender, EventArgs e)
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add("username@gmail.com");
        mail.From = new MailAddress("username@gmail.com", "Email
head", System.Text.Encoding.UTF8);
        mail.Subject = "This mail is send from asp.net application";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "This is Email Body Text";
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
        SmtpClient client = new SmtpClient();
        client.Credentials = new
System.Net.NetworkCredential("username@gmail.com", "MyPassword");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        try
        {
            client.Send(mail);
             }
        catch (Exception ex)
        {
            Response.Write(ex);
}
}
我正在使用服务器上的托管空间,请帮助

试试这个

1) 启用SSL true或false

2) 再次检查凭据

3) 有时某些SMTP服务器(主要针对公司)不允许外部访问,或有单独的IP用于外部访问

4) 检查用户名是否应包含@Host.com或不包含@Host.com


我希望其中一个对你有用。

试一试,我刚刚遇到了这个问题,解决了我的问题,但可能是另一种情况。 我的委托人给了我他全新产品的证书noreply@hisSite.com然后我尝试使用他们作为凭据,然后我得到了这个错误,因为他没有登录到它至少有一次。我登录后,它就开始工作了

也许这些东西中的一个可以帮助你

  • 尝试登录您的帐户,更改密码并设置新密码(强密码)
  • 尝试使用mail.UseDefaultCredentials=false
    尽快删除此邮件并在代码中不包含您的GMAIL用户名和密码的情况下重新发布!!!使用虚拟用户名和密码修改亲爱的您仍然可以在修订历史中看到您的密码供参考。此时更改密码可能是最容易的。您应该学习如何使用wireshark和telnet实际调试smtp通信,因为您可能会遇到其他问题。我更改了密码