Category Image QuickStart WebObjects Email using Project Wonder's ERJavaMail Framework


If you have never sent mail in java or WebObjects before, then it may be a daunting task. Having read the community mailing list threads, it seems that although WOMailDelivery may be easy to use, the consensus seems to be that using Sun's javamail or ERJavaMail from Project Wonder, which integrates javamail functionality into WebObjects cleanly, is the way to go. This article outlines the minimum usage of Project Wonder just to get solid email functionality with little effort on your part. This article uses the latest binary release , however if you want to really get started with Project Wonder in general, I highly recommend you go straight to source download, compile and install. It is really easy and I have an article providing instructions . Meanwhile, if you are feeling a little cautious and just want to use ERJavaMail, then follow these instructions ......

Here are the steps.

  • First download the Project Wonder binaries (precompiled frameworks)
  • Expand the zip file.
  • Copy at least ERJavaMail.framework, ERJars.framework and ERExtensions.frameworks to /Library/Frameworks.
  • In your XCode project, right-click on the Frameworks group and Add/Existing Frameworks... and choose /Library/Frameworks/ERJavaMail.framework. Also add ERJars and ERExtensions.
  • Fig. 1 shows a simple class with a static method that sends HTML mail using a WebObjects page
  • Fig. 2 shows example usage of the class.
  • Fig. 3 shows example text for you to copy/paste into your Properties file in your project for configuring Wonder.
  • Configuring Wonder's logging output is recommended too.



Fig. 1
//
// WkMailUtils.java
// cheetah
//
// Created by Kieran Kelleher on 1/28/05.
// Free to use as you wish. Use at your own risk.
//

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import org.apache.log4j.Logger;
import er.javamail.*;

public class WKMailUtils {
private static Logger log = Logger.getLogger( WKMailUtils.class );


public static void composeAndSendComponentMail( WOComponent emailPage,
NSDictionary emailHeaders ) {
// Requires no null values

composeAndSendComponentMail(emailPage,
(String)emailHeaders.valueForKey("fromAddress"),
(String)emailHeaders.valueForKey("fromPersonalName"),
(String)emailHeaders.valueForKey("toAddress"),
(String)emailHeaders.valueForKey("toPersonalName"),
(NSArray)emailHeaders.valueForKey("toAddresses"),
(String)emailHeaders.valueForKey("toPersonalName"),
(String)emailHeaders.valueForKey("toPersonalName") );
}



public static void composeAndSendComponentMail( WOComponent emailPage,
String fromAddress,
String fromPersonalName,
String toAddress,
String toPersonalName,
NSArray toAddresses,
String replyToAddress,
String subject ) {

if (log.isDebugEnabled()) log.debug("Sending email with subject '"
+ subject + "' and addressed to '"
+ (toAddress == null ? "" : toAddress.toString() )
+ (toAddresses == null ? "" : toAddresses.toString() ) );

// Create a new mail delivery instance
ERMailDeliveryHTML eMail = new ERMailDeliveryHTML();

// Set the WOComponent to be used for rendering the mail
eMail.setComponent( emailPage );

try {
eMail.newMail();

// fromAddress with optional fromPersonalName
if ( fromAddress != null && fromPersonalName != null ) {
eMail.setFromAddress( fromAddress, fromPersonalName );
} else if (fromAddress != null) {
eMail.setFromAddress( fromAddress );
}

// optional toAddress and optional toPersonalName
if ( toAddress != null && toPersonalName != null ) {
eMail.setToAddress( toAddress, toPersonalName );
} else if (toAddress != null) {
eMail.setToAddress( toAddress );
}

// optional toAddresses (NSArray)
if ( toAddresses != null ) eMail.setToAddresses( toAddresses );

// reply to address
if ( replyToAddress != null ) eMail.setReplyToAddress( replyToAddress );

eMail.setSubject( subject );

eMail.sendMail();

} catch (Exception e) {
log.error("Exception sending email: " + e);
}
}



}


Fig. 2
// Send confirmation Email
WzdNC_ConfirmationEmail emailContent = (WzdNC_ConfirmationEmail)pageWithName("WzdNC_ConfirmationEmail");

