0 votes
by (170 points)

Currently my message contains 2 AlternateViews:
1. MediaType: HTML
2. MediaType: PlainText

And for some reason when sending the message, only the PlainText version is sent to the recipient.

Any ideas?

Applies to: Rebex Secure Mail

2 Answers

+1 vote
by (58.9k points)
selected by
 
Best answer

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).

by (170 points)
edited by
Thank you, that make more sense.
Regarding the assignment order, note that the order is important as for Rebex.Mail 1.0.0
by (58.9k points)
The first version of Rebex Mail ever released was 1.0.2253.0 and it is true that until version 3300 (released on 2009-01-12) the order of setting the BodyHtml and BodyText actually did affect the order of alternate views. This caused troubles for iPhone and Gmail so we fixed it then. But this has been like 7 years. An update to version newer than 3300  would solve it. We recommend to update to the newest version (currently 2016 R1.1).

Link to the latest free trial:
https://www.rebex.net/secure-mail.net/download.aspx

You can see the release notes for version 3300 at https://www.rebex.net/secure-mail.net/history.aspx#3300
by (147k points)
Yes, this has been fixed in Rebex Secure Mail 1.0.3300.0 (released in January 2009) - see http://www.rebex.net/secure-mail.net/history.aspx#3300 for details.
–1 vote
by (58.9k points)

The Rebex.Net.Smtp client sends all the alternate views available. In your case - both the HTML and plain text AlternateViews.

Could you try saving the MailMessage to EML format prior to sending?E.g.

var mail = new MailMessage();
// load the email

mail.Save(@"C:\temp\mail.eml", MailFormat.Mime);

// alternatively check the properties:
if(mail.HasBodyHtml)
    Console.WriteLine(mail.BodyHtml);

if(mail.HasBodyText)
    Console.WriteLine(mail.BodyText);

Smtp smtp = new Smtp();
smtp.Send(mail);

You should see both the HTML and plain text part once you open the saved file. The properties should also work as expected.

by (170 points)
There is any importance to the order in which the AlternateViews are added? it seems like the last one added is the "winner" always, meaning that it is the one that the email client choose to display
by (58.9k points)
Actually, it is really the recipient's mail client (e.g. Outlook, Apple Mail, etc.) that decides which alternate view it will show. I think that most clients enable users to prefer either HTML/plain text view, but if the mail client simply chooses the last alternate view in the MIME that it supports, then it could be it.
by (170 points)
why would gmail choose plaintext?
by (58.9k points)
Simply said Gmail client displays the last alternate view from the email message. If the email has both HTML and plain text parts and the plain text part occurs after the body html, then Gmail prefers it and displays the plain text. However, please note that other email clients can have their own logic when choosing the displayed alternate view.

See my second answer for more details.
...