Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 邮件无法到达电子邮件_C#_Asp.net_Email - Fatal编程技术网

C# 邮件无法到达电子邮件

C# 邮件无法到达电子邮件,c#,asp.net,email,C#,Asp.net,Email,我正在尝试在我的网站上建立一个联系表单,我使用asp.net和C#,当我提交邮件时,它没有到达电子邮件,是因为我使用的是本地服务器吗?或者我的代码中有错误?我在catch部分收到一条错误消息:“您的消息发送失败,请重试。” 这是C页后面的代码# 这是个例外 System.Net.Mail.SmtpException:SMTP服务器需要安全的 连接或客户端未通过身份验证。服务器响应 was:5.5.1需要身份验证。了解更多信息,请访问 System.Net.Mail.MailCommand.Che

我正在尝试在我的网站上建立一个联系表单,我使用asp.net和C#,当我提交邮件时,它没有到达电子邮件,是因为我使用的是本地服务器吗?或者我的代码中有错误?我在catch部分收到一条错误消息:“您的消息发送失败,请重试。”

这是C页后面的代码#

这是个例外

System.Net.Mail.SmtpException:SMTP服务器需要安全的 连接或客户端未通过身份验证。服务器响应 was:5.5.1需要身份验证。了解更多信息,请访问 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode状态码, 字符串响应)在System.Net.Mail.MailCommand.Send(SmtpConnection)中 conn,Byte[]命令,MailAddress from,布尔allowUnicode)位于 System.Net.Mail.SmtpTransport.SendMail(邮寄地址发送者, MailAddressCollection收件人,字符串deliveryNotify,布尔值 allowUnicode、SmtpFailedRecipientException和exception)在 System.Net.Mail.SmtpClient.Send(MailMessage)位于 Default2.btnSubmit\u在 c:\Users\looly\Documents\Visual Studio 2015\WebSites\WebSite6\Default2.aspx.cs:第46行

服务器响应为:5.5.1身份验证

基于此错误,请检查以下步骤

  • 输入正确的登录密码
  • 删除两步验证
  • 您必须为您的google启用从其他时区/ip登录 帐户。要执行此操作,请按照 并通过单击“继续”按钮允许访问
  • 转到以下位置的安全设置并启用不太安全的应用程序。这样您就可以从所有应用程序登录

我们想知道正确的异常,所以只需删除catch块内的标签即可。然后写入exception..catch(exception ex){throw ex};乍一看,您的代码似乎还可以。你正确配置Gmail了吗?它通常会阻止它认为不安全的应用程序的发送。如果您启用了2步,您可能需要生成一个特定于应用程序的密码发布您的确切异常。如果异常非常严重,我将获得10美元
服务器响应为:5.5.1需要身份验证。
我更新了帖子,equalsk您是对的:D
try
{
    //Create the msg object to be sent
    MailMessage msg = new MailMessage();
    //Add your email address to the recipients
    msg.To.Add("jasmine.afnan@gmail.com");
    //Configure the address we are sending the mail from
    MailAddress address = new MailAddress("jasmine@gmail.com");
    msg.From = address;
    //Append their name in the beginning of the subject
    msg.Subject = txtName.Text + " :  " + ddlSubject.Text;
    msg.Body = txtMessage.Text;

    //Configure an SmtpClient to send the mail.
    SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
    client.EnableSsl = true; //only enable this if your provider requires it
    //Setup credentials to login to our sender email address ("UserName", "Password")
    NetworkCredential credentials = new NetworkCredential("jasmine.afnan@gmail.com", "*");
    client.Credentials = credentials;

    //Send the msg
    client.Send(msg);

    //Display some feedback to the user to let them know it was sent
    lblResult.Text = "Your message was sent!";

    //Clear the form
    txtName.Text = "";
    txtMessage.Text = "";
}
catch
{
    //If the message failed at some point, let the user know
    lblResult.Text = "Your message failed to send, please try again.";
}