2017-03-06 21 views
1

」的權限即時獲取錯誤 - 「請求獲得'System.Net.Mail.SmtpPermission,System,Version = 2.0.0.0,Culture =中立,PublicKeyToken = b77a5c561934e089'失敗。「獲取錯誤 - 請求獲得類型爲「System.Net.Mail.SmtpPermission

代碼

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, "[email protected]")) 
    { 
     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("[email protected]", "password"); 
     smtp.UseDefaultCredentials = true; 
     smtp.Credentials = NetworkCred; 
     smtp.Port = 587; 
     smtp.Send(mm); 
     ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true); 
    } 
} 

回答

1

Web.Cofig: -

檢查你的web.config並添加此標記代碼...

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

在標籤我需要寫,即時通訊使用.net 3.5框架 – Swapna

+0

這兩個代碼.. @ Swapna –

+0

我已經添加這些標籤仍然有相同的錯誤。 – Swapna