C#
  Home arrow C# arrow Page 2 - Sending Simple E-Mail in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Sending Simple E-Mail in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2008-08-12

    Table of Contents:
  • Sending Simple E-Mail in C#
  • Theory
  • Implementations
  • Final Thoughts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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#.

    More C# Articles
    More By Barzan "Tony" Antal


       · MessageBox.Show(ex.Message, "Error: " + ex.Message, MessageBoxButtons.OK,...
     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT