1 7 8 package org.enhydra.oyster.test; 9 10 import javax.mail.Transport ; 11 import javax.mail.internet.MimeBodyPart ; 12 import javax.mail.internet.MimeMessage ; 13 import javax.mail.internet.MimeMultipart ; 14 import javax.mail.internet.InternetAddress ; 15 import javax.mail.Session ; 16 import org.enhydra.oyster.smime.SignedAndEnvelopedSMIME; 17 import org.enhydra.oyster.exception.SMIMEException; 18 import javax.activation.DataHandler ; 19 import javax.activation.FileDataSource ; 20 import java.util.Properties ; 21 import java.io.File ; 22 import java.security.Security ; 23 24 47 public class TestExternalMessage 48 { 49 50 public static void main(String [] args) 51 { 52 53 String subject = "S/MIME signed and enveloped message - Subject test: ÜüÄäÖöÜüß"; 54 String content = "S/MIME signed and enveloped message example\r\nContent test: ÜüÄäÖöÜüß!"; 55 String from = "sender@together.at"; 56 String password = "together"; 57 58 if (args.length < 8) 59 { 60 System.err.println( 61 System.getProperty("line.separator") + 62 "Usage of TestExternalMessage: " + 63 System.getProperty("line.separator") + 64 "java TestExternalMessage <mailHost> <mailAddress> <cerFileName> " + 65 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 66 "<pfxFileName> [<attachment>]"+ 67 System.getProperty("line.separator") + 68 System.getProperty("line.separator") + 69 "Examples:" + 70 System.getProperty("line.separator") + 71 "java TestEncSig together.at recipient@together.at recipient512.cer " + 72 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 73 System.getProperty("line.separator") + 74 "java TestEncSig together.at recipient@together.at recipient512.cer " + 75 "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip"); 76 77 System.exit(-1); 78 } 79 80 String smtpHost = args[0]; 81 String addressTO = args[1]; 82 String cerFileName = args[2]; 83 String algorithmName = args[3]; 84 String digestAlgorithm = args[4]; 85 86 boolean includingCert = true; 87 if (args[5].equals("true")) 88 includingCert = true; 89 else 90 includingCert = false; 91 92 boolean includingSignAttrib = true; 93 if (args[6].equals("true")) 94 includingSignAttrib = true; 95 else 96 includingSignAttrib = false; 97 98 String pfxfileName = args[7]; 99 100 String fileName = null; 101 if (args.length > 8) 102 fileName = args[8]; 103 104 String addressCC = "recipient@together.at"; 105 String addressBCC = "recipient@together.at"; 106 107 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 108 args[6] + " " + args[7] + " " + subject; 109 110 111 try { 112 114 Properties sesProp = new Properties (); 115 sesProp.setProperty("mail.smtp.host", smtpHost); 116 Session ses = Session.getInstance(sesProp); 117 MimeMessage message = new MimeMessage (ses); 118 InternetAddress fromAddress = new InternetAddress (from); 119 message.setFrom(fromAddress); 120 message.setSubject(subject); 121 122 MimeBodyPart mbpText = new MimeBodyPart (); 123 mbpText.setText(content); 124 125 if(fileName != null) { 126 MimeBodyPart mbpAttach = new MimeBodyPart (); 127 FileDataSource fileSource = new FileDataSource (fileName); 128 mbpAttach.setDataHandler(new DataHandler (fileSource)); 129 mbpAttach.setFileName(fileName); 130 mbpAttach.setDisposition(mbpAttach.ATTACHMENT); 131 132 MimeMultipart mm = new MimeMultipart (); 133 mm.addBodyPart(mbpText); 134 mm.addBodyPart(mbpAttach); 135 message.setContent(mm); 136 } 137 else { 138 message.setContent((String )mbpText.getContent(), mbpText.getContentType()); 139 message.setDisposition(mbpText.INLINE); 140 } 141 message.writeTo(System.out); 142 SignedAndEnvelopedSMIME ess = new SignedAndEnvelopedSMIME(message); 144 ess.addRecipient(addressTO, "TO", cerFileName); 148 ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); 152 ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib); 153 154 if (algorithmName.equals("RC240")) 155 { 156 System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... "); 157 ess.signingAndEnveloping("SIGN_FIRST"); } 160 else if (algorithmName.equals("RC264")) 161 { 162 System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... "); 163 ess.signingAndEnveloping(ess.RC2_CBC, 64, "SIGN_FIRST"); } 165 else if (algorithmName.equals("RC2128")) 166 { 167 System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... "); 168 ess.signingAndEnveloping(ess.RC2_CBC, 128, "SIGN_FIRST"); } 170 else if (algorithmName.equals("DES")) 171 { 172 System.out.println("Creating enveloped and signed message with DES algorithm... "); 173 ess.signingAndEnveloping(ess.DES, 56, "SIGN_FIRST"); } 175 else if (algorithmName.equals("3DES")) 176 { 177 System.out.println("Creating enveloped and signed message with 3DES algorithm... "); 178 ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "SIGN_FIRST"); } 180 181 System.out.print("Sending enveloped and signed message ... "); 182 ess.send(); System.out.println("done."); 185 186 } 187 catch (Exception e) { 188 SMIMEException.setErrorFilePath("Log"); if(e instanceof SMIMEException) { 190 SMIMEException eTmp = (SMIMEException)e; 191 eTmp.displayErrors(null); 193 e = eTmp.getNonSMIMEException(); 194 if(e != null) 195 e.printStackTrace(); 196 } 197 else { 198 e.printStackTrace(); 199 } 200 } 201 } 202 } | Popular Tags |