|Composing HTML Messages|
To compose an HTML formatted message, the ComposeMessage method can be used in the same way that text messages are created. For example, consider the following HTML text:
You could either assign this text to a string, or you could read the message from a file using code like this:
Where strMessageFile contains the HTML message you wish to send. To compose the HTML formatted email, simply call the ComposeMessage method as you would with a plain text message, except that instead of passing the message to the MessageText argument, you pass it to the MessageHTML argument:
nError = MailMessage1.ComposeMessage(editFrom.Text, _ editTo.Text, _ editCc.Text, _ editBcc.Text, _ editSubject.Text, _ "", _ strMessageHTML)
The message that will be sent will now be displayed to the recipient using HTML and will include the formatting (such as font and text size) as well as the hyperlink. However, not all mail clients are capable of displaying HTML email. This poses a problem because the message that they'll receive will be the largely unreadable HTML source. To resolve this problem, create both a plain text version of the message along with the HTML version. Ideally it would contain similar content, although you could provide a simple message which says that this is an HTML email and they should request a plain-text version if they can't display HTML messages. In either case, simply provide both the MessageText and MessageHTML arguments:
nError = MailMessage1.ComposeMessage(editFrom.Text, _ editTo.Text, _ editCc.Text, _ editBcc.Text, _ editSubject.Text, _ strMessageText, _ strMessageHTML)
This will create what is called a multipart/alternative MIME message which contains both plain text and HTML versions of the message. Mail clients which are capable of displaying the HTML message will use that version, while those that cannot will display the plain text version.
It should be noted that there are still some mail clients which do not understand multipart/alternative messages and therefore will display both the plain text and the HTML source text. While confusing, the plain text version will ensure that the message is still readable. For the most part, email is still a plain text medium so if you consider readability and compatibility with older mail software to be more important than formatted text, it is recommended that you use only plain text messages. However, if you know that the recipients have mail clients that are capable of displaying HTML, the Mail Message control makes this easy to do.
Copyright © 2020 Catalyst Development Corporation. All rights reserved.