我有一个 asp 网站和一个在网上找到的 asp 联系表。它在本地机器上完美运行。
我将其添加到我的服务器以进行实时测试,但没有成功。我得到了显示错误的显示消息,它是这样说的:
System.Security.SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) at System.Security.CodeAccessPermission.Demand() at System.Net.Mail.SmtpClient.Initialize() at System.Net.Mail.SmtpClient..ctor(String host, Int32 port) at contact.btnSubmit_Click(Object sender, EventArgs e) in g:\pleskvhosts\myweburl\httpdocs\contact.aspx.cs:line 33 The action that failed was: Demand The type of the first permission that failed was: System.Net.Mail.SmtpPermission The Zone of the assembly that failed was: MyComputer
谁知道这是什么意思?
我的 contact.aspx.cs 代码是
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = new MailAddress(TheirEmail.Text);
mailMsg.To.Add("myemailaddress@gmail.com");
mailMsg.IsBodyHtml = true;
mailMsg.Subject = "Contact Question!";
mailMsg.Body = "Contact Details" + "<b>Name:</b>" + TheirName.Text + " <br/> <b>Email - address :</b>" + TheirEmail.Text + "<br/> <b>Comments :</b>" + Comments.Text;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
mailMsg.Priority = MailPriority.Normal;
smtp.Credentials = new System.Net.NetworkCredential("myemailaddress@gmail.com", "mypassword");
smtp.Timeout = 25000;
smtp.EnableSsl = true;
smtp.Send(mailMsg);
TheirEmail.Text = "";
TheirName.Text = "";
Comments.Text = "";
DisplayMessage.Text = "Thank you. Your contact details and feed back has been submitted.";
DisplayMessage.Visible = true;
}
catch (Exception ex)
{
DisplayMessage.Text = ex.ToString();
DisplayMessage.Visible = true;
}
最佳答案
确保您的 Web.Config
文件的 trust level
设置为 完整
:
<configuration>
<system.web>
.....
<trust level="Full" originUrl=""/>
</system.web>
</configuration>
例如,如果您使用的是 GoDaddy
,则必须在 System.Net.Mail.SmtpClient
变量中设置以下内容(例如 * smtp *):
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net", 25);
smtp.EnableSsl = false; // check if your ISP supports SSL
You will also need to follow this page here to properly configure Email on
GoDaddy
.
在某些情况下,如果您无法实现完全信任
,则具有较低的安全级别将不允许您指定 SMTP 端口。您的 ISP 指定端口 80,但有时如果 80 不起作用,您可以使用默认端口 端口 25。
关于c# - Asp 联系页面在本地工作但不在服务器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433635/