Class Email
send() method causes the
assembled message to be formatted and sent.
Typical use:
Email email = Email.getEmail(path);email.addRecipient("foo@bar.com");email.addArgument("John");email.addArgument("On the Testing of DSpace");email.send();
path is the filesystem path of an email template, typically in
${dspace.dir}/config/emails/ and can include the subject -- see
below. Templates are processed by
Apache Velocity. They may contain VTL directives and property
placeholders.
addArgument(java.lang.Object) adds a property to the params array
in the Velocity context, which can be used to replace placeholder tokens
in the message. These arguments are indexed by number in the order they were
added to the message.
The DSpace configuration properties are also available to templates as the
array config, indexed by name. Example: ${config.get('dspace.name')}
Recipients and attachments may be added as needed. See addRecipient(java.lang.String),
addAttachment(java.io.File,java.lang.String), and
addAttachment(java.io.InputStream,java.lang.String,java.lang.String).
Headers such as Subject may be supplied by the template, by defining them
using the VTL directive #set(). Only headers named in the DSpace
configuration array property mail.message.headers will be added.
Example:
## This is a comment line which is stripped
##
## Parameters: {0} is a person's name
## {1} is the name of a submission
##
#set($subject = 'Example e-mail')
Dear ${params[0]},
Thank you for sending us your submission "${params[1]}".
--
The ${config.get('dspace.name')} Team
If the example code above was used to send this mail, the resulting mail
would have the subject Example e-mail and the body would be:
Dear John,
Thank you for sending us your submission "On the Testing of DSpace".
--
The DSpace Team
There are two ways to load a message body. One can create an instance of
Email and call setContent(java.lang.String,java.lang.String) on it, passing the body as a String. Or
one can use the static factory method getEmail(java.lang.String) to load a file by its
complete filesystem path. In either case the text will be loaded into a
Velocity template.
- Author:
- Robert Tansley, Jim Downing - added attachment handling code, Adan Roman Ruiz at arvo.es - added inputstream attachment handling code
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classWrap anInputStreamin aDataSource.static classWrap ConfigurationService to prevent templates from modifying the configuration. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddArgument(Object arg) Fill out the next argument in the template.voidaddAttachment(File f, String name) Add an attachment bodypart to the message from an external file.voidaddAttachment(InputStream is, String name, String mimetype) Add an attachment bodypart to the message from a byte stream.voidaddRecipient(String email) Add a recipient.static EmailGet the VTL template for an email message.static voidTest method to send an email to check email server settingsvoidreset()"Reset" the message.voidsend()Sends the email.voidsetCharset(String cs) Set the character set of the message.voidsetContent(String name, String content) Set the content of the message.voidsetReplyTo(String email) Set the reply-to email address.voidsetSubject(String s) Set the subject of the message.
-
Constructor Details
-
Email
public Email()Create a new email message.
-
-
Method Details
-
addRecipient
Add a recipient.- Parameters:
email- the recipient's email address
-
setContent
Set the content of the message. Setting this also "resets" the message formatting -addArgumentwill start over. Comments and any "Subject:" line must be stripped.- Parameters:
name- a name for this message bodycontent- the content of the message
-
setSubject
Set the subject of the message.- Parameters:
s- the subject of the message
-
setReplyTo
Set the reply-to email address.- Parameters:
email- the reply-to email address
-
addArgument
Fill out the next argument in the template.- Parameters:
arg- the value for the next argument. Ifnull, a zero-length string is substituted.
-
addAttachment
Add an attachment bodypart to the message from an external file.- Parameters:
f- reference to a file to be attached.name- a name for the resulting bodypart in the message's MIME structure.
-
addAttachment
Add an attachment bodypart to the message from a byte stream.- Parameters:
is- the content of this stream will become the content of the bodypart.name- a name for the resulting bodypart in the message's MIME structure.mimetype- the MIME type of the resulting bodypart, such as "text/pdf". Ifnullit will default to "application/octet-stream", which is MIME for "unknown format".
-
setCharset
Set the character set of the message.- Parameters:
cs- the name of a character set, such as "UTF-8" or "EUC-JP".
-
reset
public void reset()"Reset" the message. Clears the arguments, attachments and recipients, but leaves the subject and content intact. -
send
Sends the email. If sending is disabled then the assembled message is logged instead.- Throws:
jakarta.mail.MessagingException- if there was a problem sending the mail.IOException- if IO error
-
getEmail
Get the VTL template for an email message. The message is suitable for inserting values using Apache Velocity.Note that everything is stored here, so that only send() throws a MessagingException.
- Parameters:
emailFile- full name for the email template, for example "/dspace/config/emails/register".- Returns:
- the email object, configured with subject and body.
- Throws:
IOException- if IO error if the template couldn't be found, or there was some other error reading the template
-
main
Test method to send an email to check email server settings- Parameters:
args- command line arguments. The first is the path to an email template file; the rest are the positional arguments for the template. If there are no arguments, a short, plain test message is sent.
-