0

This is giving me quite the headache. I've pretty much code recycled this multiple times, and it worked perfectly fine, but for some reason I'm catching an error. I can't see the error because it's only an issue on the published site. When I run it locally through VS it works perfect. Any ideas?

Code Behind:

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        SqlDataAdapter cmd = new SqlDataAdapter();

        try
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("email@gmail.com");
            mail.To.Add("email@gmail.com");
            mail.Subject = ("New Inquiry!");
            mail.Body = (nameTextBox.Text + "\n" + emailTextBox.Text + "\n" + messageTextBox.Text);

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("email@gmail.com", "Passwrd");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);
            string script = "alert(\"Thank you for your inquiry.\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true);
            nameTextBox.Text = "";
            emailTextBox.Text = "";
            messageTextBox.Text = "";
        }
        catch
        {
            string script = "alert(\"There was an error submiting your information.\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                   "ServerControlScript", script, true);
        }
    }

aspx code:

<section class="qry" id="contact" >
  <div class="container text-center">
    <div class="row">
                             
      <div class="col-md-3">
        <div class="form-group">
          <asp:TextBox type="name" ID="nameTextBox" class="form-control" required="required" placeholder="Name" runat="server"></asp:TextBox>
            </div>
         </div>
      <div class="col-md-3">
        <div class="form-group">
          <asp:TextBox type="email" ID="emailTextBox" class="form-control" required="required" placeholder="Email address" runat="server"></asp:TextBox>
            </div>
          </div>
      <div class="col-md-3">
        <div class="form-group">
          <asp:TextBox type="message" ID="messageTextBox" class="form-control" required="required" placeholder="Message" runat="server"></asp:TextBox>
            </div>
          </div>
      <div class="col-md-3">
        <div class="form-group">
          <asp:Button ID="submitButton" runat="server" type="submit" class="btn btn-primary" value="Send Inquiry" Text="Send Inquiry" OnClick="SubmitButton_Click"/>
            </div>
          </div>
        </div>
     </section>
TheBluePrint
  • 13
  • 1
  • 6
  • Where are you publishing to? Do they have mail services enabled? What is the exception thrown? Either temporarily remove the try/catch or dump it into an `asp:litteral` hidden inside an HTML comment. Also consider [configuring your mail settings in web.config](https://stackoverflow.com/questions/19233108/how-to-configure-smtp-settings-in-web-config) in case you need to use different for local vs published. – Jon P Nov 13 '20 at 00:56
  • If your website does not have an SSL certificate enabled, then you might want to set it to false. So, try setting `SmtpServer.EnableSsl = false;` then try. – Jamshaid K. Nov 13 '20 at 01:08
  • I'm using myWindowsHosting.com as my host. I contacted their help desk and they didn't help much. I can't see the exception that its throwing because its only an issue when I try and send the email on the website. When debugging it works like a charm. Do you think the host is blocking SMTP traffic on my domain? – TheBluePrint Nov 13 '20 at 01:08
  • I set SmtpServer.EnableSsl = false; and still nothing :( – TheBluePrint Nov 13 '20 at 01:16
  • Then you probably want to write the exception you are getting. Write the catch block like this: `catch(Exception ex) { Response.Write(ex.Message); }` – Jamshaid K. Nov 13 '20 at 01:19
  • Change `catch` to `catch(Exception e)`, and then change your alert in catch section to `string script = $"alert({e.Message});";`. Publish it and check your exception message – Mofid.Moghimi Nov 13 '20 at 01:22

0 Answers0