Created
January 4, 2013 11:36
-
-
Save ChrisMcKee/4451957 to your computer and use it in GitHub Desktop.
Template Messaging (Email)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace Core | |
| { | |
| using System; | |
| using System.Collections; | |
| using System.Net.Mail; | |
| using Helper; | |
| public class TemplateMessage | |
| { | |
| protected Hashtable Attachments; | |
| protected string _bcc; | |
| protected string _cc; | |
| protected string _from; | |
| protected bool _isBodyHtml; | |
| protected Hashtable _placeHolders; | |
| protected string _recipient; | |
| protected string _subject; | |
| protected string _template; | |
| protected string _templateRoot; | |
| /// <summary> | |
| /// Fully qualified path to root of template folder | |
| /// </summary> | |
| public virtual string TemplateRoot | |
| { | |
| get { return _templateRoot; } | |
| set { _templateRoot = value; } | |
| } | |
| /// <summary> | |
| /// Name of template to use | |
| /// </summary> | |
| public virtual string Template | |
| { | |
| get { return _template; } | |
| set { _template = value; } | |
| } | |
| /// <summary> | |
| /// Message placeholders | |
| /// </summary> | |
| public virtual Hashtable PlaceHolders | |
| { | |
| get { return _placeHolders; } | |
| set { _placeHolders = value; } | |
| } | |
| /// <summary> | |
| /// Recipient name (can be comma delimeted) | |
| /// </summary> | |
| public virtual string Recipient | |
| { | |
| get { return _recipient; } | |
| set { _recipient = value; } | |
| } | |
| /// <summary> | |
| /// Bcc list (can be comma delimetered) | |
| /// </summary> | |
| public virtual string Bcc | |
| { | |
| get { return _bcc; } | |
| set { _bcc = value; } | |
| } | |
| /// <summary> | |
| /// Cc list (can be comma delimetered) | |
| /// </summary> | |
| public virtual string Cc | |
| { | |
| get { return _cc; } | |
| set { _cc = value; } | |
| } | |
| /// <summary> | |
| /// From address | |
| /// </summary> | |
| public virtual string From | |
| { | |
| get { return _from; } | |
| set { _from = value; } | |
| } | |
| /// <summary> | |
| /// Subject of mail | |
| /// </summary> | |
| public virtual string Subject | |
| { | |
| get { return _subject; } | |
| set { _subject = value; } | |
| } | |
| /// <summary> | |
| /// Format of template | |
| /// </summary> | |
| public virtual bool IsBodyHtml | |
| { | |
| get { return _isBodyHtml; } | |
| set { _isBodyHtml = value; } | |
| } | |
| public TemplateMessage() | |
| { | |
| _templateRoot = string.Empty; | |
| _template = string.Empty; | |
| _placeHolders = new Hashtable(); | |
| Attachments = new Hashtable(); | |
| _recipient = string.Empty; | |
| _from = string.Empty; | |
| _subject = string.Empty; | |
| _isBodyHtml = false; | |
| } | |
| /// <summary> | |
| /// Constructor to allow template root setting. | |
| /// </summary> | |
| /// <param name="templateRoot">Fully qualified path to root of template folder</param> | |
| public TemplateMessage(string templateRoot) : this() | |
| { | |
| _templateRoot = templateRoot; | |
| } | |
| public virtual void AddPlaceHolder(string key, object value) | |
| { | |
| _placeHolders.Add(key, value); | |
| } | |
| /// <summary> | |
| /// Send the message by combining the template root and template to create | |
| /// the full path to the email message file. Process all placeholders and | |
| /// send the message. | |
| /// </summary> | |
| public virtual bool Send() | |
| { | |
| string templatePath = String.Format("{0}{1}", _templateRoot, _template); | |
| _placeHolders.Add("CurrentDateTime", DateTime.Now); | |
| string templateText = EmailHelper.CreateBodyFromTemplate(templatePath, _placeHolders); | |
| var message = new MailMessage(_from, _recipient) | |
| { | |
| Body = templateText, | |
| Subject = _subject, | |
| IsBodyHtml = IsBodyHtml | |
| }; | |
| if (!string.IsNullOrWhiteSpace(_bcc)) | |
| message.Bcc.Add(_bcc); | |
| if (!string.IsNullOrWhiteSpace(_cc)) | |
| message.CC.Add(_cc); | |
| if (Attachments.Count > 0) | |
| { | |
| for (int count = 1; count <= Attachments.Count; count++) | |
| { | |
| message.Attachments.Add(new Attachment(Attachments[count].ToString())); | |
| } | |
| } | |
| EmailHelper.Send(message); | |
| return true; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment