View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email transport capabilities. 4 * @Author Vladan Obradovic 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 javax.mail.Transport; 14 import java.io.FileInputStream; 15 import java.io.File; 16 17 18 /*** 19 * Tests signing process. Signed text/html message with or 20 * withouth attachments can be sent by this test. This example is test for 21 * composing of email message content by html code which is given from the file 22 * system. To get help for this example type: 23 * "java org.webdocwf.util.smime.test.TestSignedHtml" 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> <digestAlgorithm> <includingCert> 28 * <includingSignAttrib> <pfxFileName> <externalSigning> 29 * [<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 * <externalSigning> could be: true/false<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 TestSignedHtml.java in order 40 * to use them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords. 41 */ 42 public class TestSignedHtml { 43 44 public static void main(String[] args) { 45 String subject = "S/MIME signed message - Subject test: ÜüÄäÖöÜüß"; 46 String from = "sender@seateam.co.yu"; 47 String password = "sea1"; 48 49 if (args.length < 7) { 50 System.err.println( 51 System.getProperty("line.separator") + 52 "Usage of TestSignedHtml: " + 53 System.getProperty("line.separator") + 54 "java TestSignedHtml <mailHost> <mailAddress> <digestAlgorithm> " + 55 "<includingCert> <includingSignAttrib> <pfxFileName> <externalSigning>" + 56 "[<attachment>]" + 57 System.getProperty("line.separator") + 58 System.getProperty("line.separator") + 59 "Examples:" + 60 System.getProperty("line.separator") + 61 "java TestSignedHtml seateam.co.yu recipient@seateam.co.yu SHA1_WITH_RSA true " + 62 "true sender512.pfx true" + 63 System.getProperty("line.separator") + 64 "java TestSignedHtml seateam.co.yu recipient@seateam.co.yu SHA1_WITH_DSA true " + 65 "true senderDSA512.pfx false .//test//Zip8Test.zip"); 66 System.exit(-1); 67 } 68 69 String smtpHost = args[0]; 70 String addressTO = args[1]; 71 String digestAlgorithm = args[2]; 72 73 boolean includingCert = true; 74 75 if (args[3].equalsIgnoreCase("true")) 76 includingCert = true; 77 else 78 includingCert = false; 79 80 boolean includingSignAttrib = true; 81 82 if (args[4].equalsIgnoreCase("true")) 83 includingSignAttrib = true; 84 else 85 includingSignAttrib = false; 86 87 String pfxfileName = args[5]; 88 89 boolean externalSigning = true; 90 91 if (args[6].equalsIgnoreCase("true")) 92 externalSigning = true; 93 else 94 externalSigning = false; 95 96 String fileName = null; 97 98 if (args.length > 7) 99 fileName = args[7]; 100 101 String addressCC = "recipient@seateam.co.yu"; 102 String addressBCC = "recipient@seateam.co.yu"; 103 104 subject = digestAlgorithm + " " + args[3] + " " + args[4] + " " + 105 pfxfileName + " " + args[6] + " " + subject; 106 107 File htmlContent = new File("./test/HtmlTest.html"); 108 109 SignedSMIME ss = null; 110 111 try { 112 113 ss = new SignedSMIME(smtpHost, from, subject); 114 ss.setContent(htmlContent, "text/html"); 115 // To send messages content withouth resources from FileInputStream (or any 116 // other input stream) comment previous line and use following commented line 117 // ss.setContent(new FileInputStream(htmlContent),"text/html"); 118 119 if (fileName != null) { 120 ss.addAttachment(fileName); // optional - use this if send attachment 121 } 122 123 ss.setReply(from); // optional 124 125 ss.addRecipient(addressTO, "TO"); // mandatory 126 // ss.addRecipient(addressCC, "CC"); // optional 127 // ss.addRecipient(addressBCC, "BCC"); // optional 128 129 ss.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional 130 ss.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional 131 ss.setCapabilities("SIGNATURE", 1, 2, 3, 4, 0); // optional 132 133 ss.addSigner(pfxfileName, password, digestAlgorithm, 134 includingCert, includingSignAttrib); // mandatory 135 136 System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... "); 137 ss.signing(externalSigning); 138 System.out.print("Sending signed message ... "); 139 ss.send(); // instead of this next line could be used 140 // Transport.send(es.getSignedMessage()); 141 System.out.println("done."); 142 } catch (Exception e) { 143 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging 144 if (e instanceof SMIMEException) { 145 // ((SMIMEException)e).loggingErrors(null); //logging 146 ((SMIMEException) e).displayErrors(null); 147 } else 148 System.out.println(e.getMessage()); 149 } 150 } 151 }

This page was automatically generated by Maven