Email form

Discussion in 'ASP.NET / ASP.NET Core' started by Merv Norton, Jan 16, 2010.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I am trying to send a very simple e-mail form through my Discount.Net server. My code is taken directly from Jim Cheshire’s tutorial (www.JimCobooks.com). The following is my code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Net.Mail" %>
    <%@ Import Namespace="System.Text" %>

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

    if (IsPostBack)
    {

    SmtpClient sc = new SmtpClient("localhost.1stSigBdeAssn.org");
    StringBuilder sb = new StringBuilder();
    MailMessage msg = null;

    sb.Append("Email from: " + txtEmail.Text + "\n");
    sb.Append("Message : " + txtMessage.Text + "\n");

    try
    {
    msg = new MailMessage(txtEmail.Text,
    "[email protected]", "Message from Web Site",
    sb.ToString());

    sc.Send(msg);
    }
    catch(Exception ex)
    {
    // something bad happened
    Response.Write("Something bad happened!");
    }
    finally
    {
    if (msg != null)
    {
    msg.Dispose();
    }
    }
    }
    }
    </script>

    <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Enter your e-mail address</title>
    </head>

    <body>

    <form id="form1" runat="server"> Enter your e-mail address:<br />
    <asp:TextBox runat="server" id="txtEmail" Width="203px"></asp:TextBox>
    &nbsp;<asp:RequiredFieldValidator runat="server" ErrorMessage="E-mail is required." id="RequiredFieldValidator1" ControlToValidate="txtEmail" Display="Dynamic">
    </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator runat="server" ErrorMessage="E-mail address invalid." id="RegularExpressionValidator1" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
    </asp:RegularExpressionValidator>
    <br />
    <br />
    Enter your message:<br />
    <asp:TextBox runat="server" id="txtMessage" Width="244px" Height="125px" TextMode="MultiLine">
    </asp:TextBox>
    <br />
    <asp:RequiredFieldValidator runat="server" ErrorMessage="Message is required." id="RequiredFieldValidator2" ControlToValidate="txtMessage">
    </asp:RequiredFieldValidator>
    <br />
    <br />
    <asp:Button runat="server" Text="Submit" id="Button1" Width="116px" />

    </form>
    </body>
    </html>

    The following is the key code from my web.config file:

    <system.net>
    <mailSettings>
    <smtp>
    <network host="localhost.1stSigBdeAssn.org"
    password="********"
    userName="1stsigbdea1" />
    </smtp>
    </mailSettings>
    </system.net>

    What am I missing?
     
  2. You missed posting the error message you see into this thread ;)
     
  3. All I am getting is:

    "something bad happened: which is from the script file whing the "try: fails.

    I am not knolegable in ASP so I do not know how to add any additional code. Go to www.JimCoBooks.com and you can see the original code as written by Jim Cheshire who is a Microsoft employee who helped to create ADP.NET.
     
  4. Change
    Code:
    catch(Exception ex)
    {
    // something bad happened
    Response.Write("Something bad happened!");
    }
    to
    Code:
    catch(Exception ex)
    {
    // something bad happened
    Response.Write(ex.Message);
    }
    If you do this, the real error message will be rendered to the browser and you'll have more information about what is actually failing. At this stage I expect it's the web.config bit that's not setup correctly.

    If that was your real uid/pwd in your first post, the security sensitive bits should have been replaced with ******. I'm sure one of the forum mods will be here to edit it out very soon but if it was, I recommend you change your password asap.
     
  5. I changed the host name to "localhost" and I now get the following error message:
    Mailbox unavailable. The server response was: 5.7.1 Unable to relay for [email protected]

    This indicates to me that there is a problem with the mail server at DISCOUNT.ASP.
     
  6. Problem solved. I searched the Internet for server response 5.7.1 and found out that the problem was in my computer set up, so I tried the form on the web and it worked perfectly. Tomorrow I will fix my computer set up so that it will from my own computer.
    Thanks for your help.
     
  7. mjp

    mjp

    Merv, this is good advice. If that was your password it was exposed for 18 hours or so, which is long enough to cause you problems.
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page