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;
}