Asp.net mvc 在本地主机上以C#MVC..发送电子邮件..不工作?

Asp.net mvc 在本地主机上以C#MVC..发送电子邮件..不工作?,asp.net-mvc,smtp,sendmail,smtpclient,system.net.mail,Asp.net Mvc,Smtp,Sendmail,Smtpclient,System.net.mail,我想用C#mvc发送一封基本的电子邮件……因为某些原因它无法工作。。我尝试了许多不同的解决方案,但在浏览器中不断出现“操作超时”的相同错误。我认为这是连接问题。错误开始于“client.Send(mail);我在本地计算机上执行此操作。我尝试使用端口587465995,25,但没有成功。在Visual Studio中的编译器中,当我构建它时,我没有收到任何错误。我只是在web浏览器中联机得到错误”client.Send(mail);" 控制器: public void SendEmail(H

我想用C#mvc发送一封基本的电子邮件……因为某些原因它无法工作。。我尝试了许多不同的解决方案,但在浏览器中不断出现“操作超时”的相同错误。我认为这是连接问题。错误开始于“client.Send(mail);我在本地计算机上执行此操作。我尝试使用端口587465995,25,但没有成功。在Visual Studio中的编译器中,当我构建它时,我没有收到任何错误。我只是在web浏览器中联机得到错误”client.Send(mail);"

控制器:

  public void SendEmail(History hi)
  {
    SmtpClient client = new SmtpClient();   //server type
    client.Port = 587;                      
    client.Host = "smtp.gmail.com";         //google
    client.EnableSsl = true;                //SSl set to true
    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("papasmurf985@gmail.com", "******"); 

    MailMessage mail = new MailMessage("papasmurf985@gmail.com", "papasmurf985@gmail.com", "Test Score", "Your score is" + hi.score);
    mail.BodyEncoding = System.Text.Encoding.UTF8;
    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

    client.Send(mail);                      
  }
web.config:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="smtp.gmail.com" port="587" />
    </smtp>
  </mailSettings>
</system.net>
<system.net>
   <mailSettings>
     <smtp from="no-reply@no-reply.com">
       <network host="smtp.gmail.com" port="587" enableSsl="true"
                userName="papasmurf985@gmail.com" password="PASSWORD" />
     </smtp>
   </mailSettings>
</system.net>

我也在web.config中尝试过:

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="smtp.gmail.com" port="587" />
    </smtp>
  </mailSettings>
</system.net>
<system.net>
   <mailSettings>
     <smtp from="no-reply@no-reply.com">
       <network host="smtp.gmail.com" port="587" enableSsl="true"
                userName="papasmurf985@gmail.com" password="PASSWORD" />
     </smtp>
   </mailSettings>
</system.net>

我将首先确保您的ISP不会阻止外部SMTP服务器

我将首先确保您的ISP不会阻止外部SMTP服务器

请尝试以下操作

public void SendMessage(string toAddress, string subjectText, string bodyText)
{
    //create a object to hold the message
    MailMessage newMessage = new MailMessage();

    //Create addresses
    MailAddress senderAddress = new MailAddress(your_gmail_address);
    MailAddress recipentAddress = new MailAddress(toAddress);

    //Now create the full message
    newMessage.To.Add(recipentAddress);
    newMessage.From = senderAddress;
    newMessage.Subject = subjectText;
    newMessage.Body = bodyText;

    //Create the SMTP Client object, which do the actual sending
    SmtpClient client = new SmtpClient(your_gmail_server_address, your_gmail_port)
    {
        Credentials = new NetworkCredential(your_gmail_address, your_password),
        EnableSsl = true
    };


    //now send the message
    client.Send(newMessage);
}
此代码适用于我

请尝试以下方法

public void SendMessage(string toAddress, string subjectText, string bodyText)
{
    //create a object to hold the message
    MailMessage newMessage = new MailMessage();

    //Create addresses
    MailAddress senderAddress = new MailAddress(your_gmail_address);
    MailAddress recipentAddress = new MailAddress(toAddress);

    //Now create the full message
    newMessage.To.Add(recipentAddress);
    newMessage.From = senderAddress;
    newMessage.Subject = subjectText;
    newMessage.Body = bodyText;

    //Create the SMTP Client object, which do the actual sending
    SmtpClient client = new SmtpClient(your_gmail_server_address, your_gmail_port)
    {
        Credentials = new NetworkCredential(your_gmail_address, your_password),
        EnableSsl = true
    };


    //now send the message
    client.Send(newMessage);
}

这段代码对我有效

我希望那不是你真正的gmail密码有什么例外?我希望那不是你真正的gmail密码有什么例外?谢谢我刚刚得到它…原始代码有效…只是我不得不从不同的位置尝试。我当时的连接阻塞了端口…谢谢我刚刚得到的…原始代码有效s、 .只是我必须从其他位置尝试。我所在的连接已阻止端口。。