KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > test > TestEncryptHtml


1 /*
2  * Title: Oyster Project
3  * Description: S/MIME email transport capabilities.
4  * @Author Vladan Obradovic
5  * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.test;
9
10 import javax.mail.Transport JavaDoc;
11 import org.enhydra.oyster.smime.EnvelopedSMIME;
12 import org.enhydra.oyster.exception.SMIMEException;
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15
16 /**
17  * Tests enveloping process. Encrypted text/html message with or
18  * withouth attachments can be sent by this test. This example is test for
19  * composing of email message content by html code which is given from the file
20  * system. To get help for this example type:
21  * "java org.enhydra.oyster.test.TestEncryptHtml" in command line.
22  * It is assumed that oyster_tests.jar is in your classpath.<BR>
23  * <BR>
24  * Parameters passed to example are:<BR>
25  * &lt;mailHost&gt; &lt;mailAddress&gt; &lt;cerFileName&gt; &lt;algorithmName&gt;
26  * [&lt;attachment&gt;]<BR>
27  * <BR>
28  * &lt;algorithmName&gt; could be: RC240, RC264, RC2128, 3DES or 3DES<BR>
29  * <BR>
30  * Note that for this example email address "FROM" is fixed to: "sender@together.at".
31  * You should change this values in source code of TestEncryptHtml.java in order
32  * to use them with other "FROM" address.
33  */

34 public class TestEncryptHtml
35 {
36
37   public static void main (String JavaDoc[] args)
38   {
39
40     String JavaDoc subject = "S/MIME encrypted message - Subject test: ÜüÄäÖöÜüß";
41     String JavaDoc from = "sender@together.at";
42
43     if (args.length < 4)
44     {
45       System.err.println(
46       System.getProperty("line.separator") +
47       "Usage of TestEncryptHtml: " +
48       System.getProperty("line.separator") +
49       "java TestEncryptHtml <mailHost> <mailAddress> <cerFileName> " +
50       "<algorithmName> [<attachment>]" +
51       System.getProperty("line.separator") +
52       System.getProperty("line.separator") +
53       "Examples:" +
54       System.getProperty("line.separator") +
55       "java TestEncryptHtml together.at recipient@together.at " +
56       "recipient512.cer RC240 " +
57       System.getProperty("line.separator") +
58       "java TestEncryptHtml together.at recipient@together.at " +
59       "recipient512.cer DES .\\test\\Zip8Test.zip");
60       System.exit(-1);
61     }
62     String JavaDoc smtpHost = args[0];
63     String JavaDoc addressTO = args[1];
64     String JavaDoc cerFileName = args[2];
65     String JavaDoc algorithmName = args[3];
66     String JavaDoc fileName = null;
67     if (args.length > 4)
68       fileName = args[4];
69
70     String JavaDoc addressCC = "recipient@together.at";
71     String JavaDoc addressBCC = "recipient@together.at";
72
73     subject = algorithmName + " " + cerFileName + " " + subject;
74
75     File JavaDoc htmlContent = new File JavaDoc("./test/HtmlTest.html");
76
77     EnvelopedSMIME es = null;
78
79     try
80     {
81 // Construction of enveloped smime object
82
es = new EnvelopedSMIME(smtpHost, from, subject, "ISO-8859-1");
83
84 // sets html content withouth its resources
85
es.setContent(htmlContent,"text/html");
86 // instead of previous line, next line could be used to add external alternative
87
// text/plain message instead of autogenerated text/plain message.
88
// es.setContent( htmlContent,"text/html", "External text/plain message!" );
89

90 // To send messages content (withouth resources) from FileInputStream (or any
91
// other input stream) comment previous line and use following commented line
92
// es.setContent(new FileInputStream(htmlContent),"text/html");
93

94       if (fileName!=null)
95       {
96         es.addAttachment(fileName); // optional - use this if send attachment
97
}
98
99       es.setReply(from); // optional
100

101       es.addRecipient(addressTO, "TO", cerFileName); // mandatory
102
// es.addRecipient(addressCC, "CC", cerFileName); // optional
103
// es.addRecipient(addressBCC, "BCC", cerFileName); // optional
104

105       if (algorithmName.equals("RC240"))
106       {
107         System.out.println("Creating the encrypted message with RC2_CBC - 40 bits algorithm... ");
108         es.enveloping(); // instead of this next line could be used
109
// es.enveloping(es.RC2_CBC, 40);
110
}
111       else if (algorithmName.equals("RC264"))
112       {
113         System.out.println("Creating the encrypted message with RC2_CBC - 64 bits algorithm... ");
114         es.enveloping(es.RC2_CBC, 64); // send message with RC2 - 64 bits algorithm
115
}
116       else if (algorithmName.equals("RC2128"))
117       {
118         System.out.println("Creating the encrypted message with RC2_CBC - 128 bits algorithm... ");
119         es.enveloping(es.RC2_CBC, 128); // send message with RC2 - 128 bits algorithm
120
}
121       else if (algorithmName.equals("DES"))
122       {
123         System.out.println("Creating the encrypted message with DES algorithm... ");
124         es.enveloping(es.DES, 56); // send message with DES algorithm
125
}
126       else if (algorithmName.equals("3DES"))
127       {
128         System.out.println("Creating the encrypted message with DES_EDE3_CBC algorithm... ");
129         es.enveloping(es.DES_EDE3_CBC, 192); // send message with 3DES algorithm
130
}
131       System.out.print("Sending encrypted message ... ");
132       es.send(); // instead of this next line could be used
133
// Transport.send(es.getSignedMessage());
134
System.out.println("done.");
135
136     }
137     catch (Exception JavaDoc e) {
138       SMIMEException.setErrorFilePath("Log"); //specifies directory for logging
139
if(e instanceof SMIMEException) {
140         SMIMEException eTmp = (SMIMEException)e;
141 // eTmp.loggingErrors(null); //logging
142
eTmp.displayErrors(null);
143        e = eTmp.getNonSMIMEException();
144        if(e != null)
145          e.printStackTrace();
146       }
147       else {
148         e.printStackTrace();
149       }
150     }
151   }
152
153 }
154
155
156
157
Popular Tags