C# Programming > ASP.NET

C# HTML Email
through Gmail

SMTP Email

C#.NET comes with built-in libraries to send html email easily. Particulary in this article, we'll focus on sending email with Google's Gmail service. You will need a gmail account, which is free to make if you don't already have one.

The trick to send html email messages is to connect to Gmail's SMTP server. SMTP stands for Simple Mail Transfer Protocol and is a popular way to send emails.

NET Namespace

There are two .NET namespaces that can be used to send html email: System.Web.Mail and System.Net.Mail. All you need to know is System.Web.Mail is older, so it is better to use System.Net.Mail (that is assuming you are using .NET Framework 2.0 and up).

The classes in the namespace work for sending emails in both WinForms and ASP.NET.

SMTPClient

The SMTPClient class is easy to use, there are just some properties to consider. For sending html email through Gmail, for example, the host is smtp.gmail.com and the port number is 587. The class can work for other SMTP providers, just specify the host name and the proper port number.

Another important aspect is authentication. It is very common to get an authentication exception when trying to send emails in C#. It is usually for one of two reasons. One is many servers nowadays only accept encrypted connections via the SSL protocol. Setting the property EnableSsl to true is enough to address that issue. The other reason is servers usually require a username/password combination. The NetworkCredential class allows developers to specify the username/password combination.

HTML Email

The last thing to remember is that emails can be plain text or html. HTML emails in C# use standard HTML code, how the message is displayed depends on where the recepient loads the email.

Back to C# Article List

Other C# Articles