Sending email from web page issue

Last post 05-17-2008 10:41 AM by drocco. 4 replies.

Sort Posts:

  • Sending email from web page issue

    05-16-2008, 10:03 AM
    • Loading...
    • drocco
    • Joined on 03-24-2008, 9:39 PM
    • Posts 383

    UPDATE: See my second post for updated situation

    I had set up a "Contact Us" page on my website that sends email using SMTP and information gathered from text boxes and other inputs, etc

    Everything worked fine when I tested the site locally

    After launching the site, I now get a Security Exception error

    Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

    Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

    You can recreate the error by visiting the site, completing the form, and hitting the "Submit" button

    http://www.pennlighting.com/literature

    I have already tried adding <trust level="Medium" originUrl="" /> to my web.config file, but the ISP has override turned off

    On our ISP's FAQ, it states that all applications are set to Medium by default, so I don't understand why I am having this issue 

    I am waiting to hear back from the ISP, but their tech support's turnaround has been sub-par and I'd like to resolve this issue as soon as possible

    Here is the code behind for the page. I am new to ASP.NET 2.0 and VBScript so if there is anything in my code that could be causing the problem, please let me know. Thanks!

    Imports System.Net.Mail

    Partial Class literature
        Inherits System.Web.UI.Page

        Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click

            Dim sb As New StringBuilder
            sb.Append("Name: ")
            sb.Append(NameBox.Text)
            sb.AppendLine()
            sb.Append("Email: ")
            sb.Append(EmailBox.Text)
            sb.AppendLine()
            sb.Append("Phone: ")
            sb.Append(PhoneBox.Text)
            sb.AppendLine()
            sb.Append("Fax: ")
            sb.Append(FaxBox.Text)
            sb.AppendLine()
            sb.Append("Company: ")
            sb.Append(CompanyBox.Text)
            sb.AppendLine()
            sb.Append("Address: ")
            sb.Append(AddressBox.Text)
            sb.AppendLine()
            sb.Append("City: ")
            sb.Append(CityBox.Text)
            sb.AppendLine()
            sb.Append("State: ")
            sb.Append(StateBox.SelectedValue)
            sb.AppendLine()
            sb.Append("Zip Code: ")
            sb.Append(ZipBox.Text)
            sb.AppendLine()
            sb.Append("Manufacturer: ")
            sb.Append(ManufacturerBox.Text)

            LitForm.ActiveViewIndex = 1

            SendMail("my@email.com", sb.ToString())

        End Sub

        Protected Sub SendMail(ByVal from As String, ByVal body As String)

            Dim Email As New MailMessage(EmailBox.Text, "my@email.com", "Literature Request", body)

            Dim smtpClient As SmtpClient = New SmtpClient

            smtpClient.Host = "smtp.gmail.com"

            smtpClient.Port = "587"

            smtpClient.EnableSsl = True

            Dim credentials As New System.Net.NetworkCredential("user", "pass")

            smtpClient.Credentials = credentials

            smtpClient.Send(Email)

        End Sub

    End Class
     

  • Re: Security Exception Error When Sending Email From Form

    05-16-2008, 10:52 AM
    • Loading...
    • ArminStockner
    • Joined on 09-19-2006, 2:18 AM
    • Germany, Bavaria
    • Posts 252

    This post could help you alot:

    http://forums.asp.net/p/1111145/1711371.aspx 

    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: Security Exception Error When Sending Email From Form

    05-16-2008, 10:54 AM
    • Loading...
    • drocco
    • Joined on 03-24-2008, 9:39 PM
    • Posts 383
    I made it clear in my post what I've already tried and there's nothing new to me in that post. If the bottom line is that there's absolutely nothing I can do than have the ISP make a change on their side, that's kind of what I was asking. I'm assuming that's the case? Thanks for your reply.
  • Re: Security Exception Error When Sending Email From Form

    05-16-2008, 11:39 PM
    Hi Drocco--

    Yes, you are correct. This is something the ISP needs to configure.

    Cheers!
    Dave
  • Re: Security Exception Error When Sending Email From Form

    05-17-2008, 10:41 AM
    • Loading...
    • drocco
    • Joined on 03-24-2008, 9:39 PM
    • Posts 383

    It turns out that I made a mistake... on our ISP's FAQ it instructs you to use "localhost" as the SMTP server.

    Once I changed the SMTP server to "localhost" I no longer received the security exception error, however the email is still not going through.

    I have the form set up on a MultiView so that View1 is the form to be emailed and View2 is a thank you message. In my code behind the script will A) change from View1 to View2 and B) send the email when the form is submitted.

    Right now when I submit the information, the View index is changing to the thank you page, but I am not receiving the email.

    Turns out this is probably a problem on my side after all, any ideas?
     

    Here is the updated code behind

    Imports System.Net.Mail

    Partial Class literature
        Inherits System.Web.UI.Page

        Protected Sub Submit_Click(ByVal sender As Object,

    ByVal e As System.EventArgs) Handles Submit.Click

            Dim sb As New StringBuilder
            sb.Append("Name: ")
            sb.Append(NameBox.Text)
            sb.AppendLine()
            sb.Append("Email: ")
            sb.Append(EmailBox.Text)
            sb.AppendLine()
            sb.Append("Phone: ")
            sb.Append(PhoneBox.Text)
            sb.AppendLine()
            sb.Append("Fax: ")
            sb.Append(FaxBox.Text)
            sb.AppendLine()
            sb.Append("Company: ")
            sb.Append(CompanyBox.Text)
            sb.AppendLine()
            sb.Append("Address: ")
            sb.Append(AddressBox.Text)
            sb.AppendLine()
            sb.Append("City: ")
            sb.Append(CityBox.Text)
            sb.AppendLine()
            sb.Append("State: ")
            sb.Append(StateBox.SelectedValue)
            sb.AppendLine()
            sb.Append("Zip Code: ")
            sb.Append(ZipBox.Text)
            sb.AppendLine()
            sb.Append("Manufacturer: ")
            sb.Append(ManufacturerBox.Text)

            LitForm.ActiveViewIndex = 1

            SendMail("admin@pennlighting.com", sb.ToString())

        End Sub

        Protected Sub SendMail(ByVal from As String, ByVal

    body As String)

            Dim Email As New

    MailMessage("admin@pennlighting.com",

    "admin@pennlighting.com", "Literature Request", body)

            Dim smtpClient As SmtpClient = New SmtpClient

            smtpClient.Host = "localhost"

            Dim credentials As New

    System.Net.NetworkCredential("admin@pennlighting.com",

    "password")

            smtpClient.Credentials = credentials

            smtpClient.Send(Email)

        End Sub

    End Class
     

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter