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

This page was automatically generated by Maven