// Give the page any ivars it needs to generate itself
emailContent.setCustomer( customer() );

WKMailUtils.composeAndSendComponentMail(emailContent,
"abc@florida.com", "Florida Oranges, Inc.",
"me@somewhere.com","Somebody",
null,
"aperson@somewhere.com",
"Subscription Confirmation");


WzdNewCustomer_Confirmation nextPage = (WzdNewCustomer_Confirmation)pageWithName("WzdNewCustomer_Confirmation");
nextPage.setCustomer( customer() );

// NOTE: Returning null after composing mail may return the actual mail page itself, so
// make sure you return a specific page, even 'this', after sending mail with ERJavaMail

return nextPage;

Fig. 3
#########################################################################
# ERXLogger
#########################################################################
# Pattern codes:
# %-5p : Priority DEBUG, INFO, WARN, etc.
# %d(ISO08601 : Date and time
# %t : Thread
# %c : Logger name (usually a class name)
# %m%n : Log message arguments

# Available levels are DEBUG, INFO, WARN, ERROR, FATAL

//log4j appender and formatting
log4j.rootLogger=WARN, A1
log4j.loggerFactory=er.extensions.ERXLogger$Factory

# A1 is a ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=er.extensions.ERXPatternLayout
log4j.appender.A1.layout.ConversionPattern=%r %-5p [%t] (%C, %M, %L) - %n%m%n%n

# Project Wonder logger configuration
# ---------------------------
log4j.logger.er=WARN
er.extensions.ERXNSLogLog4jBridge=DEBUG




#########################################################################
# ERXAdaptorChannelDelegate
#########################################################################
# er.extensions.ERXAdaptorChannelDelegate.enabled=false
# er.extensions.ERXAdaptorChannelDelegate.maxMilliSeconds=10
# log4j.category.er.extensions.ERXAdaptorChannelDelegate.sqlLogging

#########################################################################
# ERXEC
#########################################################################
# decides if new ECs use the shared editing context
er.extensions.ERXEC.useSharedEditingContext=true

#########################################################################
# ERXApplication
#########################################################################
# decides if locked editing contexts should be unlocked after the RR-loop.
er.extensions.ERXApplication.useEditingContextUnlocker=false

er.extensions.ERXRaiseOnMissingEditingContextDelegate=false
er.extensions.ERXLocalizer.isLocalizationEnabled=false


#########################################################################
# ERJavaMail settings
#########################################################################
# *** ERJavaMail settings
# *** Defaults to 'safe' settings:
# ***

# Sometimes when sending big masses of emails, the java virtual machine
# doesn't collects correctly the garbage and needs a little delay
# between each message sending. Use this option if you have problem with
# memory management
er.javamail.useSenderDelay = false

# This delay is set to 15 milliseconds
er.javamail.senderDelayMillis = 15

# * Default encoding used for ERJavaMail
er.javamail.defaultEncoding = iso-8859-1

# * Enables ERJavaMail to send all the mails to only one destination
# This is useful when debugging
er.javamail.centralize = false

# * Prints javamail debug infos
# It is recommended to activate this switch when there are problems
# with smtp configuration
er.javamail.debugEnabled = false

# * The admin email, this is where "centralized" mails will go
er.javamail.adminEmail = developer@mac.com

# * SMTP outbound host
er.javamail.smtpHost = smtpserver.mydomain.com

# * Number of messages that the sender queue can hold at a time
er.javamail.senderQueue.size = 50

# * Time to wait when mail sender queue is overflowed
er.javamail.milliSecondsWaitIfSenderOverflowed = 6000

# Used to set a default X-Mailer
#er.javamail.mailer.XMailerHeader =

# White and black email address patterns
# This can be useful in testing when say
# you only want to allow emails to be sent to *@mycompany.com
# In this case you would add:
# er.javamail.WhiteListEmailAddressPatterns=("*@mycompany.com", "somebody@mac.com")

# To prevent sending mail to certain addresses you can use the black list
# er.javamail.BlackListEmailAddressPatterns=("*@baddomain.com", "badperson@mycompany.com")

Posted: Thursday - January 27, 2005 at 07:58 PM        


Published by