【问题标题】:SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission'SecurityException:请求“System.Net.Mail.SmtpPermission”类型的权限
【发布时间】:2012-08-13 03:19:08
【问题描述】:

这是“在本地工作,不能在服务器上工作”的帖子之一。

我有一个发送电子邮件的简单联系表。在服务器上,我得到以下异常:

 Security Exception
Description: The application attempted to perform an operation not allowed
by the security policy.  To grant this application the required permission
please contact your system administrator or change the application's trust
level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the
permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:
[No relevant source lines]

主机无法为我提供代码或 SMTP 客户端,用于从 Web 服务器发送 SMTP 消息。所以我需要找到一种替代方式来发送它们,这将使我的网络服务器对严格的安全策略感到满意。

这里是源代码:

private void SendMessage (string returnAddress, string textSubject, string messageText)
    {
        config config = new config();

        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

        message.To.Add(config.toEmailAddress);
        message.Subject = "Website Contact Form Message: " + textSubject;
        message.From = new System.Net.Mail.MailAddress(returnAddress);
        message.Body = messageText;

        message.IsBodyHtml = false;

        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.naturalcarpetcompany.com");
        smtp.Credentials = new System.Net.NetworkCredential(config.fromEmailAddress, config.fromEmailPassword);
        smtp.Port = 587;

        try
        {
            smtp.Send(message);
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            string errorMessage = string.Empty;
            while (ex2 != null)
            {
                errorMessage += ex2.ToString();
                ex2 = ex2.InnerException;
            }
            HttpContext.Current.Response.Write(errorMessage);
        }
    }

【问题讨论】:

  • 这是一个代码访问问题。看来您的主机已专门剥夺了您访问 SMTP 所需的权限。
  • @vcsjones 是的,有没有办法发送不需要高权限级别的 smtp?
  • 很难说不知道该主机还限制了您做什么。这故意删除了该访问权限,因此他们可能也删除了这样做的权限。这是您可能需要与托管服务一起使用的东西。
  • 他们将信任级别设置为低或中。不确定是哪个。他们不能给我更高的级别,因为它是共享服务器。

标签: c# asp.net smtp


【解决方案1】:

低安全级别不允许您指定 smtp 端口。默认是 25 端口。虽然我的 ISP 指定了 587 端口,但我可以使用 25 端口,它工作正常。

【讨论】:

  • 干杯瑞恩 - 这也帮助了我!我升级了你的分数。丹。
  • 我还必须设置 UseDefaultCredentials = false 和 ssl = false 才能让它工作。谢谢!
【解决方案2】:

我遇到了类似的问题,我只是在 Web.config 文件中添加了以下代码,它对我有用

<configuration>
  <system.web>
    .....
    <trust level="Full" originUrl=""/>
  </system.web>
</configuration>

我在共享主机服务器上遇到了这个问题。尝试创建 SMTP 客户端对象然后为其分配端口号时出现错误。提高信任度对我来说效果很好。我只是详细说明了细节,以确保它也有助于其他人。非常感谢解决方案!由于无法发表评论,因此在此感谢原作者。

【讨论】:

  • 这对我来说不是一个选择,因为我正在处理的站点是一个共享服务器并且具有中等信任级别。
  • 我还在它工作之前添加了端口和 enablesl。
  • 我遇到了与 OP 完全相同的问题,但我在 web.config 中添加了这一行,但后来出现了一个新异常。关于套接字的一些东西我认为与 SQL 有关。我的网站不使用 sql,所以我不知道从这里去哪里。如果我删除了该信任级别,那么我会收到 OP 发布的原始错误消息。有什么建议吗?
猜你喜欢
  • 2017-07-25
  • 2010-12-10
  • 2012-12-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多