C# SecurityException:请求类型为“System.Net.Mail.SmtpPermission”的权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11992851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:59:56  来源:igfitidea点击:

SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission'

c#asp.netsmtp

提问by RyanJMcGowan

This is one of those 'works locally, doesn't work on the server' posts.

这是“在本地有效,在服务器上无效”的帖子之一。

I have a simple contact form that sends an email. On the server, I get the following exception:

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

 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]

The host was unable to give me code or an SMTP client that will work for sending SMTP messages from the web server. So I need to find an alternate way to send them that will make my web server happy with a constrictive security policy.

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

Here's the source code:

这是源代码:

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

采纳答案by RyanJMcGowan

A low security level doesn't allow you to specify the smtp port. Default is port 25. Although my ISP specifies port 587, I can use port 25 and it works fine.

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

回答by Aditya

I had a similar problem I simply added the following code in the Web.config file and it worked for me

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

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

I was facing this issue on my Shared Hosting Server. The error was coming up while trying to create an object of SMTP client and then assigning it a port number. Increasing trust level worked pretty well for me. I just elaborated details to make sure it helps some one else too. really appreciate the solution ! As I was unable to comment hence just thanked here the original author.

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