PPTX DOCX XLSX PDF ODP
Aspose.Email  for .NET
EML

Add Watermark to EML Files in C#

Watermark EML files with Aspose.Email for .NET. Enhance your email app with an outstanding feature using the given code sample.

How to Add a Watermark using C# Email API

Watermarking EML files can be a useful feature for developers building professional email applications. Embedding visible marks, such as logos or text, into email messages, gives users the following benefits:

  • Brand emails with a company logo for consistent identity.
  • Protect content integrity by discouraging unauthorized use or tampering.
  • Ensure legal compliance with copyright notices or disclaimers.

Aspose.Email for .NET empowers developers to add watermarks to EML files programmatically. With its API features, you can incorporate text or image-based watermarks into your emails. It is a powerful API for advanced email management in .NET applications. Its features include:

  • Embedding watermarks into the email body while preserving original content.
  • Customizing watermark placement, transparency, and appearance.
  • Supporting efficient batch processing of multiple EML files.

This flexibility allows developers to integrate watermarking into their workflows for individual emails or large-scale email automation.

Steps to Add Watermark to EML via C#

Aspose.Email provides you with precise and simple steps and reference to the main components to work with MIME messages. The code below describes the process of integrating watermarks into your email content.

  1. Begin by loading an existing EML file into a MailMessage object.
  2. Add the watermark image as an attachment and assign a unique Content-ID.
  3. Modify the HTML body of the email by parsing it, embedding the watermark, and updating the content.
  4. Save the modified email file with the watermark embedded.

System Requirements

Aspose.Email for .NET is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, and Xamarin Platforms.
  • Development environment like Microsoft Visual Studio.
  • Aspose.Email for .NET referenced in your project.
 

Add Watermark to EML - C#

// Load an EML file
var mail = MailMessage.Load("sample.eml");

// Add a watermark attachment with image data
var watermarkAttachment = mail.Attachments.Add("watermark", imageBytes);

// Set Content-ID for the watermark to reference it in the HTML
watermarkAttachment.SetContentId("watermark");

// Parse the email's existing HTML body
var htmlDocument = new Aspose.Html.HTMLDocument(mail.BodyHtml, "");

// Define the new watermark HTML (ensure watermarkHtml includes a reference to the Content-ID)
var watermarkHtml = $@"watermarkHtml";

// Update the document body with the watermark HTML
htmlDocument.Body.InnerHTML = watermarkHtml;

// Save the updated HTML to a temporary file
var tempFilePath = Path.GetTempFileName();
htmlDocument.Save(tempFilePath);

// Read the updated HTML content
var updatedContent = File.ReadAllText(tempFilePath);

// Clean up the temporary file
File.Delete(tempFilePath);

// Set the modified HTML content as the email body
mail.SetBodyContent(updatedContent, BodyContentType.Html);

mail.Save("watermarked.eml");
 

Remove Watermarks from EML Files in C#

Aspose.Email for .NET also offers tools for removing watermarks from EML files, enabling developers to create adaptable and efficient email management systems. Scenarios may vary from preparing emails for reuse, updating branding, or ensuring compliance, the API provides the flexibility and reliability needed to handle evolving content requirements with ease.

 

Remove watermark from MSG file - C#

// Load the HTML body of the email
    var htmlDocument = new Aspose.Html.HTMLDocument(mail.BodyHtml, "");

    // Find and remove the watermark <img> element based on the Content-ID
    var watermarkElement = htmlDocument.QuerySelector("img[src^='cid:watermark']");
    if (watermarkElement != null)
    {
        watermarkElement.Remove();
    }

    // Optional: Clean up any surrounding containers added specifically for the watermark
    var watermarkContainer = htmlDocument.QuerySelector("div[data-watermark]");
    if (watermarkContainer != null)
    {
        // Replace the watermark container with its inner content to preserve other elements
        watermarkContainer.ReplaceWith(watermarkContainer.InnerHTML);
    }

    // Save the updated HTML back to the email
    var updatedHtml = htmlDocument.DocumentElement.InnerHTML;
    mail.SetBodyContent(updatedHtml, BodyContentType.Html);

    // Remove the watermark attachment if it exists
    var watermarkAttachment = mail.Attachments.FirstOrDefault(a => a.ContentId == "watermark");
    if (watermarkAttachment != null)
    {
        mail.Attachments.Remove(watermarkAttachment);
    }
 

With capabilities like HTML parsing for precise element identification, attachment management for removing watermark-related content, and content restoration to preserve the email structure, the Apose.Email API ensures seamless and reliable email processing.

  • About Aspose.Email for .NET API

    Aspose.Email for .NET is a standalone solution for working with popular email formats like MSG, EML, EMLX, and MHT. It allows developers to create, manipulate, and convert email messages and storage files programmatically in .NET applications, making it an essential tool for building advanced email management systems. Handling of email attachments, customization of message headers and implementation of different network protocols like POP3, IMAP & SMTP to send & receive emails is much easier.

    The library that does not require Microsoft Outlook or any other external software. Its rich functionality makes handling complex email-related tasks simple and efficient, whether for small-scale applications or enterprise-level solutions.

    With Aspose.Email for .NET, developers can focus on enhancing their applications capabilities without worrying about email format compatibility or intricate email protocol handling.

    Watermark EML via Online App

    Other Supported Watermarking Formats

    One can easily watermark other formats using C#.

    MSG (Outlook & Exchange Format)