This is part 14 of a series on sending Email

In our last post, “Sending Email In C# & .NET - Part 13 - Sending Email With Inline Attachments Using MailKit”, we looked at how to send inline attachments using MailKit.

In this post, we will look at how to send an email with multiple formats.

The process is as follows:

  1. Create a MimeMessage
  2. Create one (or more) MailboxAddress for the recipients and add to the To collection of the MimeMessage
  3. Create one MailboxAddress for the sender and add it to the From collection of the MimeMessage
  4. Set the Subject of the MimeMessage
  5. Create a BodyBuilder
  6. Add LinkedResources to the BodyBuilder (if any)
  7. Set the TextBody of the the BodyBuilder
  8. Set the HtmlBody of the BodyBuilder
  9. Set the body from the BodyBuilder
  10. Send the message using the SmtpClient. This is the SmtpClient from MailKit, not the one in System.Net.

The code is as follows:

using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Utils;
using Serilog;

// Configure logging to the console
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();

// Create the email
var message = new MimeMessage();
// Add the sender
message.From.Add(new MailboxAddress("James Bond", "james@mi5.org"));
// Set the recipient
message.To.Add(new MailboxAddress("M", "m@mi5.org"));
// Set the email subject
message.Subject = "Christmas Card";

var builder = new BodyBuilder();

// Create a LinkedResource with the image
var image1 = builder.LinkedResources.Add("Bond1.jpeg");
// Generate an ID for use in linkage
image1.ContentId = MimeUtils.GenerateMessageId();

// Add the card attachment
builder.Attachments.Add("Card.txt");

// Build the html version of the message text using the IDs
var htmlBody = $"""
                <p>Dear M,<br/>
                <p>Merry Christmas From Me<br/>
                <br/
                <center>
                <img src="cid:{image1.ContentId}">
                </center>
                <p>James<br>
                """;

// Set the html body
builder.HtmlBody = htmlBody;

// Build the html version of the message text using the IDs
var body = """
           Dear M,

           Merry Christmas From Me

           James
           """;

// Set the plain text body
builder.TextBody = body;

// Set the message body 
message.Body = builder.ToMessageBody();

// Now send the email
using (var client = new SmtpClient())
{
    Log.Information("Connecting to smtp server...");
    await client.ConnectAsync("localhost", 25, false);
    // Typically, authenticate here. But we are using PaperCut 
    //await client.AuthenticateAsync("username", "password");
    await client.SendAsync(message);
    Log.Information("Sent message");
    await client.DisconnectAsync(true);
    Log.Information("Disconnected from server");
}

If we run this code, the resulting email will be as follows:

MailKitMultiEmail

Of particular interest:

MailKitMultiAttachments

Where we are seeing:

  • Inline attachment for the HTML body
  • File attachment for the card

We also have the plain text version of the email.

MailKitMultiText

TLDR

The BodyBuilder object in MailKit allows for the construction of elaborate, multi-format email messages.

The code is in my GitHub.

Happy hacking!