0

I have implemented a contact form to a web app but when I try to send the email I get JavaScript runtime errors, http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp at line 37 which is an empty line.

Does anyone have an idea as to what is wrong with my implementation or do I have to change some settings to test it on local host?

//calls the SendMail() and resets the textboxes
    protected void sendBtn_Click(object sender, EventArgs e)
    {
        try
        {
            //here on button click what will done 
            SendMail();
            confirmationLbl.Text = "Your email has been sent to customer support.";
            confirmationLbl.Visible = true;
            subjectTbx.Text = "";
            emailTbx.Text = "";
            nameTbx.Text = "";
            questionTbx.Text = "";
        }
        catch (Exception ex) {

            confirmationLbl.Text = "Your email has failed to send,please check your connection.";
            Console.WriteLine("IOException source: {0}", ex.Message );


        }

//line 37 is here, which is blank
    }

    //method to compose email from textboxes
    protected void SendMail()
    {
        // Gmail Address from where you send the mail
        var fromAddress = emailTbx.Text.ToString(); 
        // any address where the email will be sending
        var toAddress = "brianDoe@gmail.com";
        //Password of your gmail address
        const string fromPassword = "Password";
        // Passing the values and make a email formate to display
        string subject = subjectTbx.Text.ToString();
        string body = "From: " + nameTbx.Text + "\n";
        body += "Email: " + emailTbx.Text + "\n";
        body += "Subject: " + subjectTbx.Text + "\n";
        body += "Question: \n" + questionTbx.Text + "\n";
        // smtp settings
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        // Passing values to smtp object
        smtp.Send(fromAddress, toAddress, subject, body);

    }
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • Your email address is in there, i.e., for all to see on here. – Nick Apr 17 '14 at 19:37
  • You get JavaScript errors in your C# code? Very weird (tip: Try looking at line 37 in your JavaScript... also try posting the error; it's quite important to the debugging process). – Ant P Apr 17 '14 at 19:44
  • I don't think his last name is Doe – Jonesopolis Apr 17 '14 at 19:48
  • You're right, it isn't......... because he changed it - not relevant to continue discussing. – Nick Apr 17 '14 at 19:54

1 Answers1

1

You should check your parsed HTML for JavaScript error rather than C# code. Also, make sure POP/IMAP access is enabled in Gmail under settings.

enter image description here

Maverick
  • 791
  • 9
  • 23
  • I just changed my email settings but I'm not sure what you mean by check the parsed HTML, Where would I find this code? Is it the Html file for that page? – Brian Var Apr 17 '14 at 19:55
  • The aspx and the C# code is parsed into HTML and JavaScript for displaying in the browser. When you open the page, right click and select View Page Source (in Chrome) and check line 37. – Maverick Apr 17 '14 at 19:57
  • Also I'm not sure if this is the error causing the problem, because the exception `ex` is thrown at the button click. – Brian Var Apr 17 '14 at 19:59
  • 1
    Code seems to be fine. You do need to enable POP/IMAP inside Gmail to make it work. Also, what is the exception you get, when you click on the button ? [Chk this](http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – Maverick Apr 17 '14 at 20:03
  • Okay so I printed the exception message being caught and it states this: `The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required` – Brian Var Apr 17 '14 at 20:05
  • Add `smtp.UseDefaultCredentials = false;` to the code. – Maverick Apr 17 '14 at 20:08
  • Still getting the same error after adding that code.. Any ideas as to what it could be? – Brian Var Apr 17 '14 at 20:11
  • Are you running this on a remote server ? Sometimes Gmail blocks the connection, because the request is received from an unknown IP. If that is the case - Login to Gmail first from the remote server, you are trying to the app to familiarize Gmail with that particular IP for your account. – Maverick Apr 17 '14 at 20:15
  • I'm running from visual studio on local host, I tried that too but it didn't work.. I'm guessing if the website was hosted the email should send though? – Brian Var Apr 17 '14 at 20:20