获取错误 - 请求“System.Net.Mail.SmtpPermission”类型的权限

时间:2017-03-06 06:37:00

标签: c# asp.net smtp smtpclient

我得到错误 - “请求类型'System.Net.Mail.SmtpPermission,System,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的权限失败。”

    string name = txtName.Text.Trim();
    string email = txtemail.Text.Trim();
    string phone = txtPhoneNum.Text.Trim();
    string companyname = txtcmpnm.Text.Trim();
    string subject = txtsubject.Text.Trim();
    System.IO.StringWriter sw = new System.IO.StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    System.IO.StreamReader r = null;
    r = new System.IO.StreamReader(Server.MapPath("~/ContactMail.txt"));
    string body = r.ReadToEnd();

    body = body.Replace("<%Name%>", name);
    body = body.Replace("<%email%>", email);
    body = body.Replace("<%phonenum%>", txtPhoneNum.Text.Trim());
    body = body.Replace("<%message%>", txtmsg.Text.Trim());


    try
    {


    using (MailMessage mm = new MailMessage(email, "xyz@gmail.com"))
    {
        mm.IsBodyHtml = true;
        mm.Subject = subject;
        mm.Body = body;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential("krishna@gmail.com", "password");
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
}

1 个答案:

答案 0 :(得分:1)

Web.Cofig: -

检查您的web.config并添加此标记代码...

<trust level="Full" originUrl="" />
<customErrors mode="Off"/>