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?
 
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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.