☰  TOPICS

Running C#

Note: C# scripting is not supported in the Web app. See the compatibility table for a full comparison.

See also:
Email Parser scripting SDK
Overview of scripting in Email Parser
Example – Basic use of a script in an action

running a cs code as part of email text capturing

Email Parser provides actions for the most common tasks (SQL, Excel, Command line, Web API …) but if you need to perform something different you can use C# to create your own action.

A script action is the implementation of the Run() method as it is shown below. This method takes the following parameters:

Writing your script outside Email Parser

The code editor built into Email Parser is intentionally minimal. It is not meant for writing or debugging code. Think of it simply as a place to paste your finished script. Do your actual development in a proper code editor: Visual Studio and Visual Studio Code are the most popular choices for C# development. For quick edits or simple tests, online tools such as .NET Fiddle and SharpLab are also very handy and require no installation.

.NET dependencies (DLL files)

By default, Email Parser loads your script with the most common .NET assemblies already referenced (things like string handling, collections, and file I/O) so you can start writing code without worrying about dependencies for everyday tasks.

However, for more specialized needs you may need to add your own DLL references. For example, if you want to work with JSON data, you might need to reference System.Text.Json.dll. To add a DLL, click the DLL button on the right side of the code editor inside Email Parser. Standard .NET runtime assemblies can typically be found at C:\Program Files\dotnet\shared.

Adding a .NET assembly (DLL) reference in a C# action in Email Parser
Example

The example script below demonstrates the basic structure of a C# action. When it runs, it reads two fields that Email Parser has already extracted from the incoming email (the subject line and the sender’s address) and prints them to the output log using the Print() method. It also checks whether the email was sent by a specific address (john@doe.com) and prints a different message in that case. Finally, it creates a new field called my_new_field and assigns it a value.

//
// To implement your own action in C#, add your code to the Run() method below.
// This action uses .NET 7. If you need to reference any .NET library, use the DLL
// button in the code editor.
//
// The 'fields' parameter is a NameValueCollection object containing fields from
// previous items and the received email (Subject, Body, etc.).
// Read field values using fields["FieldName"], where 'FieldName' is the name of the
// field you want to access.
//
// You can create new fields by calling fields.Add("NewFieldName", "NewFieldValue").
// For more information, see the NameValueCollection reference:
// https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.namevaluecollection?view=net-7.0
//
// Use the Print() method to send messages to the output window and processing log.
// You can call other Email Parser actions using CallAction("action name").
//
// The 'email' parameter is a standard System.Net.Mail.MailMessage object.
// For more information, see:
// https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage?view=net-7.0
//
// For additional documentation, visit the Email Parser website:
// https://www.emailparser.com/d/actions-scripting/creating-a-custom-automated-process-with-a-script
//
// We recommend using a full IDE like Visual Studio to develop and debug your code.
// Once finished, paste your code here.

using System;
using System.Text;
using System.IO;
using System.Net.Mail;
using System.Collections.Specialized;
using EmailParserBackend.ScriptingInterface;

public class MyAction : ScriptBasedAction
{
    public override void Run(MailMessage email, NameValueCollection fields)
    {
        // Access fields from previous items through the 'fields' parameter:
        String message_subject = fields["Subject"];
        String sender_address = fields["From"];

        // Use the Print() method to send messages to the Output (and log).
        Print("An email has been received from " + sender_address + " with the subject: " + message_subject);

        if (sender_address.Equals("john@doe.com"))
            Print("The email is from John. Processing the request.");

        // You can create or modify current fields. Any changes will be available to 
        // subsequent actions that Email Parser runs.
        fields.Add("my_new_field", "1234567890");
    }
}

running a C# script as an action in the process of parsing an email

© 2008-2026 Triple Click Software Ltd.
News & Updates·Service status

Windows App privacy police and terms of use
Web App privacy police and terms of use

This site privacy police and terms of use
PAD file·Old news