EWS Message Tracking Report

前提是你 提交于 2019-12-11 03:02:18

问题


I've been doing a bunch of research on how to get a message tracking report from exchange using EWS and can't seem to pinpoint anything. I was going to build an application that scrapes the log files but if I can do it through EWS it be better for what I'm doing. Any ideas?


回答1:


I was finally able to create a solution to my issue. I am using Powershell in C# to send commands to exchange and parse through the Message Tracking Log. In order to this you need to make sure the user you are using to connect to exchange has rights to MessageTrackingLog in exchange. The user I used has access to the RecordsManagement Role in exchange. Here is the code that allowed me to connect and get the message tracking log.

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();

    }
}
}



回答2:


I think the Office 365 Reporting web service would be a better solution than EWS, as it's got a number of mail traffic reports available that would suit your needs. There is more information here: Office 365 Reporting web service and all of the Exchange specific reports are listed here: Exchange reports available in Office 365 Reporting web service. The MailTraffic* reports all report on messages coming into and out of the organization so you don't have to code that logic yourself.



来源:https://stackoverflow.com/questions/21921174/ews-message-tracking-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!