I have to generate the mail to their mail address specified at the time of the registration to specify the username and password. So i tried the following code to send email but the email is not generated showing me either "Failure sending mail" or "Connection timed out". So please help me to get out of this problem.

Thanks in Advance

try
            {
                System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
                message.From = txtFrom.Text.Trim();
                MailMessage objMailMessage = new MailMessage();
                objMailMessage.From = new MailAddress(txtFrom.Text.Trim());
                objMailMessage.To.Add(new MailAddress(txtTo.Text.Trim()));
                objMailMessage.Subject = txtsubject.Text.Trim();
                objMailMessage.Body = txtMessage.Text.Trim();
                objMailMessage.IsBodyHtml = false;
                SmtpClient objSmtpClient = new SmtpClient();
                objSmtpClient.Host = "smtp.gmail.com";
                objSmtpClient.Port = 587;
                objSmtpClient.Credentials = new System.Net.NetworkCredential(txtFrom.Text.Trim(), txtPassword.Text.Trim());
                objSmtpClient.UseDefaultCredentials = true;
                objSmtpClient.EnableSsl = true;
                objSmtpClient.Send(objMailMessage);
                lblStatus.Text = "Mail has been sent Successfully";
                lblStatus.ForeColor = System.Drawing.Color.Green;
            }

1.You have to confirm the credential(username and password) with the Gmail.com website
2.If you activated step 2 verification( for asking security code to mobile) or any security question means it gets timeout

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.