Dear Friends,
After searching a lot on this topic, I came across various sites where
the solution was available but none of them worked properly. Finally I
decided to give it a try and after lot trials and errors I got a code
that worked perfectly. I have deployed this code in many applications
on different web-servers.
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
System.Net.NetworkCredential AuthInfo = new
System.Net.NetworkCredential("[email protected]", "password");
message.To.Add("[email protected]");
message.To.Add("[email protected]");
message.CC.Add("[email protected]");
message.Bcc.Add("[email protected]");
message.Subject = "Test Mail using System.Net.Mail";
message.From = new MailAddress("[email protected]");
message.Body = "Hi This is a test message";
smtp.Host = "smtp.gmail.com";
smtp.UseDefaultCredentials = false;
smtp.Credentials = AuthInfo;
smtp.Timeout = 20;
smtp.Port = 465;
// use stmp.port = 587 if 465 does not work.
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
try
{
smtp.Send(message);
}
catch (System.Net.Mail.SmtpException ex)
{
// display the error (ex.Message)
}