# Using MailMerge in ASP.NET Core 6 Web Applications

> This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.

> **Note:** This article is outdated and may no longer be accurate.

- **Author:** Bjoern Meyer
- **Published:** 2022-01-27
- **Modified:** 2026-07-17
- **Description:** This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.
- **5 min read** (872 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - MailMerge
  - Tutorial
- **Web URL:** https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/
- **LLMs URL:** https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms-full.txt

---

> **New Article**
> 
> This article contains outdated information and has been replaced with a new version.
> 
> [TX Text Control Document Editor Deployment Strategies ](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms-full.txt)

The following tutorial shows how to create an ASP.NET Core .NET 6 web application that uses the MailMerge class to merge JSON data into a document template and to load this into the document viewer.

### Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0).

1. In Visual Studio 2022, create a new project by choosing *Create a new project*.
2. Select *ASP.NET Core Web App (Model-View-Controller)* as the project template and confirm with *Next*.
3. Choose a name for your project and confirm with *Next*.
4. In the next dialog, choose *.NET 6 (Long-term support)* as the *Framework* and confirm with *Create*.
    
    ![Creating the .NET 6 project](https://s1-www.textcontrol.com/assets/dist/blog/2022/01/27/a/assets/visualstudio1.webp "Creating the .NET 6 project")

#### Adding the NuGet Package

5. In the *Solution Explorer*, select your created project and choose *Manage NuGet Packages...* from the *Project* main menu.
    
    Browse for *txtextcontrol.web* and *Install* the latest version of the *TXTextControl.Web.DocumentViewer* package.
    
    ![ASP.NET Core Web Application](https://s1-www.textcontrol.com/assets/dist/blog/2022/01/27/a/assets/visualstudio4.webp "ASP.NET Core Web Application")

#### Configure the Application

6. Open the *Program.cs* file located in the project's root folder. Add the following code after the entry `app.UseStaticFiles();`:
    
    ```
    // enable local 'App_Data' folder to access local documents
    AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(System.IO.Path.GetDirectoryName(
                   System.Reflection.Assembly.GetEntryAssembly().Location), "App_Data"));
    ```

#### Adding the Document Viewer to the View

7. Find the *Index.cshtml* file in the *Views -> Home* folder. Replace the complete content with the following code:
    
    ```
    @using TXTextControl.Web.MVC.DocumentViewer
    	
    <div style="width: 800px; height: 600px;">
    	@Html.TXTextControl().DocumentViewer(settings => {
    		settings.Dock = DocumentViewerSettings.DockStyle.Fill;
    		settings.ToolbarDocked = true;
    	}).Render()
    </div>
    	
    <input type="button" value="Merge Document" onclick="mergeDocument()" />
    	
    @section Scripts {
    	<script type="text/javascript">
    	
    	    function mergeDocument() {
    	     
    	        // call the Web API "MergeDocument"
    	        $.ajax({
    	            type: "GET",
    	            url: "/Home/mergeDocument",
    	            contentType: 'application/json',
    	            success: successFunc,
    	            error: errorFunc
    	        });
    	
    	        function successFunc(data, status) {
    	            TXDocumentViewer.loadDocument(data);
    	        }
    	
    	        function errorFunc(xhr, ajaxOptions, thrownError) {
    	            alert(thrownError);
    	        }
    
    	    }
    	</script>
    }
    ```
    
    This code adds the document viewer to the view and a button that calls a Web API endpoint to merge a document server-side.

#### Adding ServerTextControl to the Project

8. While the project is selected in the *Solution Explorer*, choose *Project -> Add Project Reference...* to open the *Reference Manager*. In the opened dialog, select *Browse...* to select the required TX Text Control assemblies. Navigate to the installation folder of TX Text Control and select the following assemblies from the *Assembly* folder:
    
    
    - TXDocumentServer.dll
    - TXTextControl.dll
    - TXTextControl.Server.dll
    
    ![Create a .NET Core 6 application](https://s1-www.textcontrol.com/assets/dist/blog/2022/01/27/a/assets/vs2019_step3.webp "Create a .NET Core 6 application")
    
    Repeat this step with the following assemblies from the *Assembly/bin64* folder:
    
    
    - txic.dll
    - txkernel.dll
    - txtools.dll
    
    After selecting these assemblies, close the *Reference Manager* by confirming with *OK*.
9. While the project is selected in the *Solution Explorer*, choose *Project -> Add New Item...*. Select *Text File*, name the file *licenses.licx* and close the dialog by clicking *Add*.
    
    ![Create a .NET Core 6 application](https://s1-www.textcontrol.com/assets/dist/blog/2022/01/27/a/assets/vs2019_step4.webp "Create a .NET Core 6 application")
    
    Open the newly added file and add the following content:
    
    ```
    TXTextControl.ServerTextControl, TXTextControl.Server, Version=30.0.1500.500, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638
    ```
    
    Set the *Build Action* property to *Embedded Resource*.
10. Select the project in the *Solution Explorer* and choose *Edit Project File* from the *Project* main menu. Find the *PropertyGroup* entry and replace the whole node with the following code:
    
    ```
    <PropertyGroup>
    	<TargetFramework>net6.0</TargetFramework>
    	<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
    	<EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>true</EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>
    	<ImplicitUsings>enable</ImplicitUsings>
    	<Nullable>enable</Nullable>
    </PropertyGroup>
    ```
    
    > **BinaryFormatter Information**
    > 
    > The above setting is required to enable the obsolete .NET BinaryFormatter while compiling the license into the assembly. We are working with Microsoft to avoid this requirement in the near future.

#### Adding the Template and Data

11. In the *Solution Explorer*, select the project and click *New Folder* from the *Project* main menu and name it *App\_Data*.
12. Download the following sample template and save it to the newly created folder:
    
    [template.tx](https://s1-www.textcontrol.com/assets/dist/blog/2022/01/27/a/assets/template.tx)
13. In the same folder, create a new JSON file, name it *data.json* and insert the following content into it:
    
    ```
    [
      {
        "name": "Jackson",
        "firstname": "Peter",
        "city": "Charlotte",
        "zip": "28226",
        "street": "6920 Shannon Willow Rd",
        "product": [
          {
            "name": "TX Text Control .NET Server for ASP.NET",
            "price": "2998"
          },
          {
            "name": "TX Text Control .NET for Windows Forms",
            "price": "1459"
          }
        ]
      }
    ]
    ```

#### Adding a Web API Endpoint

14. Open the *HomeController.cs* and add the following method:
    
    ```
    [HttpGet]
    public IActionResult MergeDocument()
    {
    
        byte[] bDocument;
    
        // create a ServerTextControl
        using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { 
    
            tx.Create();
    
             // load the template
            tx.Load("App_Data/template.tx", TXTextControl.StreamType.InternalUnicodeFormat);
    
            // create the mail merge engine
            using (TXTextControl.DocumentServer.MailMerge mm = new TXTextControl.DocumentServer.MailMerge())
            {
                // connect to ServerTextControl instance
                mm.TextComponent = tx;
    
                // merge data
                var jsonData = System.IO.File.ReadAllText("App_Data/data.json");
                mm.MergeJsonData(jsonData);
            }
    
            // save in the internal format
            tx.Save(out bDocument, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
        }
    
        return Ok(bDocument);
    }
    ```

Now, compile and start the application. Click the button *Merge Document* to call the Web API and to load the merged document.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt)
- [Adding SVG Watermarks to Documents](https://www.textcontrol.com/blog/2022/01/28/adding-svg-watermarks-to-documents/llms.txt)
- [Building a Production-Ready PDF Generation Pipeline in C# with ASP.NET Core](https://www.textcontrol.com/blog/2026/10/12/reliable-pdf-generation-pipeline-csharp-aspnet-core/llms.txt)
- [Create SignFabric Envelopes from Mail Merge Templates in .NET C#](https://www.textcontrol.com/blog/2026/06/23/create-signfabric-envelopes-from-mail-merge-templates-using-dotnet-csharp/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET](https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#](https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Designing the Perfect Contract Template for MailMerge in C#](https://www.textcontrol.com/blog/2024/09/26/designing-the-perfect-contract-template-for-mailmerge-in-csharp/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Getting Started Video Tutorial: How to use the MailMerge and ServerTextControl Classes in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-mailmerge-and-servertextcontrol-classes-in-asp-net-core-c/llms.txt)
- [Getting Started Videos: New Text Control YouTube Channel](https://www.textcontrol.com/blog/2024/08/02/getting-started-videos-new-text-control-youtube-channel/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [Designing a MailMerge Web API Endpoint with ASP.NET Core in C#](https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms.txt)
- [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/llms.txt)
- [Document Automation 101: Leveraging TX Text Control for Business Efficiency in .NET C# Applications](https://www.textcontrol.com/blog/2024/07/09/document-automation-101-leveraging-tx-text-control-for-business-efficiency-in-net-c-applications/llms.txt)
- [Merging Templates with MailMerge with Different Merge Field Settings in C#](https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt)
- [MailMerge: Working with Image Placeholders](https://www.textcontrol.com/blog/2022/12/22/mailmerge-working-with-image-placeholders/llms.txt)
- [Getting Started: ServerTextControl and MailMerge with ASP.NET Core](https://www.textcontrol.com/blog/2022/09/01/getting-started-servertextcontrol-and-mailmerge-with-aspnet-core/llms.txt)
- [code.talks 2026: Two Fantastic Days of Developers, Innovation, and Great Conversations in Hamburg](https://www.textcontrol.com/blog/2026/11/06/code-talks-2026-hamburg/llms.txt)
- [Meet Text Control at code.talks 2026: Spreadsheets, Documents, and More](https://www.textcontrol.com/blog/2026/10/27/meet-text-control-at-code-talks-2026/llms.txt)
