Click here to Skip to main content
15,896,348 members
Articles / Web Development / ASP.NET

YaMessaging - A simple e-mail like messaging application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (20 votes)
19 Apr 2012CPOL4 min read 58.4K   2.9K   41  
A simple e-mail like application using ASP.NET and C#.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 YAMessaging.BLL;

public partial class SendMessage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        if (IsPostBack == false)
        {
            UserHandler userHandler = new UserHandler();

            chkLstUsers.DataSource = userHandler.GetUserList();
            chkLstUsers.DataTextField = "userid";
            chkLstUsers.DataValueField = "userid";
            chkLstUsers.DataBind();

            if (Request.QueryString["action"] != null &&
                Session["Message"] != null)                
            {
                Message msg = (Message)Session["Message"];

                switch (Request.QueryString["action"].ToString())
                {
                    case "reply":
                        txtSubject.Text = "Re: " + msg.Subject;
                        txtToList.Text = msg.SenderId + ",";
                        FreeTextBox1.Text = Server.HtmlDecode
                            ("<br/><br/><br/> Original Message<hr/> From: " + 
                            msg.SenderId +
                            "<br/> To: " +
                            msg.RecieverId +
                            "<br/> Message contents: " +
                            msg.Body);
                        break;

                    case "forward":
                        txtSubject.Text = "Fw: " + msg.Subject;
                        FreeTextBox1.Text = Server.HtmlDecode("<br/><br/><br/> Original Message<hr/> From: " +
                            msg.SenderId +
                            "<br/> To: " +
                            msg.RecieverId +
                            "<br/> Message contents: " +
                            msg.Body);
                        break;
                }
            }

        }
    }
    protected void btnAddSelected_Click(object sender, EventArgs e)
    {
        //There could be some users already added to the list so check and put a ',' if needed.
        if (txtToList.Text.Trim() != string.Empty && txtToList.Text.Trim()[txtToList.Text.Trim().Length -1] != ',')
        {
            txtToList.Text += ",";
        }
        foreach (ListItem item in chkLstUsers.Items)
        {
            if (item.Selected == true)
            {
                string recieversList = txtToList.Text.Replace(" ", "");
                if (recieversList.Contains(item.Text + ",") == false)
                {
                    txtToList.Text += item.Text + ",";
                }
            }
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtToList.Text.Trim() == string.Empty)
        {
            //Set the notification label here
            return;
        }
        
        // Let us get the list of valid users first
        UserHandler userHandler = new UserHandler();
        DataTable table = userHandler.GetUserList();

        //Now get the recievers list entered by user        
        string recieversList = txtToList.Text.Replace(" ", "");
        string[] users = recieversList.Split(new char[] { ','});
        string[] failList = new string[users.Length];
        string[] successList = new string[users.Length];

        int successCount = 0;
        int failCount = 0;

        MessageHandler handler = new MessageHandler();
        foreach (string user in users)
        {
            if (userHandler.IsValidUser(user) == true)
            {
                if (true == handler.SendMessage(user, Session["UserId"].ToString().Trim(), txtSubject.Text, Server.HtmlEncode(FreeTextBox1.Text)))
                {
                    successList[successCount++] = user;
                }
                else
                {
                    failList[failCount++] = user;
                }
            }
            else
            {
                failList[failCount++] = user;
            }
        }        

        Session["SuccessList"] = successList;
        Session["FailList"] = failList;

        Response.Redirect("Confirmation.aspx");        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India

I Started my Programming career with C++. Later got a chance to develop Windows Form applications using C#. Currently using C#, ASP.NET & ASP.NET MVC to create Information Systems, e-commerce/e-governance Portals and Data driven websites.

My interests involves Programming, Website development and Learning/Teaching subjects related to Computer Science/Information Systems. IMO, C# is the best programming language and I love working with C# and other Microsoft Technologies.

  • Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
  • Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

If you like my articles, please visit my website for more: www.rahulrajatsingh.com[^]

  • Microsoft MVP 2015

Comments and Discussions