我在网上找到了asp网站和asp联系表格。它在本地机器上运行完美。
我将它添加到我的服务器以进行实时测试,但它无效。我收到显示错误的显示消息,它说:
System.Security.SecurityException:请求类型的权限 'System.Net.Mail.SmtpPermission,System,Version = 4.0.0.0, Culture = neutral,PublicKeyToken = b77a5c561934e089'失败。在 System.Security.CodeAccessSecurityEngine.Check(对象需求, 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:第33行动作 失败的是:Demand失败的第一个权限的类型 是:System.Net.Mail.SmtpPermission组件的区域 失败的是: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;
}
答案 0 :(得分:4)
确保 Web.Config
文件的 trust level
设置为 full
:
<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
您还需要按照 here 这一页面正确配置
GoDaddy
上的电子邮件。
在某些情况下,如果无法实现full trust
,则安全级别较低将不允许您指定SMTP端口。您的ISP指定端口 80 ,但有时您可以使用默认值端口25 如果80不起作用。
答案 1 :(得分:-1)
这样做.....
List<people> lstPeeps = new List<people>();
foreach (var item in ListBox1.Items)
{
if (item.Contains("@")) //This checks to see if it's an email address...
lstPeeps.Add(item as peep.m_sEmail);
else lstPeeps.Add(item as peep.m_sName);
foreach (var peep in lstPeeps)
MessageBox.Show(peep.name + "\t" + peep.email);