Class Email
- java.lang.Object
-
- org.dspace.core.Email
-
public class Email extends Object
Class representing an e-mail message, also used to send e-mails.Typical use:
Email email = new Email();
email.addRecipient("foo@bar.com");
email.addArgument("John");
email.addArgument("On the Testing of DSpace");
email.send();
nameis the name of an email template indspace-dir/config/emails/(which also includes the subject.)arg0andarg1are arguments to fill out the message with.Emails are formatted using Apache Velocity. Headers such as Subject may be supplied by the template, by defining them using #set(). 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]}".If the example code above was used to send this mail, the resulting mail would have the subject
Example e-mailand the body would be:Dear John, Thank you for sending us your submission "On the Testing of DSpace".- Author:
- Robert Tansley, Jim Downing - added attachment handling code, Adan Roman Ruiz at arvo.es - added inputstream attachment handling code
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEmail.InputStreamDataSourcestatic classEmail.UnmodifiableConfigurationServiceWrap ConfigurationService to prevent templates from modifying the configuration.
-
Constructor Summary
Constructors Constructor Description Email()Create a new email message.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddArgument(Object arg)Fill out the next argument in the templatevoidaddAttachment(File f, String name)voidaddAttachment(InputStream is, String name, String mimetype)voidaddRecipient(String email)Add a recipientstatic EmailgetEmail(String emailFile)Get the VTL template for an email message.static voidmain(String[] args)Test method to send an email to check email server settingsvoidreset()"Reset" the message.voidsend()Sends the email.voidsetCharset(String cs)voidsetContent(String name, String cnt)Set the content of the message.voidsetReplyTo(String email)Set the reply-to email addressvoidsetSubject(String s)Set the subject of the message
-
-
-
Method Detail
-
addRecipient
public void addRecipient(String email)
Add a recipient- Parameters:
email- the recipient's email address
-
setContent
public void setContent(String name, String cnt)
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 bodycnt- the content of the message
-
setSubject
public void setSubject(String s)
Set the subject of the message- Parameters:
s- the subject of the message
-
setReplyTo
public void setReplyTo(String email)
Set the reply-to email address- Parameters:
email- the reply-to email address
-
addArgument
public void addArgument(Object arg)
Fill out the next argument in the template- Parameters:
arg- the value for the next argument
-
addAttachment
public void addAttachment(InputStream is, String name, String mimetype)
-
setCharset
public void setCharset(String cs)
-
reset
public void reset()
"Reset" the message. Clears the arguments, attachments and recipients, but leaves the subject and content intact.
-
send
public void send() throws javax.mail.MessagingException, IOExceptionSends the email. If the template defines a Velocity context property named among the values of DSpace configuration propertymail.message.headersthen that name and its value will be added to the message's headers."subject" is treated specially: if
setSubject()has not been called, the value of any "subject" property will be used as if setSubject had been called with that value. Thus a template may define its subject, but the caller may override it.- Throws:
javax.mail.MessagingException- if there was a problem sending the mail.IOException- if IO error
-
getEmail
public static Email getEmail(String emailFile) throws IOException
Get the VTL template for an email message. The message is suitable for inserting values using Apache Velocity.- 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
public static void main(String[] args)
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.
-
-