Headers

GemBox.Email provides an advanced API for manipulating with mail messages through the GemBox.Email.Mime namespace.

The message headers contain important information related to the content of the message, and store various email metadata, like priority, spam score, etc. They are represented with Header objects.

The MailMessage object contains properties for required and commonly used message headers, like MailMessage.Subject, MailMessage.Sender, etc. Other headers can be added to the HeaderCollection collection.

The following example shows how to modify header values manually or through provided properties. It also demonstrates the link between headers and properties, and how they are updated automatically.

Screenshot of updated email headers and properties
Email headers in C# and VB.NET
using System;
using GemBox.Email;
using GemBox.Email.Mime;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new message.
        MailMessage message = new MailMessage("sender@example.com", "receiver@example.com");

        // Get 'From' property and 'From' header.
        MailAddressCollection fromAddresses = message.From;
        Header fromHeader = message.MimeEntity.Headers[HeaderId.From];

        Console.WriteLine($"Original 'From' property value: {fromAddresses}");

        // Change 'From' header value.
        fromHeader.Body = "new.sender@example.com";

        Console.WriteLine($"Modified 'From' property value: {fromAddresses}");

        // Get 'To' property and 'To' header.
        MailAddressCollection toAddresses = message.To;
        Header toHeader = message.MimeEntity.Headers[HeaderId.To];

        Console.WriteLine($"Original 'To' header value: {toHeader.Body}");

        // Change 'To' property value.
        toAddresses[0].Address = "new.receiver@example.com";

        Console.WriteLine($"Modified 'To' header value: {toHeader.Body}");
    }
}
Imports System
Imports GemBox.Email
Imports GemBox.Email.Mime

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create new message.
        Dim message As New MailMessage("sender@example.com", "receiver@example.com")

        ' Get 'From' property and 'From' header.
        Dim fromAddresses As MailAddressCollection = message.From
        Dim fromHeader As Header = message.MimeEntity.Headers(HeaderId.From)

        Console.WriteLine($"Original 'From' property value: {fromAddresses}")

        ' Change 'From' header value.
        fromHeader.Body = "new.sender@example.com"

        Console.WriteLine($"Modified 'From' property value: {fromAddresses}")

        ' Get 'To' property and 'To' header.
        Dim toAddresses As MailAddressCollection = message.To
        Dim toHeader As Header = message.MimeEntity.Headers(HeaderId.To)

        Console.WriteLine($"Original 'To' header value: {toHeader.Body}")

        ' Change 'To' property value.
        toAddresses(0).Address = "new.receiver@example.com"

        Console.WriteLine($"Modified 'To' header value: {toHeader.Body}")
    End Sub
End Module

The headers are defined by the RFC 5322 standard. They require two parts, a name and a body, and the first part (name) cannot be empty.

GemBox.Email supports adding both standard and custom headers. Standard headers can be added using HeaderId identifiers.

The following example shows how to add additional headers to an email.

using GemBox.Email;
using GemBox.Email.Mime;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new message.
        MailMessage message = new MailMessage("sender@example.com", "receiver@example.com")
        {
            Subject = "Example Message",
            BodyText = "High priority email with comments."
        };

        // Add message comments.
        message.MimeEntity.Headers.Add(
            new Header(HeaderId.Comments, "Example of mail message comment"));

        // Add message priority.
        message.MimeEntity.Headers.Add(
            new Header("Importance", "high"));
        message.MimeEntity.Headers.Add(
            new Header("Priority", "urgent"));
        message.MimeEntity.Headers.Add(
            new Header("X-Priority", "1"));

        message.Save("High Priority Message.%OutputFileType%");
    }
}
Imports GemBox.Email
Imports GemBox.Email.Mime

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create new message.
        Dim message As New MailMessage("sender@example.com", "receiver@example.com") With
        {
            .Subject = "Example Message",
            .BodyText = "High priority email with comments."
        }

        ' Add message comments.
        message.MimeEntity.Headers.Add(
            New Header(HeaderId.Comments, "Example of mail message comment"))

        ' Add message priority.
        message.MimeEntity.Headers.Add(
            New Header("Importance", "high"))
        message.MimeEntity.Headers.Add(
            New Header("Priority", "urgent"))
        message.MimeEntity.Headers.Add(
            New Header("X-Priority", "1"))

        message.Save("High Priority Message.%OutputFileType%")
    End Sub
End Module

Check next example or download examples from GitHub.