GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Headers

The GemBox.Email .NET component provides an advanced API for manipulating mail messages through the GemBox.Email.Mime namespace.

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

Screenshot
Headers Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Mime;
using System;
using System.Collections.Generic;
using GemBox.Email.Smtp;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new message
        MailMessage message = new MailMessage(new MailAddress("sender@example.com"),
                                              new MailAddress("receiver-1@example.com", "First"),
                                              new MailAddress("receiver-2@example.com", "Second"));

        // Get 'From' reference
        MailAddressCollection fromAddresses = message.From;

        // Get 'To' header
        Header headerTo = message.MimeEntity.Headers[HeaderId.To];

        Console.WriteLine("Original value of property 'To': " + message.To.ToString());

        // Change header value
        headerTo.Body = "new.receiver@example.com";
        Console.WriteLine("Header 'To' value changed to: " + headerTo.Body);
        Console.WriteLine("Modified value of property 'To': " + message.To.ToString());

        Header headerFrom = null;

        // Try to get 'From' header
        if (!message.MimeEntity.Headers.TryGetHeader("From", out headerFrom))
            throw new KeyNotFoundException("Header not found.");

        Console.WriteLine();
        Console.WriteLine("Original value of variable 'fromAddresses': " + fromAddresses.ToString());

        // Change header value
        headerFrom.Body = "New sender <new.sender@example.com>";
        Console.WriteLine("Header 'From' value changed to: " + headerFrom.Body);
        Console.WriteLine("Modified value of variable 'fromAddresses': " + fromAddresses.ToString());
    }
}
Imports GemBox.Email
Imports GemBox.Email.Mime
Imports GemBox.Email.Smtp

Module Samples

    Sub Main()

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

        ' Create New message
        Dim message As New MailMessage(New MailAddress("sender@example.com"),
                                       New MailAddress("receiver-1@example.com", "First"),
                                       New MailAddress("receiver-2@example.com", "Second"))

        ' Get 'From' reference
        Dim fromAddresses As MailAddressCollection = message.From

        ' Get 'To' header
        Dim headerTo As Header = message.MimeEntity.Headers(HeaderId.To)

        Console.WriteLine("Original value of property 'To': " & message.To.ToString())

        ' Change header value
        headerTo.Body = "new.receiver@example.com"
        Console.WriteLine("Header 'To' value changed to: " & headerTo.Body)
        Console.WriteLine("Modified value of property 'To': " & message.To.ToString())

        Dim headerFrom As Header = Nothing

        ' Try to get 'From' header
        If Not message.MimeEntity.Headers.TryGetHeader("From", headerFrom) Then
            Throw New KeyNotFoundException("Header not found.")
        End If

        Console.WriteLine()
        Console.WriteLine("Original value of variable 'fromAddresses': " & fromAddresses.ToString())

        ' Change header value
        headerFrom.Body = "New sender <new.sender@example.com>"
        Console.WriteLine("Header 'From' value changed to: " & headerFrom.Body)
        Console.WriteLine("Modified value of variable 'fromAddresses': " & fromAddresses.ToString())

    End Sub

End Module

Check next sample.