解惑

解己之惑,解人之惑

标签:size

从CDO得到Mailbox的大小

前几天的方案都比较复杂一些,再来一个简单的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAPI;

namespace ConsoleApplication3
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            Session session = new Session();
            session.Logon(null, null, false, true, 0, true, "exchange2007\nuser1");
            InfoStores infoStores = (InfoStores)session.InfoStores;
            for (int i = 0; i < (int)infoStores.Count; i++)
            {
                InfoStore infoStore = (InfoStore)infoStores.get_Item(i + 1);
                if (((string)infoStore.Name).IndexOf("Mailbox – ") >-1)
                {
                    Fields fields = (Fields)infoStore.Fields;
                    Field size = (Field)fields.get_Item(CdoPropTags.CdoPR_MESSAGE_SIZE, System.Reflection.Missing.Value);
                    Field count = (Field)fields.get_Item(CdoPropTags.CdoPR_CONTENT_COUNT, System.Reflection.Missing.Value);
                    Console.WriteLine(infoStore.Name + ": " + size.Value + "Bytes, messages:" + count.Value);
                }
            }
            session.Logoff();
            Console.ReadLine();
        }
    }
}

这个方案的问题是需要安装CDO,好在这个东东是微软的,而且很小,安装包才几百K,其实就是把MAPI的一些API包装成COM了,所以.net平台的那些语言都可以调。

从客户端得到Mailbox的Size

昨天已经搞定了从服务器端得到Mailbox的size,但是这样不是很方便,需要在Exchange服务器上发布代码,今天继续研究从客户端得到这个Size,历经磨难终于成功:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.exchange2007;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace ConsoleApplication1
{
    class EWS
    {
        public static void Run()
        {
            ServicePointManager.ServerCertificateValidationCallback =
            delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };
            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.RequestServerVersionValue = new RequestServerVersion();
            esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007;
            esb.Credentials = new NetworkCredential("Administrator", "password", "domain.com");
            esb.Url = "https://exchange2007/EWS/Exchange.asmx";

            Console.WriteLine("Administrator:" + GetMailboxSize(esb, "Administrator@domain.com"));
            Console.WriteLine("cherami:" + GetMailboxSize(esb, "cherami@domain.com"));
            Console.WriteLine("ES1Test1:" + GetMailboxSize(esb, "ES1Test1@domain.com"));

            Console.ReadLine();
        }

阅读全文

从Exchange2007得到Mailbox的大小

开始的时候觉得这个是一个很小的问题,肯定有API直接可以拿到的,不过看了一圈下来,给出的最多的建议是从MAPI得到Mailbox,然后遍历目录累加大小得到Mailbox的大小。可以参考微软的KB:
http://support.microsoft.com/kb/320071
http://support.microsoft.com/kb/200160/

不过感觉都很麻烦。Exchange 2007提供了Exchange Management Shell ,里面有个脚本命令Get-MailboxStatistics可以得到Mailbox的信息,其中就包括Mailbox的大小,然后可以用c#调用这个脚本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
            PSSnapInException snapInException = null;
            PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
            Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
            myRunSpace.Open();
            Pipeline pipeLine = myRunSpace.CreatePipeline();
            Command myCommand = new Command("Get-MailboxStatistics");
            pipeLine.Commands.Add(myCommand);
            CommandParameter databaseParam = new CommandParameter("database", "Main Database");
            myCommand.Parameters.Add(databaseParam);
            Collection<PSObject> commandResults = pipeLine.Invoke();
            foreach (PSObject cmdlet in commandResults)
            {
                Console.WriteLine(cmdlet.Properties["DisplayName"].Value + "\t" + cmdlet.Properties["TotalItemSize"].Value);
            }
        }
    }
}

唯一的问题是这个需要在exchange server上执行,而且也不排除这个脚本也是通过遍历目录得到的大小(执行的速度似乎也不快)。

© 2024 解惑

本主题由Anders Noren提供向上 ↑