magoffin.matt.ma2.util
Class MailMergeHelper

java.lang.Object
  extended by magoffin.matt.ma2.util.MailMergeHelper

public class MailMergeHelper
extends java.lang.Object

A bean to support MailMerge email sending.

The email templates are handled also in a locale-specific way. The templatePath property should be set to the directory that contains the message templates, and the templateName should be set to the desired template to use, without any locale in the name. The performMerge(String, ClassLoader, Map) will construct a path to the template resource by inserting the language before the template extension. For example, if templatePath is set to mailtemplates and templateName set to confirmation.txt, then for the English language it will look for a resource in the classpath at mailtemplates/confirmation_en.txt.

The configurable properties of this class are:

templatePath
The classpath-relative path to the directory containing the locale-specific mail merge template files.
templateName
The file name of the mail merge template, without any locale suffix.
messageTemplate
A mail message to use as a template for the outgoing mail message. This can be used to configure the from address, for example.
mailsender
The MailSender to use to send the email.
messageSource
The MessageSource to use for resource messages.
subjectMessageKey
The resource message key to use for the subject of the email message.
ignoreMailExceptions
If true then ignore all mail exceptions. This can be useful for development, when you may not have access to a real mail server. Defaults to false.

Version:
$Revision: 43 $ $Date: 2007-10-21 11:10:01 +1300 (Sun, 21 Oct 2007) $
Author:
Matt Magoffin (spamsqr@msqr.us)

Field Summary
static java.lang.String MSG_NO_SUBJECT_KEY
          The key for the message resource for a "no subject" subject.
 
Constructor Summary
MailMergeHelper()
           
 
Method Summary
 org.springframework.mail.MailSender getMailSender()
           
 org.springframework.context.MessageSource getMessageSource()
           
 org.springframework.mail.SimpleMailMessage getMessageTemplate()
           
 java.lang.String getSubjectMessageKey()
           
 java.lang.String getTemplateName()
           
 java.lang.String getTemplatePath()
           
 boolean isIgnoreMailExceptions()
           
 java.lang.String performMerge(java.lang.String lang, java.lang.ClassLoader loader, java.util.Map<java.lang.String,?> model)
          Perform a mail merge.
 void send(org.springframework.mail.SimpleMailMessage msg)
          Send a mail message.
 void sendMerge(java.util.Locale locale, java.lang.ClassLoader loader, java.util.Map<java.lang.String,?> model, org.springframework.mail.SimpleMailMessage msg)
          Perform a mail merge and send an email with the result of the merge as the email message text.
 void setIgnoreMailExceptions(boolean ignoreMailExceptions)
           
 void setMailSender(org.springframework.mail.MailSender mailSender)
           
 void setMessageSource(org.springframework.context.MessageSource messageSource)
           
 void setMessageTemplate(org.springframework.mail.SimpleMailMessage messageTemplate)
           
 void setSubjectMessageKey(java.lang.String subjectMessageKey)
           
 void setTemplateName(java.lang.String templateName)
           
 void setTemplatePath(java.lang.String templatePath)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MSG_NO_SUBJECT_KEY

public static final java.lang.String MSG_NO_SUBJECT_KEY
The key for the message resource for a "no subject" subject.

See Also:
Constant Field Values
Constructor Detail

MailMergeHelper

public MailMergeHelper()
Method Detail

sendMerge

public void sendMerge(java.util.Locale locale,
                      java.lang.ClassLoader loader,
                      java.util.Map<java.lang.String,?> model,
                      org.springframework.mail.SimpleMailMessage msg)
Perform a mail merge and send an email with the result of the merge as the email message text.

The msg should have all required fields already set prior to calling this method. The text property of the message will be set to the result of the mail merge. If the msg has a null subject, then the subject will be set to the resource message specified by the subjectMessageKey of this bean. If subjectMessageKey is null then the MSG_NO_SUBJECT_KEY resource message will be used.

Parameters:
locale - the locale for the mail merge template. If null the default Locale will be used.
loader - the class loader to use
model - the merge model data
msg - the mail message

send

public void send(org.springframework.mail.SimpleMailMessage msg)
Send a mail message.

Parameters:
msg - the message to send
Throws:
org.springframework.mail.MailException - if an error occurs

performMerge

public java.lang.String performMerge(java.lang.String lang,
                                     java.lang.ClassLoader loader,
                                     java.util.Map<java.lang.String,?> model)
Perform a mail merge.

Parameters:
lang - the language for the mail merge template
loader - the class loader to use
model - the merge model data
Returns:
the merged message body

getMailSender

public org.springframework.mail.MailSender getMailSender()
Returns:
Returns the mailSender.

setMailSender

public void setMailSender(org.springframework.mail.MailSender mailSender)
Parameters:
mailSender - The mailSender to set.

getMessageSource

public org.springframework.context.MessageSource getMessageSource()
Returns:
Returns the messageSource.

setMessageSource

public void setMessageSource(org.springframework.context.MessageSource messageSource)
Parameters:
messageSource - The messageSource to set.

getMessageTemplate

public org.springframework.mail.SimpleMailMessage getMessageTemplate()
Returns:
Returns the messageTemplate.

setMessageTemplate

public void setMessageTemplate(org.springframework.mail.SimpleMailMessage messageTemplate)
Parameters:
messageTemplate - The messageTemplate to set.

getSubjectMessageKey

public java.lang.String getSubjectMessageKey()
Returns:
Returns the subjectMessageKey.

setSubjectMessageKey

public void setSubjectMessageKey(java.lang.String subjectMessageKey)
Parameters:
subjectMessageKey - The subjectMessageKey to set.

getTemplateName

public java.lang.String getTemplateName()
Returns:
Returns the templateName.

setTemplateName

public void setTemplateName(java.lang.String templateName)
Parameters:
templateName - The templateName to set.

getTemplatePath

public java.lang.String getTemplatePath()
Returns:
Returns the templatePath.

setTemplatePath

public void setTemplatePath(java.lang.String templatePath)
Parameters:
templatePath - The templatePath to set.

isIgnoreMailExceptions

public boolean isIgnoreMailExceptions()
Returns:
the ignoreMailExceptions

setIgnoreMailExceptions

public void setIgnoreMailExceptions(boolean ignoreMailExceptions)
Parameters:
ignoreMailExceptions - the ignoreMailExceptions to set