I'd like to send a mail with embedded attachments. The mail content must contain pictures, but they should not be shown as attachments by a mail client.

asked 08.04.2016 at 09:21

SupportIvyTeam's gravatar image

SupportIvyTeam ♦♦
1.4k102118122
accept rate: 77%


This is possible if you send the mail by using internal API instead of the Mail-Activity. alt text

I've created a demo project: embeddedAttachmentMailer_51.iar

Be aware that this internal API will change in the future without any further notice. The used JavaBean could look as follows:

package ch.ivyteam.demo;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
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.security.SecurityManagerFactory;

@SuppressWarnings("restriction")
public class JavaMailer {

    private static final String AXON_LOGO = "axonLogo";

    public static void send() throws Exception
    {
        File myImage = getCmsImageFile("/Project/Banner");
        Map<String, File> attachments = new HashMap<>();
        attachments.put(AXON_LOGO, myImage);

        EmailConfiguration mail = new EmailConfiguration();
        mail.setFrom("developer@axonivy.com");
        mail.setTo("manager@axonivy.com");
        mail.setSubject("hi from script");
        mail.setMessageContent("<html>hi, i'm a cool mail with embedded attachments! <img src=\"cid:"+AXON_LOGO+"\"></html>");
        mail.setAttachments(attachments.keySet().toArray(new String[attachments.size()]));

        new SimpleMailSender(attachments, mail, Ivy.log(), mailSetup()).sendMessage();
    }

    private static File getCmsImageFile(String coUri) throws IOException {
        File myImage = Files.createTempFile("mailImage", ".png").toFile();
        Ivy.cms().getContentObject(coUri).getValues().get(0)
            .exportContentToFile(myImage, null);
        return myImage;
    }

    /**
     * @return the mail configuration: designer+engine aware, so that mail sending with test-preferences is possible!
     * @throws Exception 
     */
    private static EmailSetupConfiguration mailSetup() throws Exception
    {
        return SecurityManagerFactory.getSecurityManager().executeAsSystem(
          new Callable<EmailSetupConfiguration>() {
            @Override
            public EmailSetupConfiguration call() throws Exception {
                IProject project = Ivy.request().getProcessModelVersion().getProject();
                IIvyProject ivyProject = (IIvyProject)project.getAdapter(IIvyProject.class);
                return EmailSetupProviderUtil.getEmailSenderConfiguration(ivyProject);
            }
        });
    }

//  /**
//   * @param mail
//   * @return converts {@link EmailConfiguration} from 5.1 and older to {@link EMailConfig} from 6.0 and newer
//   */
//  private static EMailConfig make60ApiCompatible(EmailConfiguration mail) 
//  {
//      return new BackgroundOperationLegacyService()
//        .convertMailAnythingAspect(mail.getAnythingContainer());
//  }

}
link

answered 08.04.2016 at 09:26

Reguel%20Wermelinger's gravatar image

Reguel Werme... ♦♦
9.4k31958
accept rate: 70%

edited 07.03.2018 at 10:37

Alex%20Suter's gravatar image

Alex Suter ♦♦
3.1k122247

Hi, I get your demo project. Mail-Activity always sends logo as attachment. For second solution, Using external API but SimpleMailSender constructor requires EmailConfig instead of EmailConfiguration..

new SimpleMailSender(attachments, mail, Ivy.log(), mailSetup()).sendMessage();

Maybe the API has changed? Any solution suggest to me?

link

answered 24.10.2017 at 07:08

chinhdq's gravatar image

chinhdq
(suspended)
accept rate: 0%

edited 24.10.2017 at 23:02

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:

×34

Asked: 08.04.2016 at 09:21

Seen: 3,416 times

Last updated: 07.03.2018 at 10:37