Esempio n. 1
0
 public final Email buildEmail() {
   final Email email = new Email();
   email.setAttachments(getAttachments());
   email.setContent(getContent());
   email.setSubject(subject);
   email.setTo(getTo());
   return email;
 }
  /**
   * Get the template for an email message. The message is suitable for inserting values using
   * <code>java.text.MessageFormat</code>.
   *
   * @param emailFile full name for the email template, for example
   *     "/dspace/config/emails/register".
   * @return the email object, with the content and subject filled out from the template
   * @throws IOException if the template couldn't be found, or there was some other error reading
   *     the template
   */
  public static Email getEmail(String emailFile) throws IOException {
    String charset = null;
    String subject = "";
    StringBuffer contentBuffer = new StringBuffer();

    // Read in template
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(emailFile));

      boolean more = true;

      while (more) {
        String line = reader.readLine();

        if (line == null) {
          more = false;
        } else if (line.toLowerCase().startsWith("subject:")) {
          // Extract the first subject line - everything to the right
          // of the colon, trimmed of whitespace
          subject = line.substring(8).trim();

          // 20110611 by Pudding Chen
          Date date = new Date();
          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String time = df.format(date);
          subject = subject + " (" + time + ")";
        } else if (line.toLowerCase().startsWith("charset:")) {
          // Extract the character set from the email
          charset = line.substring(8).trim();
        } else if (!line.startsWith("#")) {
          // Add non-comment lines to the content
          contentBuffer.append(line);
          contentBuffer.append("\n");
        }
      }
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
    // Create an email
    Email email = new Email();
    email.setSubject(subject);
    email.setContent(contentBuffer.toString());

    if (charset != null) email.setCharset(charset);

    return email;
  }