Code
Try
{
System. Net. Mail. MailMessage mailObj = new System. Net. Mail. MailMessage ();
MailObj. IsBodyHtml = true;
MailObj. Subject = "mail title ";
MailObj. Body = "email Body ";
MailObj. To. Add ("recipient address ");
System. Net. Mail. SmtpClient SmtpMail = new SmtpClient ("smtp.gmail.com ");
MailObj. From = new MailAddress ("sender @ gmail.com", "Your name", System. Text. Encoding. UTF8 );
SmtpMail. Credentials = new System. Net. NetworkCredential ("sender @ gmail.com", "password ");
// Gmail private configuration starts
SmtpMail. Port = 587;
SmtpMail. EnableSsl = true;
// The gmail private configuration is complete.
SmtpMail. Send (mailObj );
Response. Write ("email sent successfully! ");
}
Catch (Exception ex)
{
Response. Write (ex. Message );
}
Of course, it is easier to configure in web. config.
Code
<System.net>
<MailSettings>
<Smtp>
<Network host = "smtp.gmail.com" userName = "sender @ gmail.com" port = "587" password = "*****"/>
</Smtp>
</MailSettings>
</System.net>
The client code is as follows:
Code
SmtpClient client = new SmtpClient ();
MailMessage mm = new MailMessage ("sender @ gamil.com", "recipient @ qq.com", "Ttile", "body ");
Client. Send (mm );
Three lines of code! (Laugh !)