Download OutlookEmailAddressExtract.zip version 1.0.0.0, last updated 26/09/2012 (27.15 KB)

Download
  • md5: 8ff6f5d3e6a03b2efeb65db5b2735ea5
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Office.Interop.Outlook;

namespace OutlookEmailAddressExtract
{
  internal class OutlookEmailAddressExtractor
  {
    public OutlookEmailAddressExtractor()
    {
      this.IncludedDomains = new List<string>();
    }

    public event EventHandler<MAPIFolderEventArgs> FolderScanning;

    public IEnumerable<string> ExtractAddresses()
    {
      Application application;

      application = new Application();
      this.EmailAddresses = new HashSet<string>();

      this.CurrentFolderIndex = 0;
      this.FolderCount = this.GetTotalFolderCount(application.Session.Folders);

      foreach (MAPIFolder folder in application.Session.Folders)
        this.ScanFolder(folder);

      return this.EmailAddresses;
    }

    public List<string> IncludedDomains { get; set; }

    public Options Options { get; set; }

    private int GetTotalFolderCount(Folders folders)
    {
      int count;

      count = 0;

      foreach (MAPIFolder folder in folders)
      {
        count++;
        count += this.GetTotalFolderCount(folder.Folders);
      }

      return count;
    }

    protected int CurrentFolderIndex { get; set; }

    protected HashSet<string> EmailAddresses { get; set; }

    protected int FolderCount { get; set; }

    protected virtual void OnFolderScanning(MAPIFolderEventArgs e)
    {
      EventHandler<MAPIFolderEventArgs> handler;

      handler = this.FolderScanning;

      if (handler != null)
        handler(this, e);
    }

    protected virtual void ProcessAddress(AddressEntry addressEntry)
    {
      if (addressEntry != null && (addressEntry.AddressEntryUserType == OlAddressEntryUserType.olSmtpAddressEntry || addressEntry.AddressEntryUserType == OlAddressEntryUserType.olOutlookContactAddressEntry))
        this.ProcessAddress(addressEntry.Address);
      else if (addressEntry != null)
        Debug.Print("Unknown address type: {0} ({1})", addressEntry.AddressEntryUserType, addressEntry.Address);
    }

    protected virtual void ProcessAddress(string emailAddress)
    {
      int domainStartPosition;

      domainStartPosition = emailAddress.IndexOf("@");

      if (!string.IsNullOrEmpty(emailAddress) && domainStartPosition != -1)
      {
        bool canAdd;

        if (this.Options.HasFlag(Options.FilterByDomain))
          canAdd = this.IncludedDomains.Contains(emailAddress.Substring(domainStartPosition + 1));
        else
          canAdd = true;

        if (canAdd)
          this.EmailAddresses.Add(emailAddress);
      }
    }

    protected virtual void ScanFolder(MAPIFolder folder)
    {
      this.CurrentFolderIndex++;
      this.OnFolderScanning(new MAPIFolderEventArgs(folder, this.FolderCount, this.CurrentFolderIndex));

      // items
      foreach (object item in folder.Items)
      {
        if (item is MailItem)
        {
          MailItem email;

          email = (MailItem)item;

          // add the sender of the email
          if (this.Options.HasFlag(Options.Sender))
            this.ProcessAddress(email.Sender);

          // add the recipies of the email
          if (this.Options.HasFlag(Options.Recipient))
          {
            foreach (Recipient recipient in email.Recipients)
              this.ProcessAddress(recipient.AddressEntry);
          }
        }
      }

      // sub folders
      if (this.Options.HasFlag(Options.SubFolders))
      {
        foreach (MAPIFolder childFolder in folder.Folders)
          this.ScanFolder(childFolder);
      }
    }
  }
}

Donate

Donate