【问题标题】:Sending Email In C# MVC..On Local host..not working?在 C# MVC 中发送电子邮件..在本地主机上..不起作用?
【发布时间】:2013-02-26 03:30:07
【问题描述】:

我正在尝试使用 C# mvc 发送一封基本的电子邮件...由于某种原因它不起作用..我尝试了许多不同的解决方案,但在浏览器中不断收到相同的错误,显示“操作已超时”..哪个我认为是连接问题。错误开始于“client.Send(mail); 我在我的本地机器上执行此操作。我尝试使用端口 587,465,995,25,但没有运气。在 Visual Studio 的编译器中,当我构建它时,我没有收到任何错误。我刚刚得到错误,在网络浏览器中,在线“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>

我也在 web.config 中试过这个:

<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>

【问题讨论】:

标签: asp.net-mvc smtp sendmail smtpclient system.net.mail


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请尝试以下方法

    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);
    }
    

    这段代码对我有用

    【讨论】:

    • 谢谢我刚刚得到它......原始代码工作......只是我不得不从不同的位置尝试它。我所在的连接阻止了端口......
    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2010-12-19
    • 2014-08-19
    • 1970-01-01
    • 2018-02-20
    • 2012-03-17
    相关资源
    最近更新 更多