2017-10-16 86 views
0

我用这个发送电子邮件,但是当我提交时,它显示我“Line 41:smtp.Send(msg);”错误。我不知道这有什么问题。谢谢您的帮助!SmtpClient.Send(msg);错误

public partial class Contact : Page 
 
    { 
 
     protected void btnSubmit_Click(object sender, EventArgs e) 
 
     { 
 
      MailMessage msg = new MailMessage(); 
 
      msg.From = new MailAddress("[email protected]"); 
 
      msg.To.Add("[email protected]"); 
 
      msg.Subject = txtSubject.Text; 
 
      msg.Body = txtFirstName.Text + " " + txtLastName.Text + "'s phone number is: " + txtPhone.Text + ". <br />" + "Email Address is: " + txtEmail.Text + "Message: <br />" + txtMessage.Text; 
 
      msg.IsBodyHtml = true; 
 

 
      SmtpClient smtp = new SmtpClient(); 
 
      smtp.Host = "smtp.gmail.com"; 
 
      
 
      System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential(); 
 
      NetworkCred.UserName = "[email protected]"; 
 
      NetworkCred.Password = "xxxxx"; 
 
      smtp.UseDefaultCredentials = true; 
 
      smtp.Credentials = NetworkCred; 
 
      smtp.Port = 587; 
 
      smtp.EnableSsl = true; 
 
      smtp.Send(msg); 
 
      lblMessage.Text = "Email has been successfully sent!"; 
 

 
     } 
 
    }

Stack Trace: 
 

 

 
[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] 
 
    System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) +1844406 
 
    System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) +46 
 
    System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) +88 
 
    System.Net.Mail.SmtpClient.Send(MailMessage message) +1856 
 
    XinNing_Web.Contact.btnSubmit_Click(Object sender, EventArgs e) in E:\Projects\ASP.NET\XinNing_Web\XinNing_Web\Contact.aspx.cs:41 
 
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9767618 
 
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204 
 
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12 
 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 
 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 
 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1738

+0

请检查您的双重身份验证设置,通常在使用Gmail提供程序从不同IP地址发送机器时发生此问题。您可以尝试在当前计算机上登录Gmail并确认您帐户的身份验证,请访问https://support.google.com/accounts/answer/1144110?hl=zh-CN –

+0

非常感谢。我知道了! –

回答

0

允许在您的Gmail帐户的安全性应用到真正的从你的尝试发送邮件。

转到我的帐户 - >登录&安全 - >允许安全性较低的应用(位于页面底部)。

由于此错误发生的一段时间。

相关问题