WebObjects/Project WONDER/Frameworks/ERJavaMail

Overview
ERJavaMail provides a simple and powerful API for sending component-based emails from a WebObjects application. ERJavaMail does not depend on any other pieces of Project Wonder, and is a good way to get your feet wet using Wonder.

Configuration
Centralize sends all emails to the er.javamail.adminEmail user. Determines whether or not email debugging is displayed. This contains protocol-level debug information. The email address of the admin user to send centralized emails to. This is a required property. The SMTP host name to use. If this isn't set, mail.smtp.host will be checked and ultimately WOHost will be used. The number of messages that the sender queue can hold. Defaults to 50. The number of milliseconds to wait if the sender queue is full. Default is 6000. Sets whether or not Authenticated SMTP is used to send outgoing mail. If set, er.javamail.smtpUser MUST also be set (and preferably er.javamail.smtpPassword). The username to use to login to the authenticated SMTP server. The password to use to login to the authenticated SMTP server. The X-Mailer header to put into all outgoing mail messages. Defaults to nothing. The default character encoding to use for message content. Defaults to ???. A comma-separated list of whitelisted email address patterns. If set, then only addresses that match one of the whitelisted patterns will delivered to. Pattern syntax is the same as EOQualifier's caseInsensitiveLike. A comma-separated list of blacklisted email address patterns. If set, then any email addresses that match a blacklist pattern will not be delivered to. Pattern syntax is the same as EOQualifier's caseInsensitiveLike. The blacklist filter is processed last, so a blacklist pattern beats a whitelist pattern.
 * er.javamail.centralize = true
 * er.javamail.debugEnabled = true
 * er.javamail.adminEmail = user@domain.com
 * er.javamail.smtpHost = smtp.domain.com
 * er.javamail.senderQueue.size = 50
 * er.javamail.milliSecondsWaitIfSenderOverflowed = 6000
 * er.javamail.smtpAuth = true
 * er.javamail.smtpUser = smtpusername
 * er.javamail.smtpPassword = smtppassword
 * er.javamail.XMailerHeader =
 * er.javamail.defaultEncoding = UTF-8
 * er.javamail.WhiteListEmailAddressPatterns =
 * er.javamail.BlackListEmailAddressPatterns =

Example Usage
// Create an instance of an ERMailDelivery subclass ERMailDeliveryHTML mail = new ERMailDeliveryHTML ; // Here ERMailDeliveryHTML needs a WOComponent to  // render the HTML text content. mail.setComponent(mailPage); // Here you create a new instance of the message // You can loop over this fragment of code, not // forgetting to use newMail // before you set the attributes of the message. try { mail.newMail; mail.setFromAddress(emailFrom); mail.setReplyToAddress(emailReplyTo); mail.setSubject(emailSubject); mail.setToAddresses(new NSArray (toEmailAddresses)); // Send the mail. There is an optional sendMail(boolean) that // optionally blocks during the send. mail.sendMail; } catch (Exception e) { // handle the exception ... }

Example of sending Mail with Attachments
The subclasses of ERMailDelivery will not only deliver the mail for you, but also will create the message. So ERMailDelivery is in some way more like a message than a delivery mechanism. For each message you want to sent instantiate a concrete subclass of ERMailDelivery (e.g. ERMailDeliveryPlainText). You can then add attachments to it. Below a simple code snippet sending of a mail with an attachment. The MimeType of the attachment is parsed out of the extension of the filename.

Sending mail is per default asynchronous, you can specify a flag (true == shouldBlock, false == should not block) to influence the behaviour.

byte[] content; // assume this exist, same interface exists for NSData, too // Create an instance of an ERMailDelivery subclass ERMailDeliveryPlainText message = new ERMailDeliveryPlainText; // set the text and subject message.setTextContent("Some Mail text"); message.setSubject("The mail subject"); // add the attachment message.addAttachment(new ERMailDataAttachment("myattachment.zip", null, content)); message.setToAddress("receiver@bitbucket.com"); message.setFromAddress("sender@bitbucket.com"); // send the mail asynchronously message.sendMail;

Gotchas
Be careful of the WOContext that contains the component you are sending. If you use ERMailDeliveryHTML inside of the normal request-response loop with the default WOContext, it is very likely that the next page that is sent to the user will be the emailed component rather than the page you WANTED to send. There are several possible workarounds for this. One is to return a specific component rather than null from your action method. I have had better and more consistent success with the following code:

WOContext context = (WOContext) context.clone; MyComponent component = (MyComponent) WOApplication.application.pageWithName(MyComponent.class.getName, context); ERMailDeliveryHTML mail = new ERMailDeliveryHTML; mail.setComponent(component); ...

This seems to properly isolate the email to a clone of the current context rather than the actual active context. Your mileage may vary.