C# Code Snippet to Send Email

Microsoft.Net framework provides System.Net.Mail Namespace for handling SMTP and email related utilities. SmtpClient class allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP) where as MailMessage class represents an e-mail message that can be sent using the SmtpClient class.

Please note that System.Web.Mail namespace should not be used any more as most of the functionalist in this namespace are now obsolete.

Here are few code snippets to send email in C# using SMTP:

Gist to Send email via Gmail

And if you want to attach a file, here is code to send email with attachment: