Multipurpose of Exception Handling Class Part 3

July 5, 2007

Hi..see you again.. this section is the continue part of my trilogy Multipurpose of Exception Handling class, okay let’s make it this quickly, the last class that we need is EmailGenerator class :

  • This class using 3 core of .Net library; System IO for accessing the XML email template,System XML and then System.Web.Email.
  • The main idea for this class is service another class that want to generate the email notification so we need to produce the public method for sending the emai, here are the method

public void AutomaticSendEmailLogging(string ErrorMessage)
        {
          
            MailMessage mailMsg = new MailMessage(); // Create instance of EmailMessage Object

            string templateName = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.ProgrammerEmailName;
            mailMsg.From = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.EmailSender;
            mailMsg.To = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.ProgrammerEmail;
            //mailMsg.Cc = EmailUser;

            string[] arrParamEmail = new string[2];

            //parametere yang akan dikirim ke email template
            arrParamEmail[0] = System.DateTime.Now.ToString();//tgl
            arrParamEmail[1] = ErrorMessage.ToString(); 
          

            this.SendEmail(mailMsg, templateName, arrParamEmail);
        }

  • I Guess i will rock’n roll of all entire code :) .. okay let see below …

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using System.Xml;
using System.Web.Mail;
using FrameworkLayer.ConfigurationUtility;
namespace FrameworkLayer.EmailUtility
{
    public class EmailGenerator
    {
        private string xml_file_name;
        //private Logger lg;

        public EmailGenerator()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  public bool IsEmail(string mail)
  {
   System.Text.RegularExpressions.Regex mailPattern = new System.Text.RegularExpressions.Regex(@”\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*”);
   return mailPattern.IsMatch(mail);
  }

        //public bool SendEmail (MailMessage mailMessage)
        //{
        //    bool retEmail=false;
        //    string enableEmailTrigger=”";

        //    enableEmailTrigger = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.IsEmailTrigger;
        //    try
        //    {
        //        if(enableEmailTrigger.ToUpper().Trim()!=”NO”)
        //        {
        //            SmtpMail.SmtpServer = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.SmtpServer;
        //            mailMessage.From= FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.EmailSender;
        //            SmtpMail.Send(mailMessage);
        //            retEmail=true;
        //        }
        //    }
        //    catch (Exception exp)
        //    { 
        //        //lg=new Logger();
        //        //lg.WriteTextLog(“Email”,exp,((string)System.Configuration.ConfigurationSettings.AppSettings.Get(“ERROR_LOG_PATH”)));

        //        //string err= exp.Message;
        //        //retEmail=false;   
        //    }
        //    return retEmail;
        //}

        private bool SendEmail(MailMessage mailMessage, string xmlfilename, string[] arrParam)
        {
            bool retEmail = false;
            string m_subject = “”;
            string m_title = “”;
            string m_body = “”;
            string smtpUsr = “”;
            string smtpPsw = “”;
            string enableEmailTrigger = “”;
            string enableEmailAuth = “”;

            enableEmailTrigger = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.IsEmailTrigger;
            enableEmailAuth = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.IsEmailEnableAuthorization;
            try
            {
                if (enableEmailTrigger.ToUpper().Trim() != “NO”)
                {
                    xml_file_name = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.EmailTemplateLocation + xmlfilename;

                    m_title = readFromXMLEmailTemplate(“TITLE”);

                    m_subject = readFromXMLEmailTemplate(“SUBJECT”);
                    //m_subject += ” – ” + m_title;

                    mailMessage.Subject = m_subject;

                    m_body = readFromXMLEmailTemplate(“BODY”);
                    m_body = replaceParameter(m_body, arrParam);
                    //m_body.Replace (“\\N”,”\r\n”);
                    mailMessage.Body = m_body;
                    mailMessage.From = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.EmailSender;

                    if (enableEmailAuth.Trim().ToUpper().Equals(“YES”))
                    {

                        smtpUsr = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.SmtpUserName;
                        smtpPsw = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.SmtpPassword;

                        mailMessage.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate“, “1″); //basic authentication
                        mailMessage.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendusername“, smtpUsr); //set your username here
                        mailMessage.Fields.Add(“http://schemas.microsoft.com/cdo/configuration/sendpassword“, smtpPsw); //set your password here
                    }
                    SmtpMail.SmtpServer.Insert(0, FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.SmtpServer);
                    //SmtpMail.SmtpServer =MPConfiguration.SMTPServer ;

                    SmtpMail.Send(mailMessage);
                    retEmail = true;
                }
            }
            catch (Exception exp)
            {
                //string err= exp.Message;
                //lg=new Logger();
                //lg.WriteTextLog(“Email Template: “+xmlfilename,exp,((string)System.Configuration.ConfigurationSettings.AppSettings.Get(“ERROR_LOG_PATH”)));

                //retEmail=false;   
            }
            return retEmail;
        }

        public void AutomaticSendEmailLogging(string ErrorMessage)
        {
          
            MailMessage mailMsg = new MailMessage(); // Create instance of EmailMessage Object

            string templateName = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.ProgrammerEmailName;
            mailMsg.From = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.EmailSender;
            mailMsg.To = FrameworkLayer.ConfigurationUtility.WebApplicationConfiguration.ProgrammerEmail;
            //mailMsg.Cc = EmailUser;

            string[] arrParamEmail = new string[2];

            //parametere yang akan dikirim ke email template
            arrParamEmail[0] = System.DateTime.Now.ToString();//tgl
            arrParamEmail[1] = ErrorMessage.ToString(); 
          

            this.SendEmail(mailMsg, templateName, arrParamEmail);
        }

  public string replaceParameter(string input,string[] arrParam)
  {
   StringBuilder tmpInput= new  StringBuilder (input);
   int idx=0;
   for(int i=0 ; i < arrParam.Length;i++)
   {
    idx = i+1;
    tmpInput.Replace (“{” + i + “}”,arrParam[i].Trim());
   }
   tmpInput.Replace (“\\N”,”\r\n”);tmpInput.Replace (“\\n”,”\r\n”);
   return tmpInput.ToString ();
  }

  public string readFromXMLEmailTemplate(string tagName)
  {
   bool bStop=false;
   string m_RetVal=”";
   XmlTextReader reader = new  XmlTextReader (xml_file_name);
   
   while(reader.Read ())
   {
    //    bStop=false;
    //    m_RetVal=”";
    switch(reader.NodeType )
    {
     case XmlNodeType.Element :
      if(reader.Name.Equals (tagName))
      {
       m_RetVal=”";
       bStop=true;
      }
      break;
     case XmlNodeType.Text:
      m_RetVal=reader.Value ; 
      break;
    }
    if (bStop && m_RetVal!=”")
     break;
   
   }
   return m_RetVal;
  }
    }
}

Entry Filed under: Design Pattern Code, Development in C# 2.0, Own design custom Framework. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


 

July 2007
M T W T F S S
« Jun   Dec »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

Recent Posts

Archives

Category Cloud

Design Pattern Code Development in C# 2.0 Development in C# 3.0 Dotnet Framework 2.0 Dotnet Framework 3.0 Own design custom Framework Pattern and Practise

Blog Stats

Top Posts

Blogroll

Recent Comments

Top Clicks

Feeds

Tags