Class 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();

    name is the name of an email template in dspace-dir/config/emails/ (which also includes the subject.) arg0 and arg1 are 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-mail and 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
    • Constructor Detail

      • Email

        public Email()
        Create a new email 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 - addArgument will start over. Comments and any "Subject:" line must be stripped.
        Parameters:
        name - a name for this message body
        cnt - 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​(File f,
                                  String name)
      • 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,
                         IOException
        Sends the email. If the template defines a Velocity context property named among the values of DSpace configuration property mail.message.headers then 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.