Try with below code ..it worked for me.
public void SendMail(string to, string subject, string body)
{
string uname = WebConfigurationManager.AppSettings["name"].ToString();
string pwd = WebConfigurationManager.AppSettings["pwd"].ToString();
int port = Convert.ToInt32(WebConfigurationManager.AppSettings["clientport"].ToString());
string host = WebConfigurationManager.AppSettings["SMTPMailServer"].ToString();
MailMessage message = new MailMessage();
message.To.Add(new MailAddress(to));
message.From = new MailAddress(uname);
message.Subject = subject;
message.Body = body;
message.BodyEncoding = System.Text.Encoding.UTF8;
message.SubjectEncoding = System.Text.Encoding.UTF8;
SmtpClient client = new SmtpClient();
client.Port = port;
client.Host = host;
System.Net.NetworkCredential nc = new System.Net.NetworkCredential(uname, pwd);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = nc;
message.IsBodyHtml = true;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
client.Send(message);
}
public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
// replace with proper validation
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
else
return false;
}
and in web.config file set username,password and host like..
<add key="SMTPMailServer" value="smtp.gmail.com"/>
<add key="name" value="ur mail id"/>
<add key="pwd" value="set password here"/>
<add key="clientport" value="587"/>
<add key="EnableSSLforMail" value="true"/>