Asp contacts page works locally but not on the server

I have an asp site and an asp contact form that I found on the Internet. It works fine on the local machine.

I added it to my server to test it live, and that didn't work. I got a displayed message to display an error and it says the following:

System.Security.SecurityException: permission request of type 'System.Net.Mail.SmtpPermission, System, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' failed. in System.Security.CodeAccessSecurityEngine.Check (object request, StackCrawlMark & ​​stackMark, Boolean isPermSet) with System.Security.CodeAccessSecurityEngine.Check (CodeAccessPermission cap, StackCrawlMark & ​​stackMark) with System.Security.emand System.Net.Mail.SmtpClient.Initialize () at System.Net.Mail.SmtpClient..ctor (host String, port Int32) in contact.btnSubmit_Click (object sender, EventArgs e) in g: \ pleskvhosts \ myweburl \ httpdocs \ contact.aspx.cs: line 33 Action failed: Demand Type of first allowed permission was: System.Net.Mail.SmtpPermission Build zone that failed: MyComputer

Does anyone know what that 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;
    }
+4
source share
2 answers

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

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

For example, if you use GoDaddy, you should set the following in a variable (e.g. * smtp *): System.Net.Mail.SmtpClient

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 in order to properly set up email on . GoDaddy

, full trust, SMTP-. 80, 25, 80 .

+4

.....

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

Source: https://habr.com/ru/post/1546157/


All Articles