Hi, here's the site I'm currently working on:

Under the 'contact' tab I have some behind the scenes C# code that SHOULD send me an email whenever the submit button is pressed and all fields are validated. This isn't working for some reason. The host I'm using is GoDaddy. I've looked up the smtp host / port and it should be working fine, but it's not. Here's my code:

protected void SendMail()
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(txtEmail.Text);
        mail.To.Add("myemail@gmail.com");
        mail.Subject = "Contact Me";
        mail.IsBodyHtml = true;
        mail.Body = "From: " + txtName.Text + "<br />";
        mail.Body += "Email: " + txtEmail.Text + "<br />";
        mail.Body += "Comments: " + txtComments.Text + "<br />";
        mail.Priority = MailPriority.Normal;


        SmtpClient client = new SmtpClient();
        client.Credentials = new NetworkCredential("godaddy_email_username", "godaddy_email_password");
        client.Host = "relay-hosting.secureserver.net";
        client.Port = 25;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Send(mail);        
    }

    protected void Reset()
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtComments.Text = "";
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            SendMail();
        }
        catch (Exception) { }

        Reset();
    }

I'm not getting an email. Any help would be appreciated, thank you.

Dani AI

Generated

Quick summary and the real culprits (why mail often looks like "it worked" but nothing arrives)

The original problem in this thread was two common mistakes that hide the real error: an empty catch { } that swallows the exception, and using the submitter's address as the message From. The empty catch removes any diagnostic feedback; many hosts and relays will reject or rewrite messages whose From is an external address. eventually got a working Gmail-based send (thanks also to 's link), but the host-side rules still matter.

Host rules to check (GoDaddy specifics)
GoDaddy’s docs explain that form mail from hosted sites often must use the host relay (different hostnames for Linux vs Windows), that port 25/relay is commonly used, and that the sender should be an address on a domain you control (and add an SPF record like include:secureserver.net). If the app runs on GoDaddy, external SMTP can be blocked and arbitrary From addresses cause authentication/failure. (godaddy.com)

Gmail notes (when switching to an external SMTP)
Using Gmail as the SMTP server (what did) is fine, but Google changed access rules: plain account passwords for third‑party SMTP are no longer supported for many accounts — use an App Password (with 2‑step verification) or OAuth2, and use smtp.gmail.com with TLS (587) or SSL (465). (emailarchitect.net)

Concrete, safe pattern to use (avoid the common traps)

  • Set MailMessage.From to a site-owned address.
  • Add the visitor email in ReplyToList so replies go to them.
  • Stop swallowing exceptions; log the exception text so the real SMTP error is visible.

Example (minimal, different from code already posted in the thread):

var msg = new MailMessage();
msg.From = new MailAddress("no-reply@yourdomain.com", "Site Contact");
msg.To.Add("owner@yourdomain.com");
msg.ReplyToList.Add(new MailAddress(txtEmail.Text));
msg.Subject = "Contact form";
msg.IsBodyHtml = true;
msg.Body = bodyHtml;

try {
  using(var smtp = new SmtpClient()) {
    smtp.Send(msg);
  }
} catch (Exception ex) {
  System.Diagnostics.Trace.WriteLine(ex.ToString()); // record the real failure
}

Use config for credentials (example mailSettings shown below), and test both with the provider’s recommended host/port and with a simple telnet/openssl test if connections fail.

<system.net>
  <mailSettings>
    <smtp from="no-reply@yourdomain.com">
      <network host="smtp.gmail.com" port="587" enableSsl="true"
               userName="your@gmail.com" password="APP_PASSWORD_HERE" />
    </smtp>
  </mailSettings>
</system.net>

See the .NET MailMessage API for using ReplyToList and related fields. (learn.microsoft.com)

Quick checklist: remove empty catch blocks, log full exception text, confirm host/port from the host (GoDaddy) or mail provider, ensure the From domain is under control and SPF/DKIM are correct, and if using Gmail generate an App Password or use OAuth2. (godaddy.com)

Recommended Answers

All 8 Replies

Any ideas? I still can't get this to work.

Haven't seen you post in a while adatapost :P Thanks for the link, I'll come back if I can / cannot get it to work.

Thanks again for the link, I finally got it working. I'll post the working code in case anyone else runs into this problem. This code will work if your sending out the email via Gmail.

protected void SendMail()
    {
        var fromAddress = new MailAddress("yourgmail@gmail.com", txtName.Text);
        var toAddress = new MailAddress("recipient@recipient.com", "Recipient");
        const string fromPassword = "gmailPassword";
        const string subject = "Contact Me";
        string body = "From: " + txtName.Text + "<br />";
        body += "Email: " + txtEmail.Text + "<br />";
        body += "Comments: " + txtComments.Text + "<br />";

        var smtp = new SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress.Address, fromPassword);
            smtp.Timeout = 20000;
        }

        using (var message = new MailMessage(fromAddress, toAddress))
        {
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;

            smtp.Send(message);
        }

    }

Usage:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            SendMail();
        }
        catch (Exception) { }
    }

you can save this email settings in web.config file to make your code more clear

Yes , I post this message for papanyquiL! please I need to know , what namespaces you use for this form. thank You !

emailid--write ur emailid
pwd-->ur id pwd
u need some modification please do that and if find problem then right in my comment so that i can go 4 it

"replace " to "localhost" for SMTP port using .net email sending

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.