Parse email signatures with C#

Here is how to parse email signatures with C#.

We’ll cover the following:

  • Why parsing email signatures is hard
  • Nuget Package
  • Code Example for Sending An Email to SigParser
  • Libraries for pulling emails with .NET Framework and Core

Why parsing email signatures is hard

Parsing email signatures is exceptionally difficult with just a couple regex expressions. Here are some of the major things you’d need to handle.

  • No signature is formatted the same
  • Phone numbers can have many different formats
  • Need to attribute the type of phone number (Fax vs Mobile vs Work phones).
  • The phone type indicator has lots of variations. For example, Mobile vs M: vs Cell vs C: and many others
  • Titles can be incredibly difficult to capture without getting too much wrong information.
  • Locations are tough. Very few people put full addresses. Often they’ll only put the city and state but no country. Even street addresses are massively different by country.

Then there is identifying where the email signature is in the email which is really hard. We use a machine learning algorithm with tons of label validation emails. We couldn’t come up with a more reliable way than that. We’ve been labeling our test set for years.

Splitting Email Chains

If you want to split emails on the headers, that is also tough. No two email clients seem to produce the same header format and over time the email clients change the way they format headers.

This is why we expose a REST API

So this is why we suggest using our email parsing service to parse emails. As we improve you’ll automatically get fixes.

Nuget

Although you can absolutely call our REST API from .NET, we’ve exposed a helper Nuget package which has the data model setup and allow syou to easy pass it the API key. This is comptabile with .NET Core and .NET Framework.

Install-Package SigParser

https://www.nuget.org/packages/SigParser/

Example Code To Send an Email

var client = new SigParser.Client(ApiKey);
            var result = client.Parse(new SigParser.EmailParseRequest
            {
                plainbody = @"
Hi John,

Lets get coffee tomorrow.

Thanks
Steve Johnson
888-333-3323 Mobile
San Diego, CA
",
                from_name = "Steve Johnson",
                from_address = "sjohnson@example.com"
            }).Result;

Email Libraries

Pulling emails can be hard but is totally doable in .NET and .NET Core. Here are the libraries we suggest.

Gmail

Nuget Package

Install-Package Google.Apis.Gmail.v1

We made a handy article on Medium for how to do it exactly:

Blog: Pull Gmail messages with C#

Office 365

This is kind of a difficult API to get your head wrapped around at first.

Install-Package Microsoft.Graph

Tips

  • The Graph API Nuget package just helps you compose ODATA formatted requests.
  • You should familiarize yourself with the Microsoft docs for Graph API Email Messages
  • If you need to sync emails every X hours or even on an initial pull, be sure to use the “delta” endpoint.
    • You can only sort the sync by “receivedDateTime desc” otherwise the results will be random.
  • If you try to sync by just querying messages newer than some date using the “List messages” endpoint, you could encounter the following errors:
    • Timeouts if the mailbox is too large
    • Missing messages

Exchange

It is suggested to use Office 365 if at all possible but if not then…

.NET Framework then use

Install-Package Microsoft.Exchange.WebServices

This is the version officially supported by Microsoft but it doesn’t work on .NET Core.

.NET Core Compatible (will also work with .NET Framework)

Install-Package Microsoft.Exchange.WebServices.NETStandard

The source code is here but it isn’t officially supported by Microsoft. For SigParser for security reasons we have our own build of this since we aren’t sure who the maintainer is in real life.

This version also lacks some of the DNS discovery features that the .NET Framework version has.

IMAP

This is an IMAP Email .NET assembly from LimiLabs. You can try it for free but you have to pay for it. As of this writing it is $149 for a single developer and a year of support. This is probably one of the best IMAP products we’ve used for handling IMAP emails. It also works with .NET Framework and .NET Core.

Install-Package Mail.dll