c# - EWS 消息跟踪报告

标签 c# exchangewebservices exchange-server-2010

我一直在研究如何使用 EWS 从 Exchange 获取消息跟踪报告,但似乎无法查明任何内容。我打算构建一个抓取日志文件的应用程序,但如果我可以通过 EWS 来完成它,那对我正在做的事情会更好。有任何想法吗?

最佳答案

我终于能够为我的问题创建一个解决方案。我在 C# 中使用 Powershell 发送命令以通过消息跟踪日志进行交换和解析。为此,您需要确保用于连接 Exchange 的用户有权访问 Exchange 中的 MessageTrackingLog。我使用的用户可以访问交换中的 RecordsManagement 角色。这是允许我连接并获取消息跟踪日志的代码。

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Management.Automation;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Management.Automation.Remoting;


namespace ExchangeConnection
{
class ExchangeShell
{

    //Credentials
    const string userName = "username";
    const string password = "password";

    private PowerShell InitializePS()
    {

        PSCredential credential = new PSCredential(userName, SecurePassword());
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("exchange server url/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
        connectionInfo.MaximumConnectionRedirectionCount = 5;
        connectionInfo.SkipCNCheck = true;
        connectionInfo.OpenTimeout = 999999;

        Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
        runspace.Open();
        PowerShell powershell = PowerShell.Create();
        powershell.Runspace = runspace;
        return powershell;

    }

    private SecureString SecurePassword()
    {
        System.Security.SecureString securePassword = new System.Security.SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }
        return securePassword;
    }

    public void GetMessageTrackingLog(string sender)
    {
        PowerShell ps = InitializePS();

        ps.AddCommand("Get-MessageTrackingLog");
        ps.AddParameter("Start", DateTime.Now.AddHours(-24).ToString());
        ps.AddParameter("ResultSize", "Unlimited");
        ps.AddParameter("Sender", sender);
        ps.AddParameter("EventId", "SEND");
        Collection<PSObject> results = ps.Invoke();
        Console.WriteLine("|----Sender----|----Recipients----|----DateTime----|----Subject----|");
        foreach (var r in results)
        {
            string senders = r.Properties["Sender"].Value.ToString();
            string recipients = r.Properties["Recipients"].Value.ToString();
            string timestamp = r.Properties["Timestamp"].Value.ToString();
            string subject = r.Properties["MessageSubject"].Value.ToString();
            string eventID = r.Properties["EventID"].Value.ToString();
            string messageInfo = r.Properties["MessageInfo"].Value.ToString();
            Console.WriteLine("{0}|{1}|{2}|{3}", sender, recipients, timestamp, subject);
        }
        ps.Dispose();
        ps.Runspace.Dispose();

    }
}
}

关于c# - EWS 消息跟踪报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21921174/

相关文章:

exchange-server - Exchange Web 服务,无法设置约会的 startTimeZone (java api)

java - 如何使用 Java EWS API 搜索 iCalUid 的 Exchange 约会?

ssl - Javamail、Weblogic、IMAP、SSL 和 Exchange 2010

java - 如何使用 Microsoft Exchange 发送电子邮件附件?

exchangewebservices - item.HasAttachments 为 true,但集合中没有附件

c# - EWS - 访问共享日历项目/约会

c# - 如何使用表达式树对数字类型应用隐式转换?

c# - 应用程序中的服务器错误 '/demo'

c# - 从数组中获取除第一个元素之外的所有元素

c# - 创建一个从阻塞线程订阅事件的表单