While the examples above demonstrate how to create EmailEvent
objects describing simple email messages, they do not show you how to create messages with different recipient types, multipart messages, or messages with file attachments. These more sophisticated capabilities can be achieved using JavaMail’s javax.mail.Message
class to describe the email message.
You can create Message
objects yourself via method calls to one of the Message
child classes, such as javax.mail.internet.MimeMessage
. Alternatively, you can use the atg.service.email.MimeMessageUtils
helper class to create and fill in MimeMessage
objects. For example, here is how one might use MimeMessageUtils
to create the simple email message shown in the previous section:
Message msg = MimeMessageUtils.createMessage(); MimeMessageUtils.setFrom(msg, "dynamotester"); msg.setSubject("I'm just testing the e-mail sender"); MimeMessageUtils.setRecipient(msg, RecipientType.TO, "test@example.com"); msg.setText("Sorry to bother you, but I'm testing the e-mail sender");
or, alternatively,
Message msg = MimeMessageUtils.createMessage ("dynamotester", "I'm just testing the e-mail sender", "test@example.com", "Sorry to bother you, but I'm testing the e-mail sender");
MimeMessageUtils
can also be used to create much more complex Message
objects. For example, here is how one might create a multi-part message with a text/plain
part and a text/html
part, a file attachment, and several kinds of recipients:
// create a Message with the given From and Subject Message msg = MimeMessageUtils.createMessage("dynamotester", "more complex test"); // set the To and Bcc recipients MimeMessageUtils.setRecipient(msg, Message.RecipientType.TO, "test@example.com"); MimeMessageUtils.setRecipient(msg, Message.RecipientType.BCC, "dynamotester"); // set the Cc recipients String[] ccAddresses = { "fred@example.com", "jane@example.com" }; MimeMessageUtils.setRecipients(msg, Message.RecipientType.CC, ccAddresses); // set the message content: multipart message + attachment ContentPart[] content = { new ContentPart("this is plain text", "text/plain"), new ContentPart("this is <b>html</b> text", "text/html") }; File attachment = new File("attachment.html"); MimeMessageUtils.setContent(msg, content, attachment, false);
After you have a Message
object, you can use it to set the email event’s message
property:
EmailEvent em = new EmailEvent();
em.setMessage(msg);
Or, more simply,
EmailEvent em = new EmailEvent(msg);