Creating and sending HTML-formatted messages (Part 3)

Assembling body text from different sources Summary: Shows various ways to create HTML body of the message.

Tutorial map:
Part 1 - Sending simple HTML e-mail
Part 2 - Importing HTML file into message body
Part 3 - Assembling body text from different sources
Part 4 - Building alternative message body
Part 5 - Advanced topics

Message body content is actually stored in BodyText property of the Message object.

Body format (plain-text, HTML, etc.) is specified through BodyFormat property.

If you already have HTML file that you want to be used as message body, use ImportBodyText method (see Part 2). However, if the file is not available, you can set BodyText property's value manually.

Note: ImportBodyText method sets BodyFormat property itself. However, if ImportBodyText is not used, you have to specify BodyFormat explicitly.


Code example:

' The code below is common for all examples in this topic

Dim Mailer

' Using Visual Basic to create object
Set Mailer = CreateObject("MailBee.SMTP")

' Using ASP to create object
' Set Mailer = Server.CreateObject("MailBee.SMTP")

Mailer.LicenseKey = "put your license key here"
Mailer.ServerName = "mail.server.com"

' The code below is specific to this sample

Mailer.Message.ToAddr = "bill@yoursite.com"
Mailer.Message.FromAddr = "joe@mysite.com"
Mailer.Message.Subject = "Hello"
Mailer.Message.BodyText = "<html><body>HTML Text here</body></html>"

' Mark that body as HTML body
Mailer.Message.BodyFormat = 1

Mailer.Send
Mailer.Disconnect

Sometimes you already have HTML file you want to be imported into message body, but you also want some additional content to be placed into the body (before or after HTML section imported from the file). Another case is when you want to import several files into the same message body (for example, if every file contains incomplete HTML such as Table).

ImportBodyText method of the Message object also supports optional parameter AppendMode that tells MailBee to do not overwrite existing message body, but extend it with new content instead.

You can mix ImportBodyText method calls with direct modifications of BodyText property's value. Sample below uses this technique: Opening and closing tags are set directly in code, but intermediate content is taken from files using ImportBodyText method.


Code example:

' The code below is common for all examples in this topic

Dim Mailer

' Using Visual Basic to create object
Set Mailer = CreateObject("MailBee.SMTP")

' Using ASP to create object
' Set Mailer = Server.CreateObject("MailBee.SMTP")

Mailer.LicenseKey = "put your license key here"
Mailer.ServerName = "mail.server.com"

' The code below is specific to this sample

Mailer.Message.ToAddr = "bill@yoursite.com"
Mailer.Message.FromAddr = "joe@mysite.com"
Mailer.Message.Subject = "Hello"

' Set opening tags of HTML body
Mailer.Message.BodyText = "<html><body>"

' Append 1-st file to HTML body
Mailer.Message.ImportBodyText "C:\docs\email_part1.htm", True, True

' Append 2-nd file to HTML body
Mailer.Message.ImportBodyText "C:\docs\email_part2.htm", True, True

' Add closing tags to HTML body
Mailer.Message.BodyText = Mailer.Message.BodyText & "</body></html>"

' BodyFormat is already set by ImportBodyText method,
' but for clarity, we set it explicitly.
Mailer.Message.BodyFormat = 1

Mailer.Send
Mailer.Disconnect

See Also:

Part 4 (Building alternative message body)

ImportBodyText Method
BodyText Property
BodyFormat Property


Copyright © 2002-2024, AfterLogic Corporation. All rights reserved.