Chapter 13. Web Applications Tutorial 171
msg.send();
Sending Rich Text Messages
Rich text messages (i.e. HTML) require using the setBody(html,alternate) method to
specify both the HTML and plain text versions of a message to send out:
Mail msg = new Mail(to, from, subject);
msg.setBody("
p This is the HTML body /p ",
"This is the plain text body");
msg.send();
Attachments
Message attachments are handled using one of the various attach(...) methods. Support is
provided for File, URL and ByteArray datasources. The URL datasource is the simplest to use
(and can also fetch from a local file using the appropriate protocol specification). All attach-
ments require a name, a description, and an optional disposition. The MIME type is determined
automatically.
Forexample, the following code fragment shows how toattach animage whose content isfetched
from a URL:
URL url = new URL("http://example.com/image.gif");
Mail msg = new Mail(to, from, subject, body);
msg.attach(url, "image.gif", "A sample image");
msg.send();
The protocol for attaching content from a local file is similar:
File path = new File("image.gif");
Mail msg = new Mail(to, from, subject, body);
msg.attach(path, "image.gif", "A sample image");
msg.send();
The Mail service provides a utility class called ByteArrayDataSource that can be used to
attach virtually any type of content contained in memory by supplying its data as a byte[]
array.