Archive for Luglio 1st, 2008

dynamic buttons and event caching(like button_Click)

CodeKeep C# Feed Luglio 1st, 2008

Description: dynamic buttons and event caching(like button_Click)

Link: http://www.codekeep.net/snippets/ccd59e46-1c6d-4c25-bc8c-db02a6c76bde.aspx

 Adding button dynamically to panel
------------------------------------
Button btn = new Button();
 btn.Name = "Dynamic Button";
 btn.Text = btn.Name;
 btn.BackColor = Color.Silver;
 btn.Location =new Point(3,3);
 btn.Click+=new EventHandler(btn_Click);
 pnlItems.Controls.Add(btn);

dynamic button Click event
----------------------------------

 private void btn_Click(object sender,EventArgs e)
        {
            Button btn = (Button) sender; //Instance for dynamic button
            MessageBox.Show(btn.Name);

        }

Class to send an Email

CodeKeep C# Feed Luglio 1st, 2008

Description: Class to Send An Email in Asp.net C#

Link: http://www.codekeep.net/snippets/69bc0c94-9178-4a92-b48b-521ccea0356a.aspx

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

 



    public class CustEMail
    {
        string m_MailTo = "";
        string m_MailCC = "test@test.com";
        string m_MailBCC = "";
        string m_MailFrom = ConfigurationManager.AppSettings.Get("MailFrom").ToString();
        string m_MailSubject = "";
        string m_ReadReceiptTo = "";
        string m_ReplyTo = "";
        string m_MailBody = "";


        public CustEMail()
        {            
            //
            // TODO: Add constructor logic here
            //
        
        }
        
        public string MailTo
        {
            get { return m_MailTo; }
            set { m_MailTo = value; }
        }

        public string MailCC
        {
            get { return m_MailCC; }
            set { m_MailCC = value; }
        }

        public string MailBCC
        {
            get { return m_MailBCC; }
            set { m_MailBCC = value; }
        }
        
        public string MailFrom
        {
            get { return m_MailFrom; }
            set { m_MailFrom = value; }
        }

        public string MailSubject
        {
            get { return m_MailSubject; }
            set { m_MailSubject = value; }
        }

        public string ReadReceiptTo
        {
            get { return m_ReadReceiptTo; }
            set { m_ReadReceiptTo = value; }
        }

        public string ReplyTo
        {
            get { return m_ReplyTo; }
            set { m_ReplyTo = value; }
        }

        public string MailBody
        {
            get { return m_MailBody; }
            set { m_MailBody = value; }
        }

        public bool Send(string Body)
        {
   

            MailMessage mail = new MailMessage();

            //set the addresses
            mail.From = new MailAddress(m_MailFrom ,"Your Company Name");


            int extIndx;           
            string Mto = "";           
            do
            {
                extIndx = MailTo.IndexOf(";");
                Mto = MailTo.Substring(0 , extIndx);
                mail.To.Add(Mto);
                Mto = Mto + ";";
                MailTo = MailTo.Replace(Mto , "");
            }
            while (MailTo.Contains(";"));

            if (MailTo != ""  && MailTo != ";")
            {
                mail.To.Add(MailTo);
            }

            //set the content
            mail.Subject = MailSubject;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            mail.Headers.Add("X-Company", "Your Company Name");
            mail.Headers.Add("X-Location", "test@test.com");

            // Add the Read Reciept If needed
            if (ReadReceiptTo != "")
            {
                ReadReceiptTo = "<" + ReadReceiptTo + ">";
                mail.Headers.Add("Disposition-Notification-To", ReadReceiptTo);          
            }

            // Add the Reply to the mail
            if (ReplyTo != "")
            {
                mail.ReplyTo = new MailAddress(ReplyTo);
            }
                 
        
            // Create smtp client objec to send an email
            SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("MailServer").ToString() );
                       
            
            try
            {
                smtp.Send(mail);
                //SmtpMail.Send(msg);
                return true;
            }
            catch (Exception ex)
            {                
              return false;
            }

         
        }

    }