Free Support Forum -

Body- TextBody- HtmlBody- CreateAlternateViewFromString: what are the differences?


Can you please explain the actual differences between the following concepts? The documentation is not very verbose on this matter.

1- emailMessage.Body = …
2- emailMessage.TextBody = …
3- emailMessage.HtmlBody = …
Aspose.Network.Mail.AlternateView view = Aspose.Network.Mail.AlternateView.CreateAlternateViewFromString(…);
view.TransferEncoding = …;

Is Body obsolete? Should we use TextBody or HtmlBody when doing a “single type” email and AlternateView when providing both plain text and html versions? If so, please mark Body with the [Obsolete] attribute.

Secondly, I also noticed that the following code creates a body text in UTF-8 (incorrect encoding):
msg.TextBody = mMsgBody;
msg.BodyEncoding = System.Text.Encoding.GetEncoding(28591); // ISO-8859-1 (Western ISO)

But inverting the two lines of code creates a body text in ISO-8859-1 (as expected):
msg.BodyEncoding = System.Text.Encoding.GetEncoding(28591); //
ISO-8859-1 (Western ISO)
msg.TextBody = mMsgBody;

To prevent simple mistakes, I suggest that you throw an InvalidOperationException when trying to set the BodyEncoding after setting the body. In my mind, allowing the user to incorrectly use a component in a way that will provide an incorrect result is almost as bad as the component doing the incorrect thing itself…

Thanks for your answers and considering my suggestions,

Using Aspose.Network…

Hi Dominic,

Below is the descriptions of properties/methods.

  1. Body: sets text body of email
  2. TextBody: sets text body of email
  3. HtmlBody: sets Html body of email
  4. AlternateViews: can be used to set alternate plain/html text in email message. Appointments and meeting requests are also added to the email using AlternateView.
For sending “single type” emails, you may either use TextBody or HtmlBody.

For sending both plain text and Html emails, you need to set both TextBody and HtmlBody properties. Adding alternate view is not necessary in this case. Below is an example that sets both properties. Text only email clients will only see the TextBody and Html mail clients will see the HtmlBody.

MailMessage msg = new MailMessage();
msg.TextBody = “text body”; // text only email clients
msg.HtmlBody = “Html body with an image. Here is an embedded image.”; // Html email clients e.g. Outlook
LinkedResource logo = new LinkedResource(“test.png”, MediaTypeNames.Image.Jpeg);
logo.ContentId = “companylogo”;
SmtpClient client = new SmtpClient(…);

MailMessage.Body and TextBody both set the text body in email. We will investigate further and mark as obsolete if necessary.

Regarding the encoding issue, we will look into it as well. Sorry for the inconvenience.