Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

Last post 05-09-2008 10:41 AM by pradeepsahoo. 6 replies.

Sort Posts:

  • Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-06-2008, 11:06 AM
    • Loading...
    • SamU
    • Joined on 07-29-2002, 4:09 PM
    • West Palm Beach, FL
    • Posts 517

    Hi,

    This is the error I'm getting from a simple ASP.NET page that collects and emails info in a form. In this case, I'm not using code behind and getting this error message. If I put my code in code behind, everything works. How do I fix this issue so that this simple form will work?

    For those who are wondering why not use code behind: The graphic designer wants to create forms and not depend on us to wire things up in the backend. So I gave her some basics of ASP.NET and C# and she's very happy. But every once in a while, she gets stuck.

    Thanks,

    Sam
  • Re: Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-06-2008, 11:45 AM

    Can you send us/attach the sample page or the sample code?

     One important thought to remember: Make sure that the destination email is not set from the client side via a javascript variable or hidden field or else your form can be used to spam people on your behalf.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    My Blog
    If you get the answer to your question, please mark it as the answer.
  • Re: Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-06-2008, 12:23 PM
    • Loading...
    • SamU
    • Joined on 07-29-2002, 4:09 PM
    • West Palm Beach, FL
    • Posts 517

    Here is the code. The only thing I need to add is that in the web.config file I defined the emails (EmailTo and EmailCC).

    <%@ Page Language="C#" AutoEventWireup="true" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%@ Import Namespace="System.Text" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            P1.Visible = true;
            P2.Visible = false;
        }

        public void btnSubmit_Clicked(object sender, EventArgs e)
        {
            
            string strFirstName = FirstName.Text;
            string strLastName = LastName.Text;
            string strEmail = Email.Text;
            string strMessage = MyMessage.Text;


            StringBuilder sb = new StringBuilder();
            sb.Append("<font color='maroon'><b>Web Site Message</b></font><br /><br />");
            sb.Append("<font color='maroon'>Message Sent by:</font> ");
            sb.Append(strFirstName);
            sb.Append(" ");
            sb.Append(strLastName);
            sb.Append("<br />");
            sb.Append("<font color='maroon'>Message:</font>");
            sb.Append("<br />");
            sb.Append(strMessage);


            MailMessage msg = new MailMessage();
            msg.To.Add(new MailAddress(ConfigurationManager.AppSettings["EmailTo"].ToString()));
            msg.CC.Add(new MailAddress(ConfigurationManager.AppSettings["EmailCC"].ToString()));
            msg.From = new MailAddress(strEmail);
            msg.IsBodyHtml = true;
            msg.Subject = "Contact Form Message";
            msg.Body = sb.ToString();


            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Send(msg);
                lblMessage.Text = "Your response has been received. Thank you.";
            }
            catch
            {
                lblMessage.Text = "There was a system error. We could not receive your response. Please try again.";
            }

            P1.Visible = false;
            P2.Visible = true;
        }
    </script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Test Form</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:Panel ID="P1" runat="server">
                <asp:TextBox ID="FirstName" runat="server" Width="250px" /><br />
                <asp:TextBox ID="LastName" runat="server" Width="250px" /><br />
                <asp:TextBox ID="Email" runat="server" Width="250px" /><br />
                <asp:TextBox ID="MyMessage" runat="server" Width="250px" /><br />
                <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Clicked" Text="Submit" />
            </asp:Panel>
            <asp:Panel ID="P2" runat="server">
                <asp:Label ID="lblMessage" runat="Server" />
            </asp:Panel>
        </form>
    </body>
    </html>

    Thanks,

    Sam
  • Re: Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-06-2008, 2:19 PM
    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: Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-06-2008, 3:14 PM
    • Loading...
    • SamU
    • Joined on 07-29-2002, 4:09 PM
    • West Palm Beach, FL
    • Posts 517

    I'm trying to access the System.Net.Mail namespace from btnSubmit_Clicked method which is declared as public. What am I missing here?

    Thanks,

    Sam
  • Re: Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-06-2008, 5:33 PM

    I think you have done one of those bugs that is not what it looks like but the compiler understood something else and made you look in the wrong places :)

    You are writing this in your code:

    Message.Text = "Your response has been received. Thank you.";

    I think you mean to say

    lbl1.Text = "Your response has been received. Thank you.";

    However it just happens that when you miswrote it as Message.Text, it just happens that there is

    System.Net.Mail.Message.Text which the compiler assumed that is what you meant by Message.Text. And Message.Text is inaccessabile indeed.

    Even if you write in your code behind:

    System.Net.Mail.Message.Text = "test";

    You will get the same error :)

    Do not bang your head against the wall :) and be careful you have used Message.Text twice and not just once, change both occurances.

     

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    My Blog
    If you get the answer to your question, please mark it as the answer.
  • Re: Need help with 'System.Net.Mail.Message' is inaccessible due to its protection level

    05-09-2008, 10:41 AM

     Use this It will work.....

    Imports System.Net.Mail

      Dim mailbody As String

            Dim c As New System.Net.Mail.SmtpClient("smtpName")
            Dim toMail As New MailAddress("TOemailAddress")
            Dim FromMail As New MailAddress("FROMemailId", "SenderName")


            Dim m1 As New System.Net.Mail.MailMessage(FromMail, toMail)
            m1.Bcc.Add(FromMail)

            '---------------------------------------mail View-------------------------

            mailbody = "Message to send in the body"



            '-------------------------------End Mail View ------------------------------------

            m1.IsBodyHtml = True
            m1.Body = mailbody
            m1.Subject = "Your Password"
            c.Send(m1)

     

     

    ======================================
    Regard,
    Pradeep Sahoo
    Sr. Sofware Developer
    Don't forget to click "Mark as Answer" on the post that helped you.
Page 1 of 1 (7 items)