0 votes
by (170 points)

Applies to: Rebex Secure Mail

2 Answers

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

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();

mail.BodyHtml = "<html>Body html</html>"; 
mail.BodyText = "text body";

mail.BodyText = "text body";
mail.BodyHtml = "<html>Body html</html>"; 

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 (150k 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)

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

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