View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email sending capabilities 4 * @Author Vladimir Radisic 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.File; 15 import java.io.ByteArrayInputStream; 16 import java.io.InputStream; 17 18 19 /*** 20 * Tests signing and enveloping process. Signed and enveloped 21 * text/html message with attachments can be sent by this test. This example 22 * is test for composing of email message content by html code which is generated 23 * by application, and adding resources assocated in html code from the 24 * application, and from the file system. Also, attachments are added to message 25 * from the application, or from file system. To get help for this example type: 26 * "java org.webdocwf.util.smime.test.TestSigEncGeneratedHtml" in command line. 27 * It is assumed that smime200tests.jar is in your classpath.<BR> 28 * <BR> 29 * Parameters passed to example are:<BR> 30 * <mailHost> <mailAddress> <cerFileName> <algorithmName> 31 * <digestAlgorithm> <includingCert> <includingSignAttrib> 32 * <pfxFileName> [<attachment>] <BR> 33 * <BR> 34 * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR> 35 * <includingCert> could be: true/false<BR> 36 * <includingSignAttrib> could be: true/false<BR> 37 * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR> 38 * <BR> 39 * Note that for this example passwords for .pfx or .p12 files are fixed to 40 * "sea1". All .pfx files or .p12 files provided with this example have this 41 * password. Also, email address "FROM" is fixed to: "sender@seateam.co.yu". 42 * You should change this values in source code of TestSigEncGeneratedHtml.java 43 * in order to use them with other .pfx or .p12 files and corresponding "FROM" 44 * addresses and passwords. 45 */ 46 public class TestSigEncGeneratedHtml { 47 48 public static void main(String[] args) { 49 50 String subject = "S/MIME signed and enveloped message - Subject test: ÜüÄäÖöÜüß"; 51 String content = "S/MIME signed and enveloped message example\r\nContent test: ÜüÄäÖöÜüß!"; 52 String from = "sender@seateam.co.yu"; 53 String password = "sea1"; 54 55 if (args.length < 8) { 56 System.err.println( 57 System.getProperty("line.separator") + 58 "Usage of TestSigEncGeneratedHtml: " + 59 System.getProperty("line.separator") + 60 "java TestSigEncGeneratedHtml <mailHost> <mailAddress> <cerFileName> " + 61 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 62 "<pfxFileName> [<attachment>]" + 63 System.getProperty("line.separator") + 64 System.getProperty("line.separator") + 65 "Examples:" + 66 System.getProperty("line.separator") + 67 "java TestSigEncGeneratedHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 68 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 69 System.getProperty("line.separator") + 70 "java TestSigEncGeneratedHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 71 "DES MD5_WITH_RSA true true sender512.pfx .//test//Zip8Test.zip"); 72 System.exit(-1); 73 74 System.exit(-1); 75 } 76 77 String smtpHost = args[0]; 78 String addressTO = args[1]; 79 String cerFileName = args[2]; 80 String algorithmName = args[3]; 81 String digestAlgorithm = args[4]; 82 83 boolean includingCert = true; 84 85 if (args[5].equals("true")) 86 includingCert = true; 87 else 88 includingCert = false; 89 90 boolean includingSignAttrib = true; 91 92 if (args[6].equals("true")) 93 includingSignAttrib = true; 94 else 95 includingSignAttrib = false; 96 97 String pfxfileName = args[7]; 98 99 String fileName = null; 100 101 if (args.length > 8) 102 fileName = args[8]; 103 104 String addressCC = "recipient@seateam.co.yu"; 105 String addressBCC = "recipient@seateam.co.yu"; 106 107 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 108 args[6] + " " + args[7] + " " + subject; 109 110 SignedAndEnvelopedSMIME see = null; 111 112 try { 113 ExampleGenerator generator = new ExampleGenerator(); 114 115 // Address for resource green.gif which will be obtained by program generation 116 // from instances of the ExampleGenerator class as InputSteam must be added to 117 // html code as virtual. For more information refer to setContent method of 118 // SignedSMIME class. 119 String green = "*****000generated.gif"; 120 121 // Address for resource red.gif class will be defined as absolute address to 122 // location on the file system. 123 File redFile = new File("./test/pictures/red.gif"); 124 String red = redFile.getAbsoluteFile().getCanonicalPath(); 125 126 // Address for resource blue.gif class will be defined as file type of url 127 // address. 128 File blueFile = new File("./test/pictures/blue.gif"); 129 String blue = "file:///" + blueFile.getAbsoluteFile().getCanonicalPath(); 130 131 blue = blue.replace('//', '/'); 132 133 // Addresses for resources orange.jpg and yellow.jpg are left relative. 134 // Default values of resources in ExampleGenerator are relative, as it can be 135 // seen in the original file of html example HtmlTest.html. To resolve this 136 // relative adresses common directory path must be obtained from the root. 137 // For more information refer to setContent method of 138 // SignedSMIME class. 139 String commonPath = new File("./test/HtmlTest.html").getParent(); 140 141 generator.setResourceInExampleHtml("GREEN", green); 142 generator.setResourceInExampleHtml("RED", red); 143 generator.setResourceInExampleHtml("BLUE", blue); 144 145 // Simulation of resources (green.gif) obtained by program. 146 InputStream[] in = {generator.getGifImage()}; 147 148 // Generation of signed smime object 149 see = new SignedAndEnvelopedSMIME(smtpHost, from, subject); 150 // es.setContent( generator.getHtmlStream(), "text/html", commonPath, in ); 151 see.setContent(generator.getHtmlString(), "text/html", commonPath, in); 152 153 // Attachments can be added to message from program (using Input stream). In 154 // the following line zip file will be generated and added as attachment. You must 155 // define some file names in order to give appropriate mime-type to your message. 156 see.addAttachment(generator.getZipStream(), "test.zip"); 157 158 // File can also be added from file system 159 File attachment2 = new File("./test/AdobeAcrobatTest.pdf"); 160 161 see.addAttachment(attachment2); 162 File attachment3 = new File("./test/Word2000Test.doc"); 163 164 see.addAttachment(attachment3); 165 166 if (fileName != null) { 167 see.addAttachment(fileName); // optional - use this if send attachment 168 } 169 170 see.setReply(from); // optional 171 172 see.addRecipient(addressTO, "TO", cerFileName); // mandatory 173 // see.addRecipient(addressCC, "CC", cerFileName); // optional 174 // see.addRecipient(addressBCC, "BCC", cerFileName); // optional 175 176 see.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional 177 see.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional 178 see.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional 179 180 see.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib); 181 182 if (algorithmName.equals("RC240")) { 183 System.out.println("Creating signed and encrypted message with RC2 - 40 bits algorithm... "); 184 see.signingAndEnveloping("SIGN_FIRST"); // instead of this next line could be used 185 // see.enveloping("RC2_CBC", 40, "SIGN_FIRST"); 186 } else if (algorithmName.equals("RC264")) { 187 System.out.println("Creating signed and encrypted message with RC2 - 64 bits algorithm... "); 188 see.signingAndEnveloping("RC2_CBC", 64, "SIGN_FIRST"); // send message with RC2 - 64 bits algorithm 189 } else if (algorithmName.equals("RC2128")) { 190 System.out.println("Creating signed and encrypted message with RC2 - 128 bits algorithm... "); 191 see.signingAndEnveloping("RC2_CBC", 128, "SIGN_FIRST"); // send message with RC2 - 128 bits algorithm 192 } else if (algorithmName.equals("DES")) { 193 System.out.println("Creating signed and encrypted message with DES algorithm... "); 194 see.signingAndEnveloping("DES", 56, "SIGN_FIRST"); // send message with DES - 56 bits algorithm 195 } else if (algorithmName.equals("3DES")) { 196 System.out.println("Creating signed and encrypted message with 3DES algorithm... "); 197 see.signingAndEnveloping("DES_EDE3_CBC", 192, "SIGN_FIRST"); // send message with 3DES - 192 bits algorithm 198 } 199 200 System.out.print("Sending signed and encrypted message ... "); 201 see.send(); // instead of this next line could be used 202 // Transport.send(see.getSignedMessage()); 203 System.out.println("done."); 204 205 } catch (Exception e) { 206 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging 207 if (e instanceof SMIMEException) { 208 // ((SMIMEException)e).loggingErrors(null); //logging 209 ((SMIMEException) e).displayErrors(null); 210 } else 211 System.out.println(e.getMessage()); 212 } 213 } 214 }

This page was automatically generated by Maven