Request for the permission of type 'System.Net

2019-09-08 17:50发布

问题:

Iam having a mail form within ASP.NET MVC 4 that works locally but not live. I get the error which you can see on the title, here is my code.

This is my Web.config code:

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network">
    <network
      host="smtp.mandrillapp.com"
    />
  </smtp>
</mailSettings>

This is my Controller code:

    [HttpPost]
    public ActionResult Contact(SendMail input)
    {
        ViewData["Message"] = "Success";
        var space = " ";

        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("no-reply@wideart.se");
        mail.To.Add("info@wideart.se");
        mail.Subject = "Intresse!";
        mail.Body = "Name: " + input.Name + space + "Email: " + input.Email + space + "Number: " + input.Number + space + "Message: " + input.Message;
        mail.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient("smtp.mandrillapp.com");
        smtp.Port = 587;
        smtp.Credentials = new NetworkCredential("info@wideart.se", "gDKFpiiWhLSqVTMNzL4nDw");

        try
        {
            smtp.Send(mail);
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.ToString());
        }

        return View();
    }
}

I tried using port="25" and port="587" on the network code, but I got an error there aswell.

I need help!