0

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.

6
Contributors
8
Replies
14
Views
7 Years
Discussion Span
Last Post by aspproject
0

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.

0

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) { }
    }
0

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

0

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

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.