安全异常:请求类型为'System.Net.Mail.SmtpPermission'的权限。

14

这是那种“本地可以工作,但在服务器上不行”的帖子之一。

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

 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]
主机无法为我提供适用于从Web服务器发送SMTP消息的代码或SMTP客户端。因此,我需要找到一种替代方法来发送它们,以使我的Web服务器满足严格的安全策略。
这是源代码:
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);
        }
    }

1
这是一个代码访问问题。看起来你的主机特意取走了你访问SMTP所需的权限。 - vcsjones
@vcsjones,是的,有没有一种不需要高权限级别的发送SMTP的方法? - RyanJMcGowan
很难说,因为不知道主机限制了你做什么其他事情。他们有意删除了那个访问权限,所以任何解决方法他们可能也已经取消了相应的权限。这是你可能需要与你的托管提供商一起解决的问题。 - vcsjones
他们将信任级别设置为低或中等。不确定是哪个级别。由于这是共享服务器,他们无法提供更高的级别给我。 - RyanJMcGowan
2个回答

21
一个低安全级别不允许您指定SMTP端口。默认端口是25号端口。虽然我的ISP指定使用587号端口,但我可以使用25号端口并且它也能正常工作。

1
谢谢你,Ryan - 这也帮助了我!我提高了你的分数。- Dan。 - DaniB
1
我还必须将UseDefaultCredentials = false和ssl = false设置为才能使其工作。谢谢! - Exel Gamboa

10

我曾经遇到类似的问题,我只需在Web.config文件中添加以下代码,问题就解决了。

<configuration>
  <system.web>
    .....
    <trust level="Full" originUrl=""/>
  </system.web>
</configuration>
我在共享主机服务器上遇到了这个问题。当尝试创建SMTP客户端对象并给它分配一个端口号时,会出现错误。增加信任级别对我非常有用。我只是详细地阐述了细节,以确保它对其他人也有帮助。非常感谢解决方案!由于无法评论,因此在此向原始作者表示感谢。

这对我来说不是一个选项,因为我正在处理的网站是共享服务器,并且具有中等信任级别。 - RyanJMcGowan
在它起作用之前,我还添加了端口和启用SSL。<network host="smtpout.europe.secureserver.net" userName="name@example.com" password="pass" enableSsl="false" port="80"/> - yogihosting
我和楼主遇到了完全相同的问题,但是我在web.config中添加了这一行,然后出现了一个新的异常。关于套接字的某些内容,我认为与SQL有关。我的网站不使用SQL,所以我不确定该怎么办。如果我删除信任级别的那一行,那么我就会得到楼主发布的原始错误消息。有什么建议吗? - user2903379

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接