There is no need to hardcode all smtp settings in your code. Put them in web.config instead. This way you can encrypt these settings if needed and change them on the fly without recompiling your application.
<configuration>
<system.net>
<mailSettings>
<smtp from="example@domain.com" deliveryMethod="Network">
<network host="smtp.gmail.com" port="587"
userName="example@domain.com" password="password"/>
</smtp>
</mailSettings>
</system.net>
</configuration>
End when you send email just enable SSL on your SmtpClient:
var message = new MailMessage("navin@php.net");
// here is an important part:
message.From = new MailAddress("example@domain.com", "Mailer");
// it's superfluous part here since from address is defined in .config file
// in my example. But since you don't use .config file, you will need it.
var client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);
Make sure that you're sending email from the same email address with which you're trying to authenticate at Gmail.
Note: Starting with .NET 4.0 you can insert enableSsl="true" into web.config as opposed to setting it in code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…