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

http://www.johnernaut.com

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.

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

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.