Sending Simple E-Mail in C# - Theory
(Page 2 of 4 )
Before we begin, let’s present what SMTP servers are all about. It is beyond the scope of this article to explain the history and the inner workings of SMTP servers. Thus, we’ll focus on just the absolutely necessary things and won’t bore you with extra details. SMTP stands for Simple Mail Transfer Protocol. It is the standard protocol, text-based, for e-mail transmissions over the Internet.
Our purpose is to send e-mails; therefore, we are going to work with SMTP servers. It makes sense, right? In our code we’re going to “compose” our message, add the necessary headers such as where it is coming from, where it is going, its subject, and the body of the mail. Right after all of this, we’re ready to send it toward the recipient. We need a free and public SMTP server to do this.
In our application we need to specify the address of the SMTP server through which our mail is supposed to go. Your program will contact the SMTP server, establish the connection, and then send the message along. There are more than a few free and public SMTP servers. For example, you can always use the one that your ISP supplies and maintains. Say your ISP is EarthLink, then it is at smtp.earthlink.com.
On the other hand, you can also use the massively popular Google’s SMTP server [Google Mail]. This is a bit trickier because they require network credentials, basically any valid GMail user account, to establish the connection with the server. Along with this, we also need to enable an SSL connection (encryption).
Google’s approach is safer because they do not allow connections from anonymous sources. As long as you supply a valid GMail username and password, you're good; you log into your GMail account and send the e-mail through that source e-mail address. Regardless of the fields specified in your code, your Google account will become the source of the message that’s going to be sent. Plus, SSL is obviously safe.
On the other hand, there are numerous SMTP servers that allow connections from anybody; this includes anonymous sources. They do not require credentials (no username or password); neither do they offer SSL to encrypt the connection. Thus, they are often exploited by spammers to send out a massive amount of spam e-mail. Why is that? Because they accept anything as “source” of the mail, say jonny@bla.net.
As long as the spammer is behind a strong and non-transparent proxy to hide their IP address, then thousands if not millions of mails can be sent out through an SMTP server like this using a fake e-mail address. I'm not endorsing this; I'm mentioning it because it is useful to know what credentials are about and why some servers absolutely require these kinds of credentials. It all can be narrowed down to safety.
As a final note, these days, more often than not, the SMTP servers that are provided by ISPs are not requiring credentials; neither are those servers from “cheap” network hosting companies. A quick Google search on “free SMTP servers” turns up a huge list, out of which surely you can pick a few that are still valid.
Thankfully, there are pre-made libraries and components already included in the dot net Frameworks that can help us out during this e-mail sending task. We don’t need to write the transmissions functions, hand-shaking requirements, and lots of extra transaction stuff; we just rely on the functions that are readily available to us in .NET.
In the next section you will see that we start the “using component_name” formula to include (specify) the components we are going to use within our application. This is important because the compiler will know where to find a function when you call it. This is the key with commercial components and libraries too. You purchase a component and then you can use all of its functions any time, anywhere.
Now that you know the basics, let’s see those three implementations in C#.
Next: Implementations >>
More C# Articles
More By Barzan "Tony" Antal