C# 使用smtp.gmail.com时System.Security.SecurityException

C# 使用smtp.gmail.com时System.Security.SecurityException,c#,.net,smtp,C#,.net,Smtp,当使用来自的代码时 我不断收到System.Security.SecurityException:请求“System.Net.Mail.SmtpPermission”类型的权限 这段代码是否必须在某些特殊的托管环境中使用,或者我是否遗漏了一些显而易见的东西 谢谢 发送电子邮件取决于SMTP服务器的配置方式 According to gmail confiruged You need to remove this line of code UseDefaultCredentials=false

当使用来自的代码时

我不断收到System.Security.SecurityException:请求“System.Net.Mail.SmtpPermission”类型的权限

这段代码是否必须在某些特殊的托管环境中使用,或者我是否遗漏了一些显而易见的东西


谢谢

发送电子邮件取决于SMTP服务器的配置方式

According to gmail confiruged You need to remove this line of code
UseDefaultCredentials=false

只要在这行留言,你的信息就会被发送出去

 var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            // do not use this command
           // UseDefaultCredentials = false,
            Credentials = new NetworkCredential("username", fromPassword)
        };

Microsoft建议尽可能不使用defaultcredentials。请参阅链接以了解详细信息。

否,您可能位于禁用通过SMTP发送电子邮件的共享主机上(即,您处于中等信任状态)。这是CAS的问题。好吧,是的,我有点太快了,对这件事感到沮丧。在开发服务器(cassini?)上失败,但在iis上工作正常。
 var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            // do not use this command
           // UseDefaultCredentials = false,
            Credentials = new NetworkCredential("username", fromPassword)
        };