繁体   English   中英

使用 asp.net 发送电子邮件显示此错误:SMTP 服务器需要安全连接或客户端未通过身份验证

[英]Send email using asp.net shows this error:The SMTP server requires a secure connection or the client was not authenticated

我尝试使用此代码发送电子邮件:

  public void Semail(string subject, string messageBody, string toAddress)
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(toAddress);
            //mail.To.Add("amit_jain_online@yahoo.com");
            mail.From = new MailAddress("noreplykaramaoozi@eesharif.edu");
            mail.Subject = subject;
            string Body = messageBody;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "sina.sharif.ir"; //Or Your SMTP Server Address
            smtp.Port = 587;
            smtp.Credentials = new System.Net.NetworkCredential
                ("noreplykaramaoozi@eesharif.edu", "*******");
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = false;
            smtp.Send(mail);
        }

但是执行后我得到了这个错误:

 The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication required 

堆栈跟踪 :

[SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication required]
   System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) +2162008
   System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) +287
   System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) +137
   System.Net.Mail.SmtpClient.Send(MailMessage message) +2188821
   Novitiate.fa.Register.btnLogin_Click(Object sender, EventArgs e) +2948
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3707

此致

您的 Gmail 安全阻止了邮件访问 Gmail 帐户。 因此,如果您想发送消息,您必须“关闭”安全性较低的应用程序的访问权限。 当您这样做时,您可以轻松地发送邮件,但会冒着 Gmail 安全的风险。 所以,这完全取决于你。

下面给出了更改安全性的链接

https://www.google.com/settings/security/lesssecureapps?pli=1

如果这有帮助,请尝试以下方法:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(Username);
mail.To.Add(TxtEmail.Text);
mail.Subject = Subject;
mail.Body = Body;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(Username, Password);
SmtpServer.EnableSsl = true;
SmtpServer.Timeout = 20000;
SmtpServer.Send(mail);

登录到您的电子邮件帐户后,请考虑 @Krunal Patil 在那里的回答,并对发送的验证邮件进行操作以授予对您的 C# 应用程序的访问权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM