Send Emails from your Web Site in ASP.Net

You will ultimately need to send emails from your web application. So how do you easily do this? Here’s one method that is really simple that I use consistently throughout my projects. Please keep in mind there are several ways to wrapper the ‘send email’ functionality. I choose a static class that I simply drop in each project because it gives me portability across several different code bases and projects.

The following code can be placed into a C# class file and used right away:

using System;
using System.Net.Mail;

///

/// Provides functionality for sending emails
/// 

public static class Email
{
    ///

    /// Sends an email via the supplied server
    /// 

    ///
The address you wish to send to
    ///
The address the email will come from
    ///
The subject of the email
    ///
The body content of the email
    ///
The SMTP server to send the email through (localhost or other)
    public static void SendEmail(string to, string from, string subject, string body, string smtpserver)
    {
        MailMessage mail = new MailMessage(from, to, subject, body);
        mail.IsBodyHtml = true;
        SmtpClient smtp = null;
        try
        {
            smtp = new SmtpClient(smtpserver);
            smtp.Send(mail);
        }
        catch { throw; }
        finally
        {
            mail.Dispose();
        }
    }
}

The class contains only a single procedure named appropriately enough, SendEmail. The procedure will throw any errors generated during the send mail process. I assume that we can create a new MailMessage object all day long without any errors being generated. The only sticky points are if you are unable to connect to the smtp server and actually send an email through it.

The mail.IsBodyHtml property can be refactored up to an input parameter if desired. Hope this helps someone out there! Let me know your thoughts or how this can be improved.


You might also enjoy these related posts

  1. A Simple Session State Item Manager for ASP.Net As I build applications there are times where I need to throw something into session state.  When I do so, I hate to have all the session state calls spread...
  2. Completed Email Transition for this site As a follow-up to my post on what I’ve been up to lately, I thought it would be nice to continue the chronicles of my hosting consolidation efforts. I recently...
  3. 10 ASP.NET Performance and Scalability Secrets Here is an article that is worth its weight in gold.  Really.  There are many aspects of ASP.Net that are completely unknown to the average developer, simply due to the...
  4. Web Site Monitoring – Monitor Your Web Site for Free Web site monitoring is an important part of operating a web site, and it allows you to know when your site is down.  Fortunately, it’s easy to set up and...
  5. Blogger.com Help – Lesson 14 – Email Settings Blogger.com’s email settings tab allows you, the blog owner, to do something really terrific. With the settings found on this tab, you can post to your blog via email and...

About Wayne

Wayne John is a web developer in Southern California that shares his 25+ years of programming and web development experience freely and happily to anyone willing to learn. He also loathes speaking in the third person. If you enjoyed this post, make sure you subscribe to my RSS feed or get updates in your email.
This entry was posted in Web Development Tips and tagged , . Bookmark the permalink.

One Response to Send Emails from your Web Site in ASP.Net

  1. trendbender says:

    helpful code, thx

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv Enabled

Enter YourName @ YourKeywords in the Name field to take advantage of Keyword love. Please note, I normally won't approve comments that use products or niche keywords for a name. Get your comment approved, don't be a spammer.