C#
  Home arrow C# arrow Page 3 - 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# - Implementations


    (Page 3 of 4 )

    The first implementation relies on the System.Web.Mail component that is part of the .NET 1.0 and 1.1 Framework. This is useful for those of you coding in older environments. Check out the attached block of code.

    // works only in pre-.NET 2.0. Frameworks (1.0 or 1.1)

    . . .

    using System.Web.Mail;

    . . .

    SmtpMail.SmtpServer = "smtp.mail.com";

    string from = "jondoe@bla.net";

    string to = "justme@isp.com";

    string subject = "voila";

    string body = "sdfsdfsdf, yes, it's all about sdfsdfsdf.";

    SmtpMail.Send(from, to, subject, body);

    As you can see at first we specify the component that we’ll use later on. Right after that we need to specify the SmtpServer. This is the address of the SMTP server. Then we also need those four strings of “from address,” “to address,” “subject,” and “body” of the e-mail message that is going to be sent. Once we get all of this prepared, we just call the Send function from the SmtpMail class— voila!

    For extensive documentation regarding the capabilities of System.Web.Mail components I strongly and wholeheartedly suggest reading this website.

    Now let’s see the second solution. Here we’re going to use the System.Net and System.Net.Mail components, all located within the currently latest frameworks.

    // works only in post-.NET 2.0. Frameworks (2.0, 3.5)

    . . .

    using System.Net;

    using System.Net.Mail;

    . . .

    MailMessage mail = new MailMessage();

    mail.From = new MailAddress("jondoe@bla.net");

    mail.To.Add("esther@mybusiness.net");

    mail.Subject = "voila";

    mail.Body = "sdfsdfsdf, yes, it's all about sdfsdfsdf.";

    SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);

    smtp.EnableSsl = true;

    smtp.Credentials = new NetworkCredential("your_account@gmail.com", "your_password");

    try

    {

    smtp.Send(mail);

    }

    catch (SmtpException ex)

    {

    MessageBox.Show(ex.Message, "Error: " + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    Don’t be fooled by the length of the above block of code. It isn’t harder; it is just a little bit more extensive because I’ve opted for the GMail solution where both SSL encryption and the necessary Network Credentials are to be supplied in order to establish the connection with their SMTP server. Also, error handling isn’t neglected!

    On the first lines we specify the components. We create a new MailMessage object called mail. Then we add content to its fields (from, to, subject, body). We head over to add the SMTP server details as well. The port we’re going to use here with GMail’s server is 587. We use EnableSsl to enable SSL (true is the boolean variable). And ultimately, we create a new NetworkCredential using your GMail account information.

    Since you are going to do all of this just for yourself to learn and try things out, nobody is going to steal your password or anything. It is safe as long as you do not give out your code, however, you shouldn’t ever commercialize codes from tutorials because they are always way too simplistic and monetizing them is close to impossible. Anyway, play around and see how it works.

    The try and catch block will “pick up” if there are any errors and you will get an error message box if that’s the case. You may get something like connection timed out (Wrong port? Slow connection?) or transfer failed (Wrong address? Wrong account information?) and so forth. If the mail goes through all right, you won’t get a message box. Have fun!

    Once again, don’t forget to check out this site for extensive documentation on the capabilities, functions, and abilities of System.Net.Mail component. It is worthwhile to look into this because this isn’t obsolete like the earlier System.Web.Mail.

    And here comes the final commercial example, using the Chilkat Mail component coming from Chilkat Software. You can download it from here. Keep in mind that you need to send any string through the UnlockComponent method in order to enter into the trial period. If you purchase a license, you will get a valid code that you should pass through the said method. Check out the attached source code.

    Chilkat.MailMan mailman = new Chilkat.MailMan();

    mailman.UnlockComponent("Unlock 30-day trial period");

    mailman.SmtpHost = "smtp.server_goes_here.com";

    Chilkat.Email mail = new Chilkat.Email();

    mail.From = "Jeff Mike <jeff@mybusiness.com>";

    mail.AddTo("Jon Doe", "jondoe@bla.net");

    mail.Subject = "voila";

    mail.Body = "sdfsdfsdf, yes, it's all about sdfsdfsdf.";

    bool success = mailman.SendMail(mail);

    if (success)

    {

    Console.WriteLine("Done.");

    }

    else

    {

    Console.WriteLine(mailman.LastErrorText);

    }

    Let’s explain in short what the code does. We create a new MailMail object called mailman, unlock the component (trial period), add the SMTP server address, and right after that we create the mail Email object. We add the necessary fields such as From, Recipient (AddTo is used because it is added to a collection of recipients!), Subject, and Body. We also create a boolean variable called “success” just for error handling.

    Surely, the above code assumes that you have downloaded and extracted the ChilkatDotNet.dll and added it as a reference into your project solution. If you have done all of this, then the code will compile and run without issues. For more practical examples using the Chilkat component just check out their official “example section.”

    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