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

Add Watermark to MSG Files in C#

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

Watermark MSG Files using C# Email API

Adding watermarks to MSG files is a practical feature for developers building email management applications. By embedding visible marks, such as images or text, you can achieve the following:

  • Maintain brand consistency by incorporating a company logo.
  • Protect email content integrity and discourage unauthorized use.
  • Ensure legal compliance through visible disclaimers or copyright notices.

Aspose.Email for .NET offers developers the ability to watermark MSG files programmatically. This feature-rich API simplifies the integration of image or text-based watermarks into email content. Key features include:

  • Embedding watermarks directly into the email body without altering the original content.
  • Supporting customizable watermark options like placement, opacity, and size.
  • Enabling batch processing for handling multiple MSG files efficiently.

This functionality is ideal for automating watermark integration across individual emails or large-scale email workflows in .NET applications.

Steps to Add Watermark to MSG 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 MSG 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 MSG - C#

// Load an MSG file
var mail = MailMessage.Load("sample.msg");

// 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.msg");
 

Remove Watermarks from MSG Files in C#

In scenarios where email content needs to be restored to its original state, the functionality to remove previously embedded watermarks from email files can be handy. Aspose.Email for .NET simplifies this task with its efficient API.

 

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);
    }
 

Thus, Aspose.Email allows you to manipulate the HTML body of email messages to identify and remove watermark elements, locate and delete attachments, such as images, linked to watermarks via Content-ID, and, finally, remove surrounding HTML containers or structures added during the watermarking process while preserving the integrity of other email content.

These features empower developers to programmatically clean up email content while maintaining complete control over the structure and elements of the email.

  • About Aspose.Email for .NET

    Aspose.Email for .NET is a library designed to handle complex email operations in .NET applications. It enables developers to programmatically manage email formats like MSG, EML, EMLX, and MHT while providing advanced features for parsing, editing, and saving message content. The API supports direct interaction with network protocols such as SMTP, IMAP, POP3, and Exchange, allowing seamless email transmission and retrieval. Its attachment-handling capabilities extend to adding, modifying, and extracting files within messages, along with managing MIME content. Additionally, Aspose.Email integrates HTML parsing for email body customization and supports various encoding types to ensure compatibility. By offering these tools in a standalone package, the library eliminates reliance on external software, empowering developers to build scalable, high-performance email solutions tailored to specific requirements.

    Watermark MSG via Online App

    Other Supported Watermarking Formats

    One can easily watermark other formats using C#.

    EML (Outlook Email Messages)