Note: This code is for evaluation purposes only.

Basic idea here is that this will connect to a DC in your domain contoso.com and dump all email addresses out of the proxyaddresses attribute for your users where they have an email address that either contains contoso1.com, contoso2.com or contoso3.com i.e. for multiple email domains if you have them and then will write it out the screen.

You technically don't want the output to screen so just do program.exe > fopesftp.txt or something to redirect output to file instead.

Should be able to paste this into Visual C# Express and compile it easily may need to add some references if they aren't there.

See Automating FOPE SFTP Uploads on how to upload that text file on a normal basis.

// DISCLAIMER:
//-------------------------------------------------------------------
//
// This sample is provided as is and is not meant for use on a
// production environment. It is provided only for illustrative
// purposes. The end user must test and modify the sample to suit
// their target environment.
//
// Microsoft can make no representation concerning the content of
// this sample. Microsoft is providing this information only as a
// convenience to you. This is to inform you that Microsoft has not
// tested the sample and therefore cannot make any representations
// regarding the quality, safety, or suitability of any code or
// information found here.
//
//===================================================================
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Collections;
 
namespace FOPESFTP
{
    class Program
    {
        static void Main(string[] args)
        {
            //Define Lists for each email domain that we are interested in collecting
            List<string> contoso1com = new List<string>();
            List<string> contoso2com = new List<string>();
            List<string> contoso3com = new List<string>();
 
            //Set the Domain Root we will be searching and the LDAP Filter
            string searchroot = "LDAP://DC=contoso,DC=com";
            string searchfilter = @"(&(objectCategory=user)(proxyaddresses=*))";
 
 
            using (DirectoryEntry root = new DirectoryEntry(searchroot))
            {
                using (DirectorySearcher searcher = new DirectorySearcher(root))
                {
                    searcher.PageSize = 1000;
                    searcher.Filter = searchfilter;
                    searcher.PropertiesToLoad.Add("proxyaddresses");
                    SearchResultCollection results = searcher.FindAll();
 
                    foreach (SearchResult result in results)
                    {
 
                        foreach (string proxyAddr in result.Properties["proxyaddresses"])
                        {
                            //Make it 'case-insensitive' for SMTP we don't care what is primary vs. not as this is just for edge blocking
                            if (proxyAddr.ToLower().StartsWith("smtp:"))
                            {
                                string proxyAddress = proxyAddr.ToLower();
                                if (proxyAddress.Contains("contoso1.com"))
                                {
                                    contoso1com.Add(proxyAddr.Substring(5));
                                }
                                else if (proxyAddress.Contains("contoso2.com"))
                                {
                                    contoso2com.Add(proxyAddr.Substring(5));
                                }
                                else if (proxyAddress.Contains("contoso3.com"))
                                {
                                    contoso3com.Add(proxyAddr.Substring(5));
                                }
                                else
                                {
                                    proxyAddress = null;
                                }
 
                            }
                            //Add to specific lists for each domain in the environment
 
 
 
                        }
 
 
                    }
 
                    Console.WriteLine("#REJECT#");
                    Console.WriteLine("#DOMAIN contoso1.com");
                    foreach (string email in contoso1com)
                    {
                        Console.WriteLine(email);
                    }
                    Console.WriteLine("#DOMAIN contoso2.com");
                    foreach (string email in contoso2com)
                    {
                        Console.WriteLine(email);
                    }
                    Console.WriteLine("#DOMAIN contoso3.com");
                    foreach (string email in contoso3com)
                    {
                        Console.WriteLine(email);
                    }
                    Console.WriteLine("#END_OF_FILE");
                }
            }
        }
    }
}