<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Send mail with embedded attachments</title><link>https://answers.axonivy.com/questions/1721/send-mail-with-embedded-attachments</link><description>&lt;p&gt;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.&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/1721/send-mail-with-embedded-attachments" rel="self"></atom:link><language>en</language><lastBuildDate>Tue, 24 Oct 2017 07:08:32 -0400</lastBuildDate><item><title>Answer by chinhdq</title><link>https://answers.axonivy.com/questions/1721/send-mail-with-embedded-attachments/2927</link><description>&lt;p&gt;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..&lt;/p&gt;
&lt;p&gt;new SimpleMailSender(attachments, mail, Ivy.log(), mailSetup()).sendMessage();&lt;/p&gt;
&lt;p&gt;Maybe the API has changed? Any solution suggest to me?&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">chinhdq</dc:creator><pubDate>Tue, 24 Oct 2017 07:08:32 -0400</pubDate><guid>https://answers.axonivy.com/questions/1721/send-mail-with-embedded-attachments/2927</guid></item><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/1721/send-mail-with-embedded-attachments/1722</link><description>&lt;p&gt;This is possible if you send the mail by &lt;strong&gt;using internal API&lt;/strong&gt; instead of the Mail-Activity.
&lt;img alt="alt text" src="https://answers.axonivy.com/upfiles/mailWithNoVisibileAttachments.png"&gt;&lt;/p&gt;
&lt;p&gt;I've created a demo project: &lt;a href="/upfiles/embeddedAttachmentMailer_51.iar"&gt;embeddedAttachmentMailer_51.iar&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Be aware that this internal API will change in the future without any further notice. The used JavaBean could look as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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&amp;lt;String, File&amp;gt; attachments = new HashMap&amp;lt;&amp;gt;();
        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("&amp;lt;html&amp;gt;hi, i'm a cool mail with embedded attachments! &amp;lt;img src=\"cid:"+AXON_LOGO+"\"&amp;gt;&amp;lt;/html&amp;gt;");
        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&amp;lt;EmailSetupConfiguration&amp;gt;() {
            @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());
//  }

}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Fri, 08 Apr 2016 09:26:52 -0400</pubDate><guid>https://answers.axonivy.com/questions/1721/send-mail-with-embedded-attachments/1722</guid></item></channel></rss>