C# contact form code only works offline?

I have a contact us page form written in C# (Code attached), it runs perfectly when testing un-compiled (offline), however, once deployed it fails! Any help would be appreciated.

I have double checked all usernames, passwords and email addresses, but obviously if that was the problem it would not send when testing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Text;

public partial class contact_us : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage EmailMsg = new MailMessage();
            EmailMsg.From = new MailAddress("noreply@somwhere.com", " Contact Us");
            EmailMsg.To.Add(new MailAddress("EMAIL ADDRESS", "Contact Us"));
            EmailMsg.Subject = "Contact Us Page Message";
            EmailMsg.Body = "Name: " + txtName.Text + "<br />Email: " + txtEmail.Text + "<br />Comments: " + txtComments.Text;
            EmailMsg.IsBodyHtml = true;
            EmailMsg.Priority = MailPriority.Normal;
            SmtpClient MailClient = new SmtpClient("smtp.1and1.com");
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "noreply@somewhere.com";
            NetworkCred.Password = "PASSWORD";
            MailClient.Port = 587;
            MailClient.Send(EmailMsg);

            lblMessage.Text = "Thank you! Your message has been sent successfully. We will respond as soon as possible to your enquiry.";

            txtName.Text = "";
            txtEmail.Text = "";
            txtEmailConfirm.Text = "";
            txtComments.Text = "";
        }
        catch
        {
            lblMessage.Text = "Sorry! Your message did NOT send. Please check all form field elements and try again.";
            
        }
    }
    protected void btnClrFrm_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtEmailConfirm.Text = "";
        txtComments.Text = "";
        lblMessage.Text = "";
    }
}


Code block in web.config:

    <system.net>
        <mailSettings>
            <smtp from="noreply@somewhere.com">
                <network host="smtp.1and1.com" password="PASSWORD" userName="noreply@somewhere.com" port="587"/>
            </smtp>
        </mailSettings>
    </system.net>

Open in new window

SirRoshuaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Can you ping smpt.landl.com from the deployment machine? If so, can you telnet to port 25 (or other if server is configured differently) from the same machine?
0
Wayne BarronAuthor, Web DeveloperCommented:
Also.
What fails? Do you receive any errors, if so, please provide them.
0
SirRoshuaAuthor Commented:
Can ping 1and1.com, however no Telnet capability. 1and1.com is one of the largest hosting companies in the world. If it helps the following comes directly from their site: To send e-mail using the SMTP-Server, activate SMTP Authentication. Please do not use the Secure Password Authentication (SPA) option. Optionally, port 587 can also be used in addition to the default port (25) RFC 2476.
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

SirRoshuaAuthor Commented:
Don't know what fails, neither do I see any errors. How would I go about catching it?
0
Wayne BarronAuthor, Web DeveloperCommented:
I am just getting started in ASP.NET, so the only thing that I can suggest and I am not sure if it will work with .net or not, and that is to make sure that you have show Error Reports to the browser.
(I am a ASP Classic Developer)

IE => Tools | Internet Options | Advanced |
Show friendly HTTP Error messages (Uncheck)

Apply | OK

See if you can see some errors. If not, then maybe one of the others EE's can assist in getting an error if any sent to the browser.

Good Luck
Carrzkiss
0
drypzCommented:
Hi! Try to remove this in your code behind since you already declared it in web.config

//remove this part
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = "noreply@somewhere.com";
            NetworkCred.Password = "PASSWORD";
            MailClient.Port = 587;
0
käµfm³d 👽Commented:
Do you actually see the message:  "Sorry! Your message did NOT send. Please check all form field elements and try again."

In other words, is an exception being generated? You can change line 41 in OP to something like:

    catch (Exception ex)

then you'll be able to inspect the Message property of the Exception object.
catch (Exception ex)
{
    lblMessage.Text = ex.Message;    
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SirRoshuaAuthor Commented:
OK, finally have an error message showing:

Insufficient permissions for setting the configuration property 'port'.

So I removed the port specific syntax from the page and the web.config file, and it worked!!!

0
SirRoshuaAuthor Commented:
Although this solution did not actually solve the problem, it did allow me to see what the problem was so I could solve it. But I am not about to award myself points!!
0
Wayne BarronAuthor, Web DeveloperCommented:
Glad that my asking about the "error" got you where you needed to be at.
Points or no points, glad you have it working.

Good Luck
Carrzkiss
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.