PopToJSON.ashx.cs :  » Content-Management-Systems-CMS » mojoPortal » SiteOffice » ExternalMail » Services » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » mojoPortal 
mojoPortal » SiteOffice » ExternalMail » Services » PopToJSON.ashx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Jayrock.Json;
using Jayrock.Json.Conversion;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
using Newtonsoft.Json;
using anmar.SharpMimeTools;
using log4net;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using SiteOffice.Business.ExternalMail;


namespace SiteOffice.ExternalMail.Services{
    /// <summary>
    /// Author:          Joe Audette
    /// Created:        2007-07-07
    /// Last Modified:      2007-11-15
    /// 
    /// The use and distribution terms for this software are covered by the 
    /// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
    /// which can be found in the file CPL.TXT at the root of this distribution.
    /// By using this software in any fashion, you are agreeing to be bound by 
    /// the terms of this license.
    ///
    /// You must not remove this notice, or any other, from this software.
    /// </summary>
    [JsonRpcHelp("This is a JSON-RPC service that serializes messages retrieved from pop mail accounts to JSON")]
    public class PopToJson : JsonRpcHandler
    {
        #region Private Properties

        private static readonly ILog log
            = LogManager.GetLogger(typeof(PopToJson));

        private Guid accountGuid = Guid.Empty;

        #endregion

        #region Public Methods

        [JsonRpcMethod("GetMessageCount", Idempotent = true)]
        [JsonRpcHelp("Returns count of messages for the provided account.")]
        public int GetMessageCount(Guid accountId)
        {
            SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
            if (currentUser == null) return -1;

            UserEmailAccount userEmailAccount
                = new UserEmailAccount(accountId);

            if (userEmailAccount.ID == Guid.Empty) return -1;
            if (userEmailAccount.UserGuid != currentUser.UserGuid) return -1;

            EmailAccountHelper emailHelper
                = new EmailAccountHelper(
                    userEmailAccount.UserName,
                    userEmailAccount.Password,
                    userEmailAccount.Pop3Server,
                    userEmailAccount.Pop3Port);


            return emailHelper.GetMessageCount();


        }

        [JsonRpcMethod("GetPageCount", Idempotent = true)]
        [JsonRpcHelp("Returns count of pages of messages for the provided account and page size.")]
        public int GetPageCount(Guid accountId, int pageSize)
        {
            SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
            if (currentUser == null) return -1;

            int totalPages = 1;

            UserEmailAccount userEmailAccount
                = new UserEmailAccount(accountId);

            if (userEmailAccount.ID == Guid.Empty) return -1;
            if (userEmailAccount.UserGuid != currentUser.UserGuid) return -1;

            EmailAccountHelper emailHelper
                = new EmailAccountHelper(
                    userEmailAccount.UserName,
                    userEmailAccount.Password,
                    userEmailAccount.Pop3Server,
                    userEmailAccount.Pop3Port);


            int messageCount 
                = emailHelper.GetMessageCount(pageSize, out totalPages);

            return totalPages;

        }

        [JsonRpcMethod("GetTop5Messages", Idempotent = true)]
        [JsonRpcHelp("Returns top 5 messages for the provided account.")]
        public string GetTop5Messages(Guid accountId)
        {
            int pageNumber = 1;
            int pageSize = 5;
            
            return GetPopMail(
                accountGuid, 
                pageNumber, 
                pageSize);


        }

        [JsonRpcMethod("GetPopMail", Idempotent = true)]
        [JsonRpcHelp("Returns page of messages for the provided account.")]
        public string GetPopMail(
            Guid accountId, 
            int pageNumber, 
            int pageSize)
        {
            int totalPages = 1;

            SiteUser currentUser = SiteUtils.GetCurrentSiteUser();
            if (currentUser == null) return "[]";

            UserEmailAccount userEmailAccount 
                = new UserEmailAccount(accountId);

            if (userEmailAccount.ID == Guid.Empty) return "[]";
            if (userEmailAccount.UserGuid != currentUser.UserGuid) return "[]";

            

            //StringBuilder jsonMessages = new StringBuilder();

            //jsonMessages.Append("{\"messages\":[");

            EmailAccountHelper emailHelper
                = new EmailAccountHelper(
                    userEmailAccount.UserName,
                    userEmailAccount.Password,
                    userEmailAccount.Pop3Server,
                    userEmailAccount.Pop3Port);
            try
            {
                List<SharpMessage> inboxMessages
                    = emailHelper.GetPage(
                    pageNumber,
                    pageSize,
                    out totalPages);

                return JavaScriptConvert.SerializeObject(inboxMessages);
                

                
            }
            catch { }

            return null;
        }


        //[JsonRpcMethod("GetCurrentUser", Idempotent = true)]
        //[JsonRpcHelp("Returns the current user")]
        //public string GetCurrentUser()
        //{
        //    return JavaScriptConvert.SerializeObject(SiteUtils.GetCurrentSiteUser());
        //}

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.