free geoip email wrapper - Jayson's Blog - jaysonKnight.com
jaysonKnight.com
A conduit to the voices inside my head.
email wrapper
here is a class i wrote wrapping up the System.Web.Mail class...simplifies emailing from .NET applications. as it is a helper class, it reads in the email server name from the calling assembly's .config file...there is an overloaded constructor that will accept the name of an email server if you want to explicitely supply it. yes it's overkill...but well worth it in my opinion :-)

using System;

using System.Reflection;

using System.Web;

using System.Web.Mail;

 

namespace Zerotrilogy.ApplicationBlocks.Framework

{

      /// <summary>

      /// Class to build and send an email message.

      /// </summary>

      /// <remarks>

      /// Inherits from System.Web.Mail.MailMessage for simplicity

      /// </remarks>

      public class EmailConfig : MailMessage

      {

            Type myType = Type.GetType(typeof(EmailConfig).ToString());

 

            // obect var

            ClassConfig configuration = new ClassConfig();

           

            // exception message constants

            const string RECIPIENT_NOT_VALID    = "You must provide a valid recipient for the email message.  ";

            const string RECIPIENT_NULL               = "You must supply a valid format for this parameter, or parameter can't be null.  ";

            const string SENDER_NOT_VALID       = "You must provide a valid sender for the email message.  ";

            const string SENDER_NULL                  = "You must supply a valid format for this parameter, or parameter can't be null.  ";

            const string MESSAGE_NULL                 = "You must provide a built email message of type MailMessage or AppEmail.  ";

 

            string _emailServerName;

 

            /// <summary>

            /// Default format is HTML for emails

            /// </summary>

            MailFormat _emailFormat = MailFormat.Html;

 

            /// <summary>

            /// Default constructor

            /// </summary>

            /// <remarks>

            /// Sets the mail server name to the name specified in the .config file of the calling assembly

            /// </remarks>

            public EmailConfig() //: this(configuration["EmailServer", Assembly.GetEntryAssembly()], true)

            {

                  _emailServerName = configuration["EmailServer", Assembly.GetEntryAssembly()];

            }

           

            /// <summary>

            /// Overidable constructor to set the email server name

            /// </summary>

            /// <remarks>

            /// Lets the calling class specify the name of the mail server to use

            /// </remarks>

            /// <param name="emailServerName">The name of the mail server</param>

            public EmailConfig(string emailServerName) : this(emailServerName, MailFormat.Html)

            {

            }

 

            /// <summary>

            /// Overidable constructor used to set the email server name and the email format

            /// </summary>

            /// <param name="emailServerName">The name of the mail server</param>

            /// <param name="emailFormat">The format used to encode the email, either text or HTML</param>

            public EmailConfig (string emailServerName, MailFormat emailFormat)

            {

                  _emailServerName  = emailServerName;

                  _emailFormat            = emailFormat;

            }

           

            /// <summary>

            /// Method to send an email message using supplied params to build the message

            /// </summary>

            /// <param name="to">To who</param>

            /// <param name="from">From who</param>

            /// <param name="subject">Subject of the message</param>

            /// <param name="message">Body of the message</param>

            /// <example>This code shows how to call the SendEmailMessage (params string) method

            /// <code>

            /// AppEmail email = new AppEmail()

            ///   email.SendEmailMessage("to", "from", "subject", "message")

            /// </code>

            ///</example>

            public virtual void SendEmailMessage(string to, string from, string subject, string message)

            {

                  // check params

                  if (to == "") throw new ArgumentException(RECIPIENT_NOT_VALID, "to");

                  if (to == null) throw new ArgumentNullException("to", RECIPIENT_NULL);

                  if (from == "") throw new ArgumentException(SENDER_NOT_VALID, "from");

                  if (from == null) throw new ArgumentNullException("from", SENDER_NULL);

 

                  // build the email message

                  this.To = to;

                  this.From = from;

                  this.Subject = subject;

                  this.Body = message;

                  this.BodyFormat = this._emailFormat;

 

                  this.SendEmailMessage(this);

            }

 

            /// <summary>

            /// Method to send an email message using a MailMessage type

            /// </summary>

            /// <param name="message">A built email of type Web.Mail.MailMessage or AppEmail to send</param>

            /// <example>This code shows how to call the SendEmailMessage (MailMessage message) method

            /// <code>

            /// AppEmail email = new AppEmail()

            /// email.To = "to"

            /// email.From = "from"

            /// email.Subject = subject"

            /// email.Body = "body"

            /// email.SendEmailMessage(email)

            /// </code>

            /// </example>

            public virtual void SendEmailMessage(MailMessage message)

            {

                  // check params

                  if (message == null) throw new ArgumentNullException("message", MESSAGE_NULL);

 

                  this.BodyFormat = this._emailFormat;

                  SmtpMail.SmtpServer = this._emailServerName;

                  SmtpMail.Send(this);

            }

      }

}

 


Posted Sun, Nov 30 2003 10:58 PM by Jayson Knight
comments powered by Disqus

Copyright © :: JaysonKnight.com
External Content © :: Respective Authors

Terms of Service/Privacy Policy