954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C# ASP.NET 3.5 Contact Form

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 + "";
        mail.Body += "Email: " + txtEmail.Text + "";
        mail.Body += "Comments: " + txtComments.Text + "";
        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.

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
 

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

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
 
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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.

papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
 

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 + "";
        body += "Email: " + txtEmail.Text + "";
        body += "Comments: " + txtComments.Text + "";

        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) { }
    }
papanyquiL
Junior Poster
168 posts since May 2009
Reputation Points: 55
Solved Threads: 18
 

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

nil
Light Poster
30 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

onetv
Newbie Poster
1 post since May 2011
Reputation Points: 10
Solved Threads: 0
 

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

sandeepchrs
Newbie Poster
6 posts since May 2011
Reputation Points: 10
Solved Threads: 1
 

"replace relay-hosting.secureserver.net" to "localhost" for SMTP port using .net email sending

aspproject
Junior Poster in Training
Banned
78 posts since Apr 2011
Reputation Points: 1
Solved Threads: 3
Infraction Points: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: