C# - Sending SMTP Email

C# can send emails with the .NET Framework fairlyand password. This will authenticate your connection
easily, especially via the SMTP protocol. The SMTPwith the server. Just make sure your username
protocol is a common way to send emails.includes
Because SMTP emails require an SMTP server toEverything is handled through SmtpClient class. The
send, it is probably easiest to use Google's Gmailclass encapsulates some pretty powerful functions,
server. Thus you'll need a Gmail account, which isincluding adding attachments and sending HTML
simple and free to create. Once you have a Gmailemails. HTML emails are emails that are written with
account sending SMTP emails with C# is a breeze.HTML code and are displayed while web pages.
The trick is to use the System.Net.Mail namespaceAlthough it depends on the email client, most clients
instead of System.Web.Mail. The second namespacecan read HTML emails without a problem, allowing
was replaced by System.Net starting on .NETyour C# application to send emails with images and
Framework 2.0.formatted text. The SmtpClient class also allows
But what do you do to connect to Google's server?developers to add headers, which can fine-tune the
You need a few bits of information. The first is thatbehavior of emails. However be aware that some
Gmail's SMTP server address is The second thing youSMTP servers, like Google's, will ignore certain
need to know is that the C# application mustheaders and just use information from your account.
connect through port 587. How do we know this?For example, setting the From field to something else
The information is provided to developers freely bywill be ignored by GMail, which will automatically set
Google. Other SMTP servers also provide their ownthe From field to your email address.
address and port to connect with.Lastly make sure SSL is enabled on the C#
However there is still one more thing, most SMTPapplication. SSL is an encryption protocol and it is
servers need authentication to send your emails. Thisabsolutely required or Gmail's server will not accept
is where the Gmail account comes into play. Using theyour connection.
NetworkCredential .NET class, specify your username