简体   繁体   中英

Asp contact page works locally but not on a server

I have an asp website and a contact form in asp that I found online. It runs perfectly on the local machine.

I added it to my server to test it live, and it didn't work. I got the display message to display the error, and it says this:

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

Does anyone know what this means?

My code for contact.aspx.cs is

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;
    }

Make sure your Web.Config file has the trust level set to full :

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

For example, if you are using GoDaddy , you must set the following in System.Net.Mail.SmtpClient variable (eg *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 .

In some cases, if you cannot achieve full trust , having a lower security level will not allow you to specify an SMTP port. Your ISP specifies port 80 , but sometimes you can use the default which is port 25 if 80 doesn't work.

Do this.....

mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = relay-hosting.secureserver.net;
smtp.EnableSsl = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM