Actually, both alternate views are sent to the recipient, but the order is important.
According to the RFC 2046, systems should "choose the 'best' type based on the local environment and references", but at the same time it states that "alternatives appear in an order of increasing faithfulness to the original content", which means that (all other things being equal) the last alternate view has higher priority. For this reason, email clients usually put the more informative HTML view last (unless the plain text view is considered a more accurate representation).
So if your client prefers the last alternate view when displaying the email, it is behaving correctly (although choosing one of the other alternatives would be correct as well).
I tried sending two differently composed emails to Gmail and its web client does in fact choose to display the last alternate view. However, many other mail clients (such as Microsoft Outlook) choose to display the HTML body in both cases (even if it's not last).
If I instruct Rebex MailMessage
to put the BodyHtml
as last part, then the HTML body is displayed in the Gmail client (and vice versa for the plain text view).
Here is the code I used for testing:
bool putHtmlLast = true;
var mail = new MailMessage();
// prepare HTML body
var html = new AlternateView();
html.SetContent("<html>This is a <strong>HTML</strong> body.</html>", "text/html");
// prepare plain text body
var text = new AlternateView();
text.SetContent("This is a plain text body.", "text/plain");
if (putHtmlLast)
{
mail.AlternateViews.Add(text);
mail.AlternateViews.Add(html);
mail.Subject = "HTML body last";
// save locally
mail.Save("html-last.eml", MailFormat.Mime);
}
else
{
mail.AlternateViews.Add(html);
mail.AlternateViews.Add(text);
mail.Subject = "Plain text body last";
// save locally
mail.Save("text-last.eml", MailFormat.Mime);
}
mail.From = "rebex.test@gmail.com";
mail.To = "support.rebex.net@gmail.com";
Smtp smtp = new Smtp();
smtp.Connect("smtp.gmail.com", SslMode.Explicit);
smtp.Login("rebex.test@gmail.com", "password");
smtp.Send(mail);
smtp.Disconnect();
You can give it a try and see whether it answers your question.
Please note that if you use the standard MailMessage.BodyHtml
and MailMessage.BodyText
properties instead of AlternateViews.Add
, then the order is fixed BodyHtml
is included as the last alternate view (regardless what you set first). So setting either
mail.BodyHtml = "<html>Body html</html>";
mail.BodyText = "text body";
or
mail.BodyText = "text body";
mail.BodyHtml = "<html>Body html</html>";
will have the same effect and put the HTML view last after the plain text view (unless the message already had both plain text and HTML views set in a reverse order).