<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: How to send a variable number of attachments in E-Mail Step</title><link>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step</link><description>&lt;p&gt;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?&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step" rel="self"></atom:link><language>en</language><lastBuildDate>Wed, 11 Mar 2015 16:07:56 -0400</lastBuildDate><item><title>Answer by HaraldWeber</title><link>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step/1222</link><description>&lt;p&gt;You can send mails from a script step with this java class.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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&amp;lt;String, File&amp;gt; attachmentFiles;

  public Mail
    (String subject, String from, String replyTo,
        String to, String cc, String bcc, String message,
        Map&amp;lt;String, File&amp;gt; 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&amp;lt;String, File&amp;gt; attachmentFiles) throws Exception {
    send(emailConfig, attachmentFiles);
  }

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

  private void send(final EmailConfiguration emailConfig,
      final Map&amp;lt;String, File&amp;gt; attachmentFiles) throws Exception {

    (new Admin&amp;lt;Void&amp;gt;())
        .execute(new Callable&amp;lt;Void&amp;gt;() {

          @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;
          }
        });
  }

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And to execute the code as system user.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import java.util.concurrent.Callable;

import ch.ivyteam.ivy.server.ServerFactory;

/**
 * Class to execute code as the ivy system user.
 *
 * @param &amp;lt;T&amp;gt;
 */
public class Admin&amp;lt;T&amp;gt; {

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

}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">HaraldWeber</dc:creator><pubDate>Wed, 11 Mar 2015 16:07:56 -0400</pubDate><guid>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step/1222</guid></item><item><title>Comment by Peter Weber on Flavio Sadeghi's answer</title><link>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step#1137</link><description>&lt;p&gt;Yes, it works, but produces warnings for each line where the makro results in an empty string:
WARNING: Email attachment: &amp;lt;%=(in.files.size() &amp;gt; 3) ? in.files.get(3) : ""%&amp;gt; is skipped, because it resulted an empty resource name.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Peter Weber</dc:creator><pubDate>Tue, 27 Jan 2015 15:41:27 -0500</pubDate><guid>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step#1137</guid></item><item><title>Answer by Flavio Sadeghi</title><link>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step/1131</link><description>&lt;p&gt;Currently it is not possible to define a list directly in the Mail-Step. &lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The macro for the first line is: &lt;code&gt;&amp;lt;%=(in.files.size() &amp;gt; 0) ? in.files.get(0) : ""%&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The macro for the second line is: &lt;code&gt;&amp;lt;%=(in.files.size() &amp;gt; 1) ? in.files.get(1) : ""%&amp;gt;&lt;/code&gt;
…&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The configuration of the E-Mail Step could look like this print screen:&lt;/p&gt;
&lt;p&gt;&lt;img alt="E-Mail Step configuration with nearly variable list of attachments" src="http://answers.axonivy.com/upfiles/mailAttachmentsFromList.png"&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Flavio Sadeghi</dc:creator><pubDate>Mon, 26 Jan 2015 09:56:54 -0500</pubDate><guid>https://answers.axonivy.com/questions/1130/how-to-send-a-variable-number-of-attachments-in-e-mail-step/1131</guid></item></channel></rss>