Class MailBuilder

java.lang.Object
net.datenwerke.rs.core.service.mail.MailBuilder

public class MailBuilder extends Object
Builder class for constructing an email step-by-step by providing the necessary elements using the builder pattern. By default, the email is sent using the ReportServer internal email settings. If you need to send the email to a given EmailDatasink, you can use withEmailDatasink(EmailDatasink). A typical email can be constructed and sent as shown below.
 {
    @code
    SimpleMail mail = mailBuilderFactory.create("my subject text", "my body text", Arrays.asList(currentUser))
          .withEmailDatasink(emailDatasink).withFileAttachments(files).withZippedAttachments("myAttachments.zip")
          .build();
    mailService.sendMail(mail);
 }
 
where
  • mailBuilderFactory denotes the MailBuilderFactory for creating the mail builder
  • emailDatasink denotes the EmailDatasink used for sending the email
  • currentUser denotes the current User as the e-mail recipient
  • files denotes the list of attachments to add
  • Finally, the e-mail is sent using the MailService
  • Constructor Details

    • MailBuilder

      public MailBuilder(<any> mailServiceProvider, <any> tempFileServiceProvider, <any> zipServiceProvider, <any> mimeUtilsProvider, String subject, String body, List<User> recipients, InternetAddress from)
  • Method Details

    • withTemplateReplacements

      public MailBuilder withTemplateReplacements(Map<String,Object> replacements)
      Specifies that the email contains a template and specifies which replacements should be used for template substitution.
      Parameters:
      replacements - the replacements for template substitution
      Returns:
      the current state of MailBuilder
    • withFileAttachments

      public MailBuilder withFileAttachments(List<Path> attachments)
      Adds a list of files as email attachments.
      Parameters:
      attachments - the list of files to add as email attachments.
      Returns:
      the current state of MailBuilder
    • withAttachments

      public MailBuilder withAttachments(List<SimpleAttachment> attachments)
      Adds a list of attachments. These can contain byte arrays or Paths.
      Parameters:
      attachments - the attachments as byte arrays or Paths
      Returns:
      the current state of MailBuilder
    • withZippedAttachments

      public MailBuilder withZippedAttachments(String zipFilename)
      Specifies that the attachments should be zipped using the provided filename.
      Parameters:
      zipFilename - the zip filename.
      Returns:
      the current state of MailBuilder
    • withEmailDatasink

      public MailBuilder withEmailDatasink(EmailDatasink emailDatasink)
      Specifies that the given EmailDatasink should be used instead of the default (internal) email settings.
      Parameters:
      emailDatasink - the EmailDatasink to use.
      Returns:
      the current state of MailBuilder
    • build

      public SimpleMail build() throws IOException, MessagingException
      Builds the email using the previously specified options.
      Returns:
      the constructed email.