podepisovani multipart mailu

Tomas Vojtech tom.vojtech na seznam.cz
Čtvrtek Říjen 29 23:32:23 CET 2009


Ahoj,
snazim se podepsat a odeslat mail s prilohou. Kdyz odeslu mail bez 
prilohy, tak je vsechno v pohode, ale kdyz odeslu mail s prilohou tak mi 
Thunderbird rve, ze mail sice byl podepsany, ale signatura nesedi na 
obsah mailu. K podepisovani pouzivam javamail-crypto.

public void test() throws MessagingException {
        Properties props = new Properties();
        Session session = Session.getInstance(props);
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("test na seznam.cz"));
        msg.setRecipient(Message.RecipientType.TO, new 
InternetAddress("tom.vojtech na seznam.cz"));
        msg.setSubject("Testovaci podepsany mail");

        // create and fill the first message part
        MimeBodyPart mbp1 = new MimeBodyPart();
        mbp1.setText("Text testovaciho podepsaneho mailu");
        msg.setContent("Testovaci podepsany mail", "text/plain");

        // create the second message part
        MimeBodyPart mbp2 = new MimeBodyPart();

        // attach the file to the message
        FileDataSource fds = new FileDataSource(new 
File("oracle_prereq.sh"));
        mbp2.setDataHandler(new DataHandler(fds));
        mbp2.setFileName(fds.getName());

        // create the Multipart and add its parts to it
        Multipart mp = new MimeMultipart();
        mp.addBodyPart(mbp1);
        mp.addBodyPart(mbp2);

        // add the Multipart to the message
        msg.setContent(mp);

        // set the Date: header
        msg.setSentDate(new Date());
        msg.saveChanges();

        msg = signMessage(msg, session);

        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.server", null, null);

        transport.sendMessage(msg, msg.getAllRecipients());
    }

    protected MimeMessage signMessage(MimeMessage mimeMessage, Session 
session) throws MailException {
        try {
            // Getting of the S/MIME EncryptionUtilities.
            EncryptionUtils encUtils = 
EncryptionManager.getEncryptionUtils(EncryptionManager.SMIME);

            // Loading of the S/MIME keystore from the file (stored as 
resource).
            char[] keystorePass = getCertificatePassword().toCharArray();
            EncryptionKeyManager encKeyManager = 
encUtils.createKeyManager();
            
encKeyManager.loadPrivateKeystore(getClass().getResourceAsStream("aaa.p12"), 
keystorePass);

            // Getting of the S/MIME private key for signing.
            Key privateKey = 
encKeyManager.getPrivateKey(getPrivateKeyAlias(), keystorePass);

            // Signing the message.
            return encUtils.signMessage(session, mimeMessage, privateKey);
        } catch (Exception e) {
            throw new MailPreparationException(e);
        }
    }

    private String getPrivateKeyAlias() {
        return "alias";
    }

    private String getCertificatePassword() {
        return "password";
    }

Nevite v cem je problem?

Diky
TV



Další informace o konferenci Konference