Hello! I have a list of files. How can I send all files in that list as attachments using the E-Mail Step? The E-Mail Step allows me only to define a known number of attachments. Or is there another way to send a E-Mail with a variable number of attachments?

asked 23.01.2015 at 15:01

Peter%20Weber's gravatar image

Peter Weber
0337
accept rate: 0%


Currently it is not possible to define a list directly in the Mail-Step.

As a workaround a predefined number of attachments can be added while using a simple macro. The macro gets the corresponding entry from the list, if it exists, or it returns an empty string.

  • The macro for the first line is: <%=(in.files.size() > 0) ? in.files.get(0) : ""%>
  • The macro for the second line is: <%=(in.files.size() > 1) ? in.files.get(1) : ""%>

The configuration of the E-Mail Step could look like this print screen:

E-Mail Step configuration with nearly variable list of attachments

link

answered 26.01.2015 at 09:56

Flavio%20Sadeghi's gravatar image

Flavio Sadeghi ♦♦
1.8k5723
accept rate: 75%

Yes, it works, but produces warnings for each line where the makro results in an empty string: WARNING: Email attachment: <%=(in.files.size() > 3) ? in.files.get(3) : ""%> is skipped, because it resulted an empty resource name.

(27.01.2015 at 15:41) Peter Weber Peter%20Weber's gravatar image

You can send mails from a script step with this java class.

import java.io.File;
import java.util.Map;
import java.util.concurrent.Callable;

import org.eclipse.core.resources.IProject;

import ch.ivyteam.ivy.components.config.EmailConfiguration;
import ch.ivyteam.ivy.email.EmailSetupConfiguration;
import ch.ivyteam.ivy.email.EmailSetupProviderUtil;
import ch.ivyteam.ivy.email.SimpleMailSender;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.project.IIvyProject;
import ch.ivyteam.ivy.project.IvyProjectUtil;
import ch.ivyteam.ivy.workflow.WorkflowNavigationUtil;
import ch.ivyteam.log.Logger;
import ch.soreco.ivybpmelements.util.Admin;

/**
 * Class to send emails from java code.
 *
 */
@SuppressWarnings("restriction")
public class Mail {

  EmailConfiguration emailConfig;
  Map<String, File> attachmentFiles;

  public Mail
    (String subject, String from, String replyTo,
        String to, String cc, String bcc, String message,
        Map<String, File> attachmentFiles) throws Exception {
      this.emailConfig = new EmailConfiguration();
      emailConfig.setSubject(subject);
      emailConfig.setFrom(from);
      emailConfig.setReplyTo(replyTo);
      emailConfig.setTo(to);
      emailConfig.setCC(cc);
      emailConfig.setBCC(bcc);
      emailConfig.setMessageContent(message);

      this.attachmentFiles = attachmentFiles;
  }

  public Mail(EmailConfiguration emailConfig, Map<String, File> attachmentFiles) throws Exception {
    send(emailConfig, attachmentFiles);
  }

  public void send() throws Exception {
    send(emailConfig, attachmentFiles);
  }

  private void send(final EmailConfiguration emailConfig,
      final Map<String, File> attachmentFiles) throws Exception {

    (new Admin<Void>())
        .execute(new Callable<Void>() {

          @Override
          public Void call() throws Exception {
            IProject project = WorkflowNavigationUtil
                .getWorkflowProcessModelVersion(
                    Ivy.request().getProcessModelVersion())
                .getProject();
            IIvyProject ivyProject = IvyProjectUtil
                .getIvyProjectByName(project.getName());
            EmailSetupConfiguration mailSetup = EmailSetupProviderUtil
                .getEmailSenderConfiguration(ivyProject);
            Logger runtimeLogger = Ivy.log();
            SimpleMailSender mailSender = new SimpleMailSender(attachmentFiles,
                emailConfig, runtimeLogger, mailSetup);
            mailSender.sendMessage();
            return null;
          }
        });
  }

}

And to execute the code as system user.

import java.util.concurrent.Callable;

import ch.ivyteam.ivy.server.ServerFactory;

/**
 * Class to execute code as the ivy system user.
 *
 * @param <T>
 */
public class Admin<T> {

  /**
   * Exeute code as system user.
   * @param callable
   * @return
   * @throws Exception
   */
  public T execute(Callable<T> callable) throws Exception {
    return ServerFactory.getServer().getSecurityManager().executeAsSystem(callable);
  }

}
link

answered 11.03.2015 at 16:07

HaraldWeber's gravatar image

HaraldWeber
101101015
accept rate: 33%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15

Asked: 23.01.2015 at 15:01

Seen: 1,142 times

Last updated: 11.03.2015 at 16:07