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/plain message with or
20 * withouth attachments can be sent by this test. To get help for this
21 * example type: "java org.webdocwf.util.smime.test.TestSigned" in command line.
22 * It is assumed that smime200tests.jar is in your classpath.<BR>
23 * <BR>
24 * Parameters passed to example are:<BR>
25 * <mailHost> <mailAddress> <digestAlgorithm> <includingCert>
26 * <includingSignAttrib> <pfxFileName> <externalSigning>
27 * [<attachment>]<BR>
28 * <BR>
29 * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR>
30 * <includingCert> could be: true/false<BR>
31 * <includingSignAttrib> could be: true/false<BR>
32 * <externalSigning> could be: true/false<BR>
33 * <BR>
34 * Note that for this example passwords for .pfx or .p12 files are fixed to
35 * "sea1". All .pfx files or .p12 files provided with this example have this
36 * password. Also, email address "FROM" is fixed to: "sender@seateam.co.yu".
37 * You should change this values in source code of TestSigned.java in order to use
38 * them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords.
39 */
40 public class TestSigned {
41
42 public static void main(String[] args) {
43 String subject = "S/MIME signed message - Subject test: ÜüÄäÖöÜüß";
44 String content = "S/MIME signed message example\r\nContent test: ÜüÄäÖöÜüß!";
45 String from = "sender@seateam.co.yu";
46 String password = "sea1";
47
48 if (args.length < 7) {
49 System.err.println(
50 System.getProperty("line.separator") +
51 "Usage of TestSigned: " +
52 System.getProperty("line.separator") +
53 "java TestSigned <mailHost> <mailAddress> <digestAlgorithm> " +
54 "<includingCert> <includingSignAttrib> <pfxFileName> <externalSigning>" +
55 "[<attachment>]" +
56 System.getProperty("line.separator") +
57 System.getProperty("line.separator") +
58 "Examples:" +
59 System.getProperty("line.separator") +
60 "java TestSigned seateam.co.yu recipient@seateam.co.yu SHA1_WITH_RSA true " +
61 "true sender512.pfx true" +
62 System.getProperty("line.separator") +
63 "java TestSigned seateam.co.yu recipient@seateam.co.yu SHA1_WITH_DSA true " +
64 "true senderDSA512.pfx false .//test//Zip8Test.zip");
65 System.exit(-1);
66 }
67
68 String smtpHost = args[0];
69 String addressTO = args[1];
70 String digestAlgorithm = args[2];
71
72 boolean includingCert = true;
73
74 if (args[3].equalsIgnoreCase("true"))
75 includingCert = true;
76 else
77 includingCert = false;
78
79 boolean includingSignAttrib = true;
80
81 if (args[4].equalsIgnoreCase("true"))
82 includingSignAttrib = true;
83 else
84 includingSignAttrib = false;
85
86 String pfxfileName = args[5];
87
88 boolean externalSigning = true;
89
90 if (args[6].equalsIgnoreCase("true"))
91 externalSigning = true;
92 else
93 externalSigning = false;
94
95 String fileName = null;
96
97 if (args.length > 7)
98 fileName = args[7];
99
100 String addressCC = "recipient@seateam.co.yu";
101 String addressBCC = "recipient@seateam.co.yu";
102
103 subject = digestAlgorithm + " " + args[3] + " " + args[4] + " " +
104 pfxfileName + " " + args[6] + " " + subject;
105
106 SignedSMIME ss = null;
107
108 try {
109
110 ss = new SignedSMIME(smtpHost, from, subject, content);
111
112 if (fileName != null) {
113 ss.addAttachment(fileName); // optional - use this if send attachment
114 }
115
116 ss.setReply(from); // optional
117
118 ss.addRecipient(addressTO, "TO"); // mandatory
119 // ss.addRecipient(addressCC, "CC"); // optional
120 // ss.addRecipient(addressBCC, "BCC"); // optional
121
122 ss.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional
123 ss.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional
124 ss.setCapabilities("SIGNATURE", 1, 2, 3, 4, 0); // optional
125
126 ss.addSigner(pfxfileName, password, digestAlgorithm,
127 includingCert, includingSignAttrib); // mandatory
128
129 System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... ");
130 ss.signing(externalSigning);
131 System.out.print("Sending signed message ... ");
132 ss.send(); // instead of this next line could be used
133 // Transport.send(es.getSignedMessage());
134 System.out.println("done.");
135 } catch (Exception e) {
136 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
137 if (e instanceof SMIMEException) {
138 // ((SMIMEException)e).loggingErrors(null); //logging
139 ((SMIMEException) e).displayErrors(null);
140 } else
141 System.out.println(e.getMessage());
142 }
143 }
144 }
This page was automatically generated by Maven