在asp.net中的服务器上托管后从Web应用程序发送邮件时发生错误

时间:2017-12-01 13:18:18

标签: c# asp.net email

我使用smtpclient对象发送邮件。邮件在本地发送,但是当Web应用程序托管在服务器上时(在BigRocks上),邮件发送失败,我发生了以下错误。

  

请求类型的许可   ' System.Security.Permissions.SecurityPermission,mscorlib,   Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'   失败。

        MailMessage mailmsg = new MailMessage();

        ServicePointManager.ServerCertificateValidationCallback =
        delegate(object s, X509Certificate certificate,
           X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            return true;
        };

        mailmsg.From = new MailAddress("xxx@gmail.com");
        mailmsg.To.Add(xxx);

            mailmsg.Subject =....
           mailmsg.Body = .....


        }
        mailmsg.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.EnableSsl = true;
        smtp.Credentials = new System.Net.NetworkCredential("xxx@gmail.com", "xxxx");
        smtp.EnableSsl = true;
        smtp.Send(mailmsg);

感谢:)

1 个答案:

答案 0 :(得分:0)

您是否尝试在web.config中添加信任级别

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