SecurityException异常:请求类型'System.Net.Mail.SmtpPermission“许可 [英] SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission'

查看:330
本文介绍了SecurityException异常:请求类型'System.Net.Mail.SmtpPermission“许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是其中的一个'本地工作,在服务器不工作的帖子。

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

 安全例外
说明:试图执行一个操作的应用程序不允许
由安全策略。要授予此应用程序所需的权限
请与系统管理员联系或更改应用程序的信任
在配置文件中的水平。异常详细信息:System.Security.SecurityException:请求
类型'System.Net.Mail.SmtpPermission,系统,版本= 4.0.0.0许可,
文化=中性公钥= b77a5c561934e089'失败。源错误:
[没有相关的源行]

主持人是无法给我code或SMTP客户端,将用于从Web服务器发送SMTP邮件的工作。所以,我需要找到一个替代的方式来给他们,这将使我的Web服务器开心的紧缩安全策略。

下面是源$ C ​​$ C:

 私人无效SendMessage函数(字符串returnAddress的,串textSubject,串MessageText中)
    {
        配置配置=新的配置();        System.Net.Mail.MailMessage消息=新System.Net.Mail.MailMessage();        message.To.Add(config.toEmailAddress);
        message.Subject =网站联系表格消息:+ textSubject;
        message.From =新System.Net.Mail.MailAddress(returnAddress的);
        message.Body = MessageText中;        message.IsBodyHtml = FALSE;        System.Net.Mail.SmtpClient SMTP =新System.Net.Mail.SmtpClient(smtp.naturalcarpetcompany.com);
        smtp.Credentials =新System.Net.NetworkCredential(config.fromEmailAddress,config.fromEmailPassword);
        smtp.Port = 587;        尝试
        {
            smtp.Send(消息);
        }
        赶上(异常前)
        {
            异常EX2 =前;
            字符串的errorMessage =的String.Empty;
            而(EX2!= NULL)
            {
                的errorMessage + = ex2.ToString();
                EX2 = ex2.InnerException;
            }
            HttpContext.Current.Response.Write(的errorMessage);
        }
    }


解决方案

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

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.

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

解决方案

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.

这篇关于SecurityException异常:请求类型'System.Net.Mail.SmtpPermission“许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