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:pleskvhostsmyweburlhttpdocscontact.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("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afc2d6cac2cec6c3cecbcbddcadcdcefc8c2cec6c381ccc0c2">[email protected]</a>");
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("<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="026f7b676f636b6e6366667067717142656f636b6e2c616d6f">[email protected]</a>", "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;
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 (e.g. *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.
Method 2
Do this…..
mm.IsBodyHtml = false; SmtpClient smtp = new SmtpClient(); smtp.Host = relay-hosting.secureserver.net; smtp.EnableSsl = false;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0