View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email sending capabilities 4 * @Author Vladan Obradovic 5 * @Version 2.0.1 6 */ 7 8 package org.webdocwf.util.smime.test; 9 10 11 import javax.mail.Transport; 12 import org.webdocwf.util.smime.smime.SignedAndEnvelopedSMIME; 13 import org.webdocwf.util.smime.exception.SMIMEException; 14 import java.io.FileInputStream; 15 import java.io.File; 16 17 18 /*** 19 * Tests enveloping and signing process. Enveloped and signed 20 * text/html message with or withouth attachments can be sent by this test. 21 * This example is test for composing of email message content by html code which 22 * is given from the file system. To get help for this example type: 23 * "java org.webdocwf.util.smime.test.TestEncSigHtml" in command line. 24 * It is assumed that smime200tests.jar is in your classpath.<BR> 25 * <BR> 26 * Parameters passed to example are:<BR> 27 * <mailHost> <mailAddress> <cerFileName> <algorithmName> 28 * <digestAlgorithm> <includingCert> <includingSignAttrib> 29 * <pfxFileName> [<attachment>] <BR> 30 * <BR> 31 * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR> 32 * <includingCert> could be: true/false<BR> 33 * <includingSignAttrib> could be: true/false<BR> 34 * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR> 35 * <BR> 36 * Note that for this example passwords for .pfx or .p12 files are fixed to 37 * "sea1". All .pfx files or .p12 files provided with this example have this 38 * password. Also, email address "FROM" is fixed to: "sender@seateam.co.yu". 39 * You should change this values in source code of TestEncSigHtml.java in order 40 * to use them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords. 41 */ 42 public class TestEncSigHtml { 43 44 public static void main(String[] args) { 45 46 String subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß"; 47 String from = "sender@seateam.co.yu"; 48 String password = "sea1"; 49 50 if (args.length < 8) { 51 System.err.println( 52 System.getProperty("line.separator") + 53 "Usage of TestEncSigHtml: " + 54 System.getProperty("line.separator") + 55 "java TestEncSigHtml <mailHost> <mailAddress> <cerFileName> " + 56 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 57 "<pfxFileName> [<attachment>]" + 58 System.getProperty("line.separator") + 59 System.getProperty("line.separator") + 60 "Examples:" + 61 System.getProperty("line.separator") + 62 "java TestEncSigHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 63 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 64 System.getProperty("line.separator") + 65 "java TestEncSigHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 66 "DES MD5_WITH_RSA true true sender512.pfx .//test//Zip8Test.zip"); 67 68 System.exit(-1); 69 } 70 71 String smtpHost = args[0]; 72 String addressTO = args[1]; 73 String cerFileName = args[2]; 74 String algorithmName = args[3]; 75 String digestAlgorithm = args[4]; 76 77 boolean includingCert = true; 78 79 if (args[5].equals("true")) 80 includingCert = true; 81 else 82 includingCert = false; 83 84 boolean includingSignAttrib = true; 85 86 if (args[6].equals("true")) 87 includingSignAttrib = true; 88 else 89 includingSignAttrib = false; 90 91 String pfxfileName = args[7]; 92 93 String fileName = null; 94 95 if (args.length > 8) 96 fileName = args[8]; 97 98 String addressCC = "recipient@seateam.co.yu"; 99 String addressBCC = "recipient@seateam.co.yu"; 100 101 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 102 args[6] + " " + args[7] + " " + subject; 103 104 File htmlContent = new File("./test/HtmlTest.html"); 105 106 SignedAndEnvelopedSMIME ess = null; 107 108 try { 109 ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject); 110 111 ess.setContent(htmlContent, "text/html"); 112 // To send messages content withouth resources from FileInputStream (or any 113 // other input stream) comment previous line and use following commented line 114 // ess.setContent(new FileInputStream(htmlContent),"text/html"); 115 116 if (fileName != null) { 117 ess.addAttachment(fileName); // optional - use this if send attachment 118 } 119 120 ess.setReply(from); // optional 121 122 ess.addRecipient(addressTO, "TO", cerFileName); // mandatory 123 // ess.addRecipient(addressCC, "CC", cerFileName); // optional 124 // ess.addRecipient(addressBCC, "BCC", cerFileName); // optional 125 126 ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional 127 ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional 128 ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional 129 130 ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib); 131 132 if (algorithmName.equals("RC240")) { 133 System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... "); 134 ess.signingAndEnveloping("ENCRYPT_FIRST"); // instead of this next line could be used 135 // ess.enveloping("RC2_CBC", 40, "ENCRYPT_FIRST"); 136 } else if (algorithmName.equals("RC264")) { 137 System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... "); 138 ess.signingAndEnveloping("RC2_CBC", 64, "ENCRYPT_FIRST"); // send message with RC2 - 64 bits algorithm 139 } else if (algorithmName.equals("RC2128")) { 140 System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... "); 141 ess.signingAndEnveloping("RC2_CBC", 128, "ENCRYPT_FIRST"); // send message with RC2 - 128 bits algorithm 142 } else if (algorithmName.equals("DES")) { 143 System.out.println("Creating enveloped and signed message with DES algorithm... "); 144 ess.signingAndEnveloping("DES", 56, "ENCRYPT_FIRST"); // send message with DES - 56 bits algorithm 145 } else if (algorithmName.equals("3DES")) { 146 System.out.println("Creating enveloped and signed message with 3DES algorithm... "); 147 ess.signingAndEnveloping("DES_EDE3_CBC", 192, "ENCRYPT_FIRST"); // send message with 3DES - 192 bits algorithm 148 } 149 150 System.out.print("Sending enveloped and signed message ... "); 151 ess.send(); // instead of this next line could be used 152 // Transport.send(ess.getSignedMessage()); 153 System.out.println("done."); 154 155 } catch (Exception e) { 156 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging 157 if (e instanceof SMIMEException) { 158 // ((SMIMEException)e).loggingErrors(null); //logging 159 ((SMIMEException) e).displayErrors(null); 160 } else 161 System.out.println(e.getMessage()); 162 } 163 } 164 }

This page was automatically generated by Maven