I am using Mailkit to send messages. This is what I am doing:
var multipart = new Multipart("alternative")
{
new TextPart(MimeKit.Text.TextFormat.Html) { Text = messageHtml },
new TextPart(MimeKit.Text.TextFormat.Text) { Text = messagePlain }
};
var mimeMessage = new MimeMessage()
{
Subject = emailTemplate.Subject,
Body = multipart
};
mimeMessage.From.Add(new MailboxAddress(fromName, fromEmail));
mimeMessage.To.Add(new MailboxAddress(name, email));
using (var client = new SmtpClient())
{
await client.ConnectAsync(options.Server, options.Port.Value, SecureSocketOptions.Auto, ct);
await client.AuthenticateAsync(Encoding.UTF8, options.Username, options.Password, ct);
await client.SendAsync(mimeMessage, ct);
await client.DisconnectAsync(true, ct);
}
The HTML version starts like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="width=device-width, initial-scale=1.0">
The message is sent to a Gmail account. The HTML version of the message is displayed correctly when viewed on mobile. However, when viewing the message in the browser, the plain text version is being displayed.
Am I missing some settings?
Problem solved.
The order of text parts if of importance, starting from the last one:
var multipart = new Multipart("alternative")
{
new TextPart(MimeKit.Text.TextFormat.Text) { Text = messagePlain },
new TextPart(MimeKit.Text.TextFormat.Html) { Text = messageHtml }
};