<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Answers to: Can we send Encrypted Emails in ivy?</title><link>https://answers.axonivy.com/questions/2166/can-we-send-encrypted-emails-in-ivy</link><description>&lt;p&gt;Hi all
The question is the same with the title. i would like to encrypt email before sending, i've tried with jana mail but just want to ask whether we have any support from ivy mail or not?&lt;/p&gt;
&lt;p&gt;Reference: &lt;a href="http://stackoverflow.com/questions/300334/how-to-send-encrypted-emails-programmatically-from-an-automated-process"&gt;How to Send Encrypted Emails Programmatically (from an automated process)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;</description><atom:link href="https://answers.axonivy.com/questions/2166/can-we-send-encrypted-emails-in-ivy" rel="self"></atom:link><language>en</language><lastBuildDate>Fri, 18 Nov 2016 09:23:29 -0500</lastBuildDate><item><title>Answer by Reguel Wermelinger</title><link>https://answers.axonivy.com/questions/2166/can-we-send-encrypted-emails-in-ivy/2195</link><description>&lt;p&gt;Another improvement: I saw that you have hardcoded host names and smtp users in your demo. I'd stick to the configuration that is provided by the Axon.ivy Designer preferences or the Engines System Properties. You can easily re-use these configuration by using an internal API&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;@SuppressWarnings("restriction")
private Session prepareSession() {
    ch.ivyteam.ivy.email.EmailSetupConfiguration emailConfig = 
            ch.ivyteam.ivy.email.EmailSetupProviderUtil.getEmailSenderConfiguration(Ivy.request().getProject());

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", emailConfig.getSmtpServer());
    props.put("mail.smtp.port", emailConfig.getSmtpPort());

    props.put("mail.smtp.connectiontimeout", 2000);

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailConfig.getSmtpUser(), emailConfig.getSmtpPassword());
                }
            });
    return session;
}
&lt;/code&gt;&lt;/pre&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Reguel Wermelinger</dc:creator><pubDate>Fri, 18 Nov 2016 09:23:29 -0500</pubDate><guid>https://answers.axonivy.com/questions/2166/can-we-send-encrypted-emails-in-ivy/2195</guid></item><item><title>Answer by trungdv</title><link>https://answers.axonivy.com/questions/2166/can-we-send-encrypted-emails-in-ivy/2193</link><description>&lt;p&gt;hi ivyteam, &lt;/p&gt;
&lt;p&gt;i've tried with the third party library is &lt;em&gt;bouncycastle&lt;/em&gt; and &lt;em&gt;java.mails&lt;/em&gt; , it's seem to work and i post implementation here&lt;/p&gt;
&lt;p&gt;Function to encrypt message:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;public MimeMessage encrypt(MimeMessage clearMessage, Session session) {

    MimeMessage encryptedMessage = null;
    try {
        MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap
                .getDefaultCommandMap();

        mailcap.addMailcap("application/pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_signature");
        mailcap.addMailcap("application/pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.pkcs7_mime");
        mailcap.addMailcap("application/x-pkcs7-signature;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_signature");
        mailcap.addMailcap("application/x-pkcs7-mime;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.x_pkcs7_mime");
        mailcap.addMailcap("multipart/signed;; x-java-content-handler=org.bouncycastle.mail.smime.handlers.multipart_signed");

        CommandMap.setDefaultCommandMap(mailcap);
        /* Add BC */
        Security.addProvider(new BouncyCastleProvider());

        /* Open the keystore */
        KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
        keystore.load(new FileInputStream(pathP12File),
                p12FilePassword.toCharArray());

        // get key alias
        Enumeration e = keystore.aliases();
        String keyAlias = null;
        //
        while (e.hasMoreElements()) {
            String alias = (String) e.nextElement();

            if (keystore.isKeyEntry(alias)) {
                keyAlias = alias;
            }
        }

        if (keyAlias == null) {
            Ivy.log().error("can't find a private key!");
            return null;
        }

        // end getting key
        Certificate[] chain = keystore.getCertificateChain(keyAlias);

        /* Create the encrypter */
        SMIMEEnvelopedGenerator encrypter = new SMIMEEnvelopedGenerator();
        encrypter
                .addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(
                        (X509Certificate) chain[0]).setProvider("BC"));

        /* Encrypt the message */
        MimeBodyPart encryptedPart = encrypter.generate(clearMessage,
                new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC)
                        .setProvider("BC").build());

        /*
         * Create a new MimeMessage that contains the encrypted and signed
         * content
         */
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            encryptedPart.writeTo(out);
        } catch (IOException e1) {
            Ivy.log().error("EncryptMEssage: " + e1.getMessage(), e1);
        }
        encryptedMessage = new MimeMessage(session,
                new ByteArrayInputStream(out.toByteArray()));
        /* Set all original MIME headers in the encrypted message */
        Enumeration headers = clearMessage.getAllHeaderLines();
        while (headers.hasMoreElements()) {
            String headerLine = (String) headers.nextElement();
            if (!Strings.toLowerCase(headerLine).startsWith("content-")) {
                encryptedMessage.addHeaderLine(headerLine);
            }
        }
    } catch (SMIMEException | MessagingException | CMSException
            | IllegalArgumentException | KeyStoreException
            | NoSuchAlgorithmException | CertificateException | IOException
            | NoSuchProviderException ex) {
        Ivy.log().error("encrypt method:" + ex.getMessage());
    }
    return encryptedMessage;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And Sendmail method:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;                    SignMessageService signMessageService = new SignMessageService();
        EncryptMessageService encryptMessageService = new EncryptMessageService();

        MimeMessage signedMessage = signMessageService.sign(clearMessage,
                session);
        MimeMessage encryptedMessage = encryptMessageService.encrypt(
                signedMessage, session);
        Ivy.log().info("send mail");
        Transport.send(encryptedMessage);
        Ivy.log().info("DONE");
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can get the full sourcecode from this &lt;a href="https://drive.google.com/file/d/0B6qldkZvvHgHb3M4RDNwZFE5YkU/view?usp=sharing"&gt;Here&lt;/a&gt; (follow the ReadMe.txt to import project) . In that project I implement demo flow with using normal process as well using Signal (experiment)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">trungdv</dc:creator><pubDate>Fri, 18 Nov 2016 08:25:43 -0500</pubDate><guid>https://answers.axonivy.com/questions/2166/can-we-send-encrypted-emails-in-ivy/2193</guid></item></channel></rss>