How to Send E-Mail Via Gmail in C#


Well, you can find this kind of information almost every where but I wanted to give a slick and concise information on how to send e-mail in C# using Gmail Servers.

Here is the code, I believe you can understand once you walk thru it :

MailMessage _message = new MailMessage(new MailAddress("email@gmail.com", "Info"), new MailAddress(_email));
                _message.Subject = "Password Recovery";
                _message.Body = "Dear User
This is your password : " + _password + ".

Please keep it secure.

"; _message.IsBodyHtml = true; SendEmail(_message) public void SendEmail(MailMessage message) { EmailConfig _config = new EmailConfig("SystemAccount"); message.From = new MailAddress(_config.UserName, _config.From); message.Body += _config.Signature; SmtpClient client = new SmtpClient(); client.EnableSsl = Convert.ToBoolean(_config.EnableSSL); client.Host = _config.Host; client.Port = _config.Port; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new System.Net.NetworkCredential(_config.UserName, _config.Password); client.Send(message); }

1 comments:

Aaron said...

Normally, I just grabbed this piece of code out of my project so it could look a little bit weird but this is why we have comment section right :)