KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > smime > BaseSMIMEObject


1 /*
2 * Title: Oyster Project
3 * Description: S/MIME email sending capabilities
4 * @Author Vladimir radisic
5 * @Version 2.1.5
6  */

7
8 package org.enhydra.oyster.smime;
9
10 import org.enhydra.oyster.exception.SMIMEException;
11 import org.enhydra.oyster.activation.StreamDataSource;
12 import org.enhydra.oyster.mail.MultipartGenerator;
13 import org.enhydra.oyster.util.ConvertAssist;
14 import org.enhydra.oyster.util.PFXUtils;
15 import org.enhydra.oyster.mail.ContentAnalyzer;
16 import org.enhydra.oyster.crypto.consts.KeyStoreConstants;
17 import javax.mail.Session JavaDoc;
18 import javax.mail.Transport JavaDoc;
19 import javax.mail.MessagingException JavaDoc;
20 import javax.mail.internet.MimeMessage JavaDoc;
21 import javax.mail.internet.MimeBodyPart JavaDoc;
22 import javax.mail.internet.MimeMultipart JavaDoc;
23 import javax.mail.internet.InternetAddress JavaDoc;
24 import javax.activation.DataHandler JavaDoc;
25 import javax.activation.FileDataSource JavaDoc;
26 import javax.activation.MimetypesFileTypeMap JavaDoc;
27 import java.util.Vector JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.security.Security JavaDoc;
34 import sun.security.provider.Sun;
35 import org.bouncycastle.jce.provider.BouncyCastleProvider;
36 import java.security.KeyStore JavaDoc;
37 import java.security.cert.X509Certificate JavaDoc;
38 import java.security.cert.CertificateFactory JavaDoc;
39
40 import java.io.*;
41
42 /**
43  * This class is used as super class for all four email types with SMIME
44  * cryptography capabilities (Enveloped, Signed Signed and Enveloped and Enveloped
45  * and Signed). It contains the common methods for all this types. Also, this
46  * class is used as a super class for class PureMIME which can be used in creation
47  * and sending of pure MIME messages withouth cryptography posibilities.
48  */

49 public class BaseSMIMEObject implements KeyStoreConstants
50 {
51
52   /**
53    * Container for MIME message
54    */

55   protected MimeMessage JavaDoc message;
56
57   /**
58    * Indicator of the email message content part presence (it can be plain text or
59    * html content). Other part of message contains attachements.
60    */

61   protected boolean contentPresence = false;
62
63   /**
64    * Indicator of the external MimeMessage object presence. This parameter is true
65    * when constructor with MimeMessage object is used.
66    */

67   protected boolean externalMessagePresence = false;
68
69   /**
70    * Storage for MIME bodyparts
71    */

72   protected Vector JavaDoc bodyPartArray = new Vector JavaDoc(0, 1);
73
74
75   /**
76    * Storage for .cer files coresponding to appropriate enveloping session
77    */

78   protected Vector JavaDoc certArray = new Vector JavaDoc(0, 1);
79
80   /**
81    * Indication that at least one recipient must be TO (others may be CC or BCC)
82    */

83   protected boolean indicatorTo = false;
84
85   /**
86    * Character set definition. The given Unicode strings will be charset-encoded
87    * by using theis attribute. The charset is also used to set the "charset"
88    * parameter. For example German letters should be encoded by usage of
89    * 'ISO-8859-1' charset. If charset parameter is null and subject or content
90    * contains non US-ASCII characters, it will be encoded using the platform's
91    * default charset. For more information about character sets refer to
92    * 'Character Encodings' chapter of package java.lang Java documentation page,
93    * or to RFC2278.
94    */

95   protected String JavaDoc charsetEnc = null;
96
97   /**
98    * Simple constructor. Dynamically loads the BC and SUN provider necessary for
99    * cryptography processing. This constructor does not create MIME message
100    * object, so it is obligatory to invoke initMimeMessage() method after this
101    * constructor.
102    */

103   protected BaseSMIMEObject ()
104   {
105     Security.addProvider(new BouncyCastleProvider()); // Dynamic loading the BC provider
106
Security.addProvider(new Sun()); // Dynamic loading the SUN provider necessary for SHA1withRSA
107
}
108
109   /**
110    * Initializes the JavaMail session for SMTP and the MimeMessage object for message
111    * which will be sent. Dynamically loads the BC and SUN provider necessary for
112    * cryptography processing. This constructor is used for creating message with
113    * text/plain content. For creating html formated content (text/html), other
114    * constructor should be used in combination with one of setContent methods.
115    * Note that after using this constructor setContent method can be used only
116    * if "content" argument of constructor was given as null, otherwise setContent
117    * method can't be used because content is already set as text/plain.
118    * @param smtpHost name of SMTP host used for sending email
119    * @param fromAddress email address of sender (FROM field in email header)
120    * @param subject subject of email (SUBJECT field in email header). This
121    * argument can be null, but email message will be sent withouth SUBJECT.
122    * @param content text/plain content of email message. This argument can be
123    * null, but later one of setContent() methods or one of addAttachment()
124    * methods should be called
125    * @param charset character set for passed subject and content. The given
126    * Unicode string will be charset-encoded using the specified charset. The
127    * charset is also used to set the "charset" parameter. For example German
128    * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
129    * parameter is null and subject or content contains non US-ASCII characters,
130    * it will be encoded using the platform's default charset.
131    * @exception SMIMEException if smtpHost or fromAddress parameters are null.
132    * Also, it can be caused by non SMIMEException which is MessagingException.
133    */

134   protected BaseSMIMEObject (String JavaDoc smtpHost, String JavaDoc fromAddress, String JavaDoc subject,
135                           String JavaDoc content, String JavaDoc charset) throws SMIMEException
136   {
137     this();
138     try {
139       initMimeMessage(smtpHost, fromAddress, subject, content, charset);
140     }
141     catch(Exception JavaDoc e) {
142       throw SMIMEException.getInstance(this, e, "constructor");
143     }
144   }
145
146
147   /**
148    * Initializes the JavaMail session for SMTP and the MimeMessage object for message
149    * which will be sent. Dynamically loads the BC and SUN provider necessary for
150    * cryptography processing. This constructor does not create content of message
151    * and it can be set later with one of setContent methods. Also, message can be
152    * left withouth content, but then at least one attachement must be added.
153    * @param smtpHost name of SMTP host used for sending email
154    * @param fromAddress email address of sender (FROM field in email header)
155    * @param subject subject of email (SUBJECT field in email header). This
156    * argument can be null, but email message will be sent withouth SUBJECT.
157    * @param charset character set for passed subject and content. The given
158    * Unicode string will be charset-encoded using the specified charset. The
159    * charset is also used to set the "charset" parameter. For example German
160    * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
161    * parameter is null and subject or content contains non US-ASCII characters,
162    * it will be encoded using the platform's default charset.
163    * @exception SMIMEException if smtpHost or fromAddress parameters are null.
164    * Also, it can be caused by non SMIMEException which is MessagingException.
165    */

166   protected BaseSMIMEObject (String JavaDoc smtpHost, String JavaDoc fromAddress, String JavaDoc subject,
167                           String JavaDoc charset) throws SMIMEException
168   {
169     this(smtpHost, fromAddress, subject, null, charset);
170   }
171
172
173   /**
174    * Construction of message with external prepared MimeMessage object. Usage of
175    * this constructor disables usage of setContent() and addAttachment() methods.
176    * Also, all recipients (TO, CC or BCC type) must be declared again via
177    * setRecipient() method, even if they were previously set. Be very carefull
178    * with usage of this constructor because all MimeBodyPart objects and
179    * MimeMultipart objects used in construction of given MimeMessage object,
180    * must have correct defined Content header arguments, and contents. Contents
181    * must be formed in format which can be recognised and appropriate interpreted
182    * in the process of sending mail. If there is any special content object
183    * added to MimeBodyPart object or MimeMultipart object, the appropriate
184    * DataContent handler must be created for that object and set to corresponding
185    * BodyPart.
186    * @param mimeMessage external created MimeMessage object
187    * @exception SMIMEException if smtpHost or fromAddress parameter is null.
188    * Also, it can be caused by non SMIMEException which is MessagingException.
189    */

190   protected BaseSMIMEObject (MimeMessage JavaDoc mimeMessage) throws SMIMEException
191   {
192     try {
193       Security.addProvider(new BouncyCastleProvider()); // Dynamic loading the BC provider
194
Security.addProvider(new Sun()); // Dynamic loading the SUN provider necessary for SHA1withRSA
195

196       this.message = new MimeMessage JavaDoc(mimeMessage);
197       message.removeHeader("TO");
198       message.removeHeader("CC");
199       message.removeHeader("BCC");
200
201       externalMessagePresence = true;
202     }
203     catch(Exception JavaDoc e) {
204       throw SMIMEException.getInstance(this, e, "constructor");
205     }
206   }
207
208
209   /**
210    * Initializes the JavaMail session for SMTP and the MimeMessage object for
211    * message which will be sent. If the argument 'content' is not given as null
212    * this method is used for creating new message with text/plain content. For
213    * creating html formated content (text/html), argument 'content' should be
214    * given as null, and content of message should be set with one of setContent
215    * methods. Note that after using initMimeMessage method, setContent method
216    * can be used only if 'content' argument of constructor was given as null,
217    * otherwise setContent method can't be used because content is already set as
218    * text/plain.
219    * @param smtpHost name of SMTP host used for sending email
220    * @param fromAddress email address of sender (FROM field in email header)
221    * @param subject subject of email (SUBJECT field in email header). This
222    * argument can be null, but email message will be sent withouth SUBJECT.
223    * @param content text/plain content of email message
224    * @param charset character set for passed subject and content. The given
225    * Unicode string will be charset-encoded using the specified charset. The
226    * charset is also used to set the "charset" parameter. For example German
227    * letters should be encoded by usage of 'ISO-8859-1' charset. If 'charset'
228    * argument is null and subject or content contains non US-ASCII characters,
229    * it will be encoded using the platform's default charset.
230    * @exception SMIMEException if smtpHost or fromAddress parameters are null.
231    * Also, it can be caused by non SMIMEException which is MessagingException.
232    */

233   public void initMimeMessage(String JavaDoc smtpHost, String JavaDoc fromAddress, String JavaDoc subject,
234                              String JavaDoc content, String JavaDoc charset) throws SMIMEException
235   {
236     if (smtpHost == null | fromAddress == null)
237       throw new SMIMEException(this, 1041);
238
239     this.charsetEnc = charset;
240
241     Properties JavaDoc sesProp = new Properties JavaDoc();
242     sesProp.setProperty("mail.smtp.host", smtpHost);
243     Session JavaDoc ses = Session.getInstance(sesProp);
244     message = new MimeMessage JavaDoc(ses);
245     try {
246       InternetAddress JavaDoc from = new InternetAddress JavaDoc(fromAddress);
247       message.setFrom(from);
248       if (subject != null) {
249         if(charset != null)
250           message.setSubject(subject, charset);
251         else
252           message.setSubject(subject);
253       }
254       if (content != null) {
255         MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
256         if(charset != null)
257           mbp.setText(content, charset);
258         else
259           mbp.setText(content);
260         mbp.setText(content);
261         bodyPartArray.addElement(mbp);
262         contentPresence = true;
263       }
264     }
265     catch(Exception JavaDoc e) {
266       throw SMIMEException.getInstance(this, e, "initMimeMessage");
267     }
268   }
269
270
271   /**
272    * Sets the character set encoding. This attribute also can be set during the
273    * construction of object, or by invoking initMimeMessage() method. By
274    * invoking this method, previously set parameter will be overriden.
275    * @param charset characterset encoding definition which will be used in
276    * process of string to byte array (and vice versa) transformation. The
277    * 'charset' argument is also used to set the 'charset' parameter in message
278    * or body header. For example German letters should be encoded by usage of
279    * 'ISO-8859-1' charset. If 'charset' argument is null and String contains non
280    * US-ASCII characters it will be encoded using the platform's default charset.
281    */

282   public void setCharsetEncoding(String JavaDoc charset) {
283     this.charsetEnc = charset;
284   }
285
286
287   /**
288    * Sets message content. Message content can be given in two differrent forms:
289    * text and html code. If content is type of text, parameter "type" should be
290    * "text/plain" and other two parameters are not in use (should be set to null).
291    * If content is type of html code, parameter "type" should be set as "text/html",
292    * otherwise (if it is set as "text/plain") html code is processed as plain
293    * text. This method can be performed only once.<BR>
294    * <BR>
295    * In case of html content, it is essential to (on appropriate way) associate some
296    * attributes of particular elements in html code ("background" or "src" atributes)
297    * with corresponding ressources (URL-s, relative file addresses or byte array
298    * streams). This resources should all be sent with message to enable recipient
299    * to see complete html message. Location of resources can be given in few
300    * different forms and depending on that, allocation resolving can be successful or
301    * not. Following text represents different possibilities for defining locations
302    * of resources (pictures, animations, sound...) inside of html code passed to
303    * this method, and necessery passed additional parameters used for resolving
304    * this resource locations.<BR>
305    * <BR>
306    * <UL>
307    * <LI>URL defined as: http://... is left unchanged. This resource is not sent
308    * with the message and it couldn't be seen by recipient if it is not online on
309    * the internet.</LI>
310    * <LI>URL defined as: file://... is transformed to corresponding Content ID if
311    * the resource can be found on specified location and it is sent with message.</LI>
312    * <LI>Absolute path, for example defined as: "c:\tmp\test\picture.bmp", is
313    * transformed to corresponding Content ID if the resource can be found on
314    * specified location, and is sent with message. If all resources in html
315    * code are specified with its absolute path, the 3rd parameter in this method
316    * can be null.</LI>
317    * <LI>Relative path of all resources specified in html code, for example
318    * defined as: ".\test\picture.bmp" and ".\example\flush.swf", must be defined
319    * to be relative to same directory path (in this case it is c:\tmp). This parameter
320    * (common directory path) is given as 3rd parameter in this method, and is named
321    * "path". If html code is obtained from .html file, necessery common directory
322    * path is usually path to this .html file. Location of resource is transformed
323    * to corresponding Content ID if the resource can be found on specified location.
324    * This location is sent with the message.</LI>
325    * <LI>Byte array stream as resource for html attribute must be referenced from
326    * html code as: <BR>
327    * <BR><PRE>
328    * *****nnn&lt;virtual_file_name&gt;<BR>
329    * <BR></PRE>
330    * Five '*' characters (must be five) define that it is resource expected from
331    * the stream. Other three characters must be digits (000-999) and represent
332    * index of corresponding stream in stream array. virtual_file_name is name and
333    * extension assigned to data passed from stream. Name is used in construction of
334    * "name" parameter in Content-Type, while extension of file name is used in
335    * detection of appropriate mime-type. Lenght of virtual_file_name is not
336    * important. If there is no data referenced from byte array stream ,4th
337    * parameter of this method named "resources" can be null. Also, if all resources
338    * are passed through the array of streams, 3th parameter ("path") can be null.
339    * Location of resource is transformed to corresponding Content ID if no error
340    * has occured during the process of allocation.</LI>
341    * </UL>
342    * <BR>
343    * All mentioned resource allocation types can be combined together in the same
344    * html code, and all will be processed with appropriate use of this method.<BR>
345    * <BR>
346    * Note that number of resource references that are defined in html code by
347    * using virtual_file_names must be greater than or equal to number of elements
348    * in array of InputStream (4th parameter). If one resource (one element in array
349    * of IputStream) is used in html code more than once, it is advisable to use
350    * same virtual_file_name in html code because message is then sent with only
351    * one attached resource (image, movie...). It is essetntial that desired resource
352    * in input stream array corresponds to specified "nnn" part of virtual_file_name.<BR>
353    * <BR>
354    * If resources specified on any described name can not be found or resolved,
355    * or if any exception has occured during its processing, they won't be added and
356    * html message will be sent withouth them.
357    * @param content String representation of message content (text or html code).
358    * This argument is mandatory.
359    * @param type type of given content. It can take values: "text/plain" or
360    * "text/html". This argument is mandatory.
361    * @param path common directory path for relative file locations in html code.
362    * It can be null if all resources set absolute path or are defined by
363    * byte array streams, or if sending resources with relative address it is not desired.
364    * Also, it is set to null in case of text/plain message.
365    * @param resources way for representing resources used in the given html code
366    * which will be added to message as array of InputStream. Detail use
367    * of this argument is described above. It can be null if no resources as byte
368    * array stream are used, or if sending resources given in that way is not desired.
369    * Also, it is set to null in case of text/plain message.
370    * @param externalPlainText external text/plain message represented as String.
371    * This argument is used as alternative message to given html message in the process
372    * of generation multipart/alternative MimeMultipart message. If this argument
373    * has value null then autogeneration of text/plain message is performed
374    * according to passed html code (content argument). Also, it is set to null
375    * in case of text/plain message.
376    * @exception SMIMEException if content is tried to be added twice, or in case of
377    * wrong "type" parameter. Also, it can be caused by non SMIMEException which can
378    * be one of the following: MessagingException UnsoportedEncodingException.
379    */

380   public void setContent(String JavaDoc content, String JavaDoc type, String JavaDoc path, InputStream JavaDoc[] resources,
381                          String JavaDoc externalPlainText) throws SMIMEException {
382
383     if(content != null) {
384       ByteArrayInputStream JavaDoc bais = null;
385       try {
386         if(this.charsetEnc != null)
387           bais = new ByteArrayInputStream JavaDoc( content.getBytes(this.charsetEnc) );
388         else
389           bais = new ByteArrayInputStream JavaDoc( content.getBytes("ISO-8859-1") );
390       }
391       catch(Exception JavaDoc e) {
392         throw SMIMEException.getInstance(this, e, "setContent");
393       }
394       this.setContent(bais, type, path, resources, externalPlainText);
395
396     }
397     else
398       throw new SMIMEException(this, 1035);
399   }
400
401   /**
402    * Sets message content from String. This method can be performed only once.
403    * Method is the same as corresponding method setContent with five parameters,
404    * where fifth parameter is set to null. This setContent method (in case that second
405    * argument - type has value text/html) is used with autogeneration of text/plain
406    * message according to given html code in the process of generation
407    * multipart/alternative MimeMultipart message. For further information refer
408    * to setContent method with five arguments.
409    * @param content String representation of message content (text or html code).
410    * This argument is mandatory.
411    * @param type type of given content. It can take values: "text/plain" or
412    * "text/html". This argument is mandatory.
413    * @param path common directory path for relative file locations in html code.
414    * It can be null if all resources set absolute path or are defined by
415    * byte array streams, or if sending resources with relative address it is not desired.
416    * Also, it is set to null in case of text/plain message.
417    * @param resources way for representing resources used in the given html code
418    * which will be added to message as array of InputStream. Detail use
419    * of this argument is described above. It can be null if no resources as byte
420    * array stream are used, or if sending resources given in that way is not desired.
421    * Also, it is set to null in case of text/plain message.
422    * @exception SMIMEException if content is tried to be added twice, or in case of
423    * wrong "type" parameter. Also, it can be caused by non SMIMEException which can
424    * be one of the following: MessagingException UnsoportedEncodingException.
425    */

426   public void setContent(String JavaDoc content, String JavaDoc type, String JavaDoc path, InputStream JavaDoc[] resources)
427       throws SMIMEException {
428
429     this.setContent(content, type, path, resources, null);
430   }
431
432
433   /**
434    * Sets message content from InputStream. This method can be performed only once.
435    * Message content can be given in two differrent forms: text and html code. If
436    * content is type of text, parameter "type" should be "text/plain", while if
437    * content is type of html code, parameter "type" should be set as "html/code".
438    * For further information refer to setContent method with five arguments
439    * (String, String, String, InputStream[], String) which is called by this method.
440    * @param content message content data given from any InputStream.
441    * Data can be text or html code and will be interpreted according to second
442    * parameter: "type".
443    * @param type type of given content. It can take values: "text/plain" or
444    * "text/html". This argument is mandatory.
445    * @param path common directory path for relative file locations in html code.
446    * It can be null if all resources in html code have set absolute path or are
447    * defined by byte array streams, or if sending resources with relative address
448    * is not desired. Also, it is set to null in case of text/plain message.
449    * @param resources way for representing resources used in the given html code
450    * which will be added to message as array of InputStreams. Detail use
451    * of this argument is described in other setContent methods mentioned before.
452    * It can be null if no resources as byte array stream are used, or if sending
453    * resources given in that way is not desired. Also, it is set to null in case
454    * of text/plain message.
455    * @param externalPlainText external text/plain message represented as String.
456    * This argument is used as alternative message to given html message in the process
457    * of generation multipart/alternative MimeMultipart message. If this argument
458    * has value null then autogeneration of text/plain message is performed
459    * according to passed html code (content argument). Also, it is set to null
460    * in case of text/plain message.
461    * @exception SMIMEException if content is tried to be added twice , in case of
462    * wrong "type" parameter or in case when parameter content is null. Also, it can
463    * be caused by non SMIMEException which is MessagingException.
464    */

465   public void setContent(InputStream JavaDoc content, String JavaDoc type, String JavaDoc path,
466                          InputStream JavaDoc[] resources, String JavaDoc externalPlainText) throws SMIMEException {
467     if(contentPresence)
468       throw new SMIMEException(this, 1049);
469     if(content != null) {
470       try {
471         if(type.equalsIgnoreCase("text/plain")) {
472           MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
473           String JavaDoc temp = null;
474           if(this.charsetEnc != null) {
475             temp = new String JavaDoc(ConvertAssist.inStreamToByteArray(content), this.charsetEnc);
476             mbp.setText(temp, this.charsetEnc);
477           }
478           else {
479             temp = new String JavaDoc(ConvertAssist.inStreamToByteArray(content), "ISO-8859-1");
480             mbp.setText(temp, "ISO-8859-1");
481           }
482           bodyPartArray.add(0, mbp);
483           contentPresence = true;
484         }
485         else if(type.equalsIgnoreCase("text/html")) {
486           MimeMultipart JavaDoc htmlMultipart =
487               MultipartGenerator.getHtmlMultipart(content, path, resources, externalPlainText);
488           bodyPartArray.add(0, htmlMultipart);
489           contentPresence = true;
490         }
491         else
492           throw new SMIMEException(this, 1048);
493       }
494       catch(Exception JavaDoc e) {
495         throw SMIMEException.getInstance(this, e, "setContent");
496       }
497     }
498     else
499       throw new SMIMEException(this, 1035);
500   }
501
502
503   /**
504    * Sets message content from InputStream. This method can be performed only once.
505    * Method is same as corresponding method setContent with five parameters, where
506    * fifth parameter is set to null. This setContent method (in case that second
507    * argument - type has value text/html) is used with autogeneration of text/plain
508    * message according to given html code in process of generation multipart/alternative
509    * MimeMultipart message. For further information refer to setContent method with
510    * five arguments (InputStream, String, String, InputStream[], String) which is
511    * called by this method.
512    * @param content message content data given from any InputStream.
513    * Data can be text or html code and will be interpreted according to second
514    * parameter: "type".
515    * @param type type of given content. It can take values: "text/plain" or
516    * "text/html". This argument is mandatory.
517    * @param path common directory path for relative file locations in html code.
518    * It can be null if all resources in html code have set absolute path or are
519    * defined by byte array streams, or if sending resources with relative address
520    * is not desired. Also, it is set to null in case of text/plain message.
521    * @param resources way for representing resources used in the given html code
522    * which will be added to message as array of InputStreams. Detail use
523    * of this argument is described in other setContent methods mentioned before.
524    * It can be null if no resources as byte array stream are used, or if sending
525    * resources given in that way is not desired. Also, it is set to null in case
526    * of text/plain message.
527    * @exception SMIMEException if content is tried to be added twice , in case of
528    * wrong "type" parameter or in case when parameter content is null. Also, it can
529    * be caused by non SMIMEException which is MessagingException.
530    */

531   public void setContent(InputStream JavaDoc content, String JavaDoc type, String JavaDoc path,
532                          InputStream JavaDoc[] resources) throws SMIMEException {
533     this.setContent(content, type, path, resources, null);
534   }
535
536
537   /**
538    * Sets message content from InputStream. This method can be performed only once.
539    * Message content can be given in two differrent forms: text and html code. If
540    * content is type of text, parameter "type" should be "text/plain", while if
541    * content is type of html code, parameter "type" should be set as "html/code".
542    * If html code content is set by this method, message will be generated withouth
543    * inclusion of the resources defined in paricular html element's attribute ("src" and
544    * "background"). Only plain html code will be sent and any reference to local
545    * file system resources will be useless for recipient of the message. HTTP referenced
546    * resources can still be available if recipient is online on Internet. Message
547    * generated on this way is smaller so encrypting process should be faster.
548    * @param content message content data given from any InputStream.
549    * Data could be text or html code and will be interpreted according to second
550    * parameter: "type". This argument is mandatory.
551    * @param type type of given content. It can take values: "text/plain" or
552    * "text/html". This argument is mandatory.
553    * @param externalPlainText external text/plain message represented as String.
554    * This argument is used as alternative message to given html message in the process
555    * of generation multipart/alternative MimeMultipart message. If this argument
556    * has value null then autogeneration of text/plain message is performed
557    * according to passed html code (content argument). Also, it is set to null
558    * in case of text/plain message.
559    * @exception SMIMEException if content is tried to be added twice , in case of
560    * wrong "type" parameter or in case when parameter content is null. Also, it can
561    * be caused by non SMIMEException which is MessagingException.
562    */

563   public void setContent(InputStream JavaDoc content, String JavaDoc type, String JavaDoc externalPlainText) throws SMIMEException {
564     if(contentPresence)
565       throw new SMIMEException(this, 1049);
566     if(content != null) {
567       try {
568         if(type.equalsIgnoreCase("text/plain")) {
569           MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
570           String JavaDoc temp = null;
571           if(this.charsetEnc != null) {
572             temp = new String JavaDoc(ConvertAssist.inStreamToByteArray(content), this.charsetEnc);
573             mbp.setText(temp, this.charsetEnc);
574           }
575           else {
576             temp = new String JavaDoc(ConvertAssist.inStreamToByteArray(content), "ISO-8859-1");
577             mbp.setText(temp, "ISO-8859-1");
578           }
579
580           bodyPartArray.add(0, mbp);
581           contentPresence = true;
582         }
583         else if(type.equalsIgnoreCase("text/html")) {
584           MimeMultipart JavaDoc htmlMultipart = MultipartGenerator.getHtmlMultipart(content);
585           bodyPartArray.add(0, htmlMultipart);
586           contentPresence = true;
587         }
588         else
589           throw new SMIMEException(this, 1048);
590       }
591       catch(Exception JavaDoc e) {
592         throw SMIMEException.getInstance(this, e, "setContent");
593       }
594     }
595     else
596       throw new SMIMEException(this, 1035);
597   }
598
599
600   /**
601    * Sets message content from InputStream. This method can be performed only once.
602    * Method is the same as corresponding method setContent with three parameters,
603    * where third parameter is set to null. This setContent method (in case that second
604    * argument - type has value text/html) is used with autogeneration of text/plain
605    * message according to given html code in the process of generation
606    * multipart/alternative MimeMultipart message. For further information refer
607    * to setContent method with thre arguments.
608    * @param content message content data given from any InputStream.
609    * Data could be text or html code and will be interpreted according to second
610    * parameter: "type". This argument is mandatory.
611    * @param type type of given content. It can take values: "text/plain" or
612    * "text/html". This argument is mandatory.
613    * @exception SMIMEException if content is tried to be added twice , in case of
614    * wrong "type" parameter or in case when parameter content is null. Also, it can
615    * be caused by non SMIMEException which is MessagingException.
616    */

617   public void setContent(InputStream JavaDoc content, String JavaDoc type) throws SMIMEException {
618     this.setContent(content, type, null);
619   }
620
621
622
623   /**
624    * Sets message content from String. This method can be performed only once.
625    * Message content can be given in two differrent forms: text and html code. If
626    * content is type of text, parameter "type" should be "text/plain", while if
627    * content is type of html code, parameter "type" should be set as "html/code".
628    * If html code content is set by this method, message will be generated withouth
629    * inclusion of the resources defined in paricular html element's attribute ("src" or
630    * "background"). Only plain html code will be sent and any reference to local
631    * file system resources will be useless for recipient of the message. HTTP referenced
632    * resources can still be available if recipient is online on Internet. Message
633    * generated on this way is smaller, so encrypting process should be faster.
634    * @param content message content data given as String which can
635    * be text or html code and will be interpreted according to second parameter:
636    * "type". This argument is mandatory.
637    * @param type type of given content. It can take values: "text/plain" or
638    * "text/html". This argument is mandatory.
639    * @param externalPlainText external text/plain message represented as String.
640    * This argument is used as alternative message to given html message in the process
641    * of generation multipart/alternative MimeMultipart message. If this argument
642    * has value null then autogeneration of text/plain message is performed
643    * according to passed html code (content argument). Also, it is set to null
644    * in case of text/plain message.
645    * @exception SMIMEException if content is tried to be added twice, or in case of
646    * wrong "type" parameter. Also, it can be caused by non SMIMEException which can
647    * be one of the following: MessagingException UnsoportedEncodingException.
648    */

649   public void setContent(String JavaDoc content, String JavaDoc type, String JavaDoc externalPlainText) throws SMIMEException {
650
651     if(content != null) {
652       ByteArrayInputStream JavaDoc bais = null;
653       try {
654         if(this.charsetEnc != null)
655           bais = new ByteArrayInputStream JavaDoc( content.getBytes(this.charsetEnc) );
656         else
657           bais = new ByteArrayInputStream JavaDoc( content.getBytes("ISO-8859-1") );
658       }
659       catch(Exception JavaDoc e) {
660         throw SMIMEException.getInstance(this, e, "setContent");
661       }
662       this.setContent(bais, type);
663
664     }
665     else
666       throw new SMIMEException(this, 1035);
667   }
668
669
670   /**
671    * Sets message content from String. This method can be performed only once.
672    * Method is same as corresponding method setContent with three parameters,
673    * where third parameter is set to null. This setContent method (in case that second
674    * argument - type has value text/html) is used with autogeneration of text/plain
675    * message according to given html code in the process of generation
676    * multipart/alternative MimeMultipart message. For further information refer
677    * to setContent method with three arguments.
678    * @param content message content data given as String which can
679    * be text or html code and will be interpreted according to second parameter:
680    * "type". This argument is mandatory.
681    * @param type type of given content. It can take values: "text/plain" or
682    * "text/html". This argument is mandatory.
683    * @exception SMIMEException if content is tried to be added twice , in case of
684    * wrong "type" parameter or in case when parameter content is null. Also, it can
685    * be caused by non SMIMEException which is MessagingException.
686    */

687   public void setContent(String JavaDoc content, String JavaDoc type) throws SMIMEException {
688
689     this.setContent(content, type, null);
690   }
691
692   /**
693    * Sets message content from file represented by File object. This method can be
694    * performed only once. Message content can be given in two differrent forms:
695    * text and html code. If content is type of text, parameter "type" should be
696    * "text/plain", while if content is type of html code, parameter "type" should
697    * be set as "html/code". Note that for this method, location of resources
698    * needed by html file (pictures, sounds... ) shouldn't be defined in html code
699    * as they should be passed as InputStream arrays. For further information refer
700    * to setContent method with five arguments (String, String, String, InputStream[], String)
701    * which is called by this method.
702    * @param inFile location of file which is used for content of the message.
703    * This argument is mandatory.
704    * @param type type of given content. It can take values: "text/plain" or
705    * "text/html". This argument is mandatory.
706    * @param externalPlainText external text/plain message represented as String.
707    * This argument is used as alternative message to given html message in the process
708    * of generation multipart/alternative MimeMultipart message. If this argument
709    * has value null then autogeneration of text/plain message is performed
710    * according to passed html code (content argument). Also, it is set to null
711    * in case of text/plain message.
712    * @exception SMIMEException if content is tried to be added twice, in case of
713    * wrong "type" parameter, or if passed file (as File object) does not exist in
714    * file sistem. Also, it can be caused by non SMIMEException which can be one of
715    * the following: MessagingException or IOException.
716    */

717   public void setContent(File JavaDoc inFile, String JavaDoc type, String JavaDoc externalPlainText) throws SMIMEException {
718
719     if(contentPresence)
720       throw new SMIMEException(this, 1049);
721     if(inFile != null && inFile.exists()) {
722       try {
723         File JavaDoc inFileAbs = inFile.getAbsoluteFile().getCanonicalFile();
724         String JavaDoc content = ConvertAssist.fileToString(inFileAbs);
725         this.setContent(content, type, inFile.getParent(), null, externalPlainText);
726       }
727       catch(Exception JavaDoc e) {
728         throw SMIMEException.getInstance(this, e, "setContent");
729       }
730     }
731     else
732       throw new SMIMEException(this, 1034);
733   }
734
735   /**
736    * Sets message content from file represented by File object. This method can be
737    * performed only once. Method is the same as corresponding method setContent with
738    * three parameters, where third parameter is set to null. This setContent
739    * method (in case that second argument - type has value text/html) is used
740    * with autogeneration of text/plain message according to given html code in
741    * the process of generation multipart/alternative MimeMultipart message. For further
742    * information refer to corresponding setContent method with thre arguments.
743    * @param inFile location of file which is used for content of the message.
744    * This argument is mandatory.
745    * @param type type of given content. It can take values: "text/plain" or
746    * "text/html".
747    * @exception SMIMEException if content is tried to be added twice , in case of
748    * wrong "type" parameter or in case when parameter content is null. Also, it can
749    * be caused by non SMIMEException which is MessagingException.
750    */

751   public void setContent(File JavaDoc inFile, String JavaDoc type) throws SMIMEException {
752     this.setContent(inFile, type, null);
753   }
754
755   /**
756    * Adds file as attachment to email message. Content-Type will be automatically
757    * detected. Note that for linux files, if they are recognised as files with
758    * text/... MIME Content-Type, line breaks (LF) will be transformed to Windows
759    * line breaks (CRLF). Same transformation will be performed to single Carriage
760    * Return (CR) character. If linux textual file should be transported via
761    * email untouched, the best solution is to force usage of BASE64 encoding with
762    * other addAttachment() method.
763    * @param fileName path and file name used for attachment
764    * @exception SMIMEException if passed file (as File object) does not exist in
765    * the file sistem. Also, it can be caused by non SMIMEException which is
766    * MessagingException
767    */

768   public void addAttachment (String JavaDoc fileName) throws SMIMEException
769   {
770     File JavaDoc fn = new File JavaDoc(fileName);
771     this.addAttachment(fn);
772   }
773
774   /**
775    * Adds file as attachment to email message with given value for prefered
776    * Content-Transfer-Encoding argument. Attachment will be encoded acording to
777    * this parameter. Be carful with usage of this method, because you must know
778    * nature and content of file well, to avoid errors in encoding. Since this
779    * method does not employ automatic detection of Content-Transfer-Encoding,
780    * it is sligtly faster. Note that for linux textual files, if they are encoded
781    * as quoted-printable, line breaks (LF) will be transformed to Windows line
782    * breaks (CRLF). Same transformation will be performed to single Carriage
783    * Return (CR) character. If linux textual file should be transported
784    * via email untouched, the best solution is to force usage of BASE64 encoding.
785    * @param fileName path and file name used for attachment
786    * @param encoding Content-Transfer-Encoding value. It can take values: 7bit,
787    * quoted-printable, and base64
788    * @exception SMIMEException if passed file (as File object) does not exist in
789    * the file sistem. Also, it can be caused by non SMIMEException which is
790    * MessagingException
791    */

792   public void addAttachment (String JavaDoc fileName, String JavaDoc encoding) throws SMIMEException
793   {
794     File JavaDoc fn = new File JavaDoc(fileName);
795     this.addAttachment(fn, encoding);
796   }
797
798   /**
799    * Adds file as attachment to email message. Content-Type will be automatically
800    * detected. Note that for linux files, if they are recognised as files with
801    * text/... MIME Content-Type, line breaks (LF) will be transformed to Windows
802    * line breaks (CRLF). Same transformation will be performed to single Carriage
803    * Return (CR) character. If linux textual file should be transported via
804    * email untouched, the best solution is to force usage of BASE64 encoding with
805    * other addAttachment() method.
806    * @param file used for attachment represented as File object
807    * @exception SMIMEException if passed file (as File object) does not exist in
808    * the file sistem. Also, it can be caused by non SMIMEException which is
809    * MessagingException or IOException
810    */

811   public void addAttachment (File JavaDoc file) throws SMIMEException
812   {
813     if(!file.exists())
814       throw new SMIMEException(this, 1034);
815     try {
816       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
817       int u = fis.available();
818       this.addAttachment(fis, file.getName());
819     }
820     catch(Exception JavaDoc e) {
821       throw SMIMEException.getInstance(this, e, "addAttachment");
822     }
823   }
824
825   /**
826    * Adds file as attachment to email message with given value for prefered
827    * Content-Transfer-Encoding argument. Attachment will be encoded acording to
828    * this parameter. Be carful with usage of this method, because you must know
829    * nature and content of file well, to avoid errors in encoding. Since this
830    * method does not employ automatic detection of Content-Transfer-Encoding,
831    * it is sligtly faster. Note that for linux textual files, if they are encoded
832    * as quoted-printable, line breaks (LF) will be transformed to Windows line
833    * breaks (CRLF). Same transformation will be performed to single Carriage
834    * Return (CR) character. If linux textual file should be transported
835    * via email untouched, the best solution is to force usage of BASE64 encoding.
836    * @param file path and file name used for attachment
837    * @param encoding Content-Transfer-Encoding value. It can take values: 7bit,
838    * quoted-printable, and base64
839    * @exception SMIMEException if passed file (as File object) does not exist in
840    * the file sistem. Also, it can be caused by non SMIMEException which is
841    * MessagingException
842    */

843   public void addAttachment (File JavaDoc file, String JavaDoc encoding) throws SMIMEException
844   {
845     if(!file.exists())
846       throw new SMIMEException(this, 1034);
847     try {
848       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
849       int u = fis.available();
850       this.addAttachment(fis, file.getName(), encoding);
851     }
852     catch(Exception JavaDoc e) {
853       throw SMIMEException.getInstance(this, e, "addAttachment");
854     }
855   }
856
857   /**
858    * Adds data from InputStream as attachment to email message. Content-Type
859    * will be automaticaly detected. Note that for linux files (virtual or real),
860    * if they are recognised as files with text/... MIME Content-Type, line breaks
861    * (LF) will be transformed to Windows line breaks (CRLF). Same transformation
862    * will be performed to single Carriage Return (CR) character. If linux textual
863    * file should be transported via email untouched, the best solution is to force
864    * usage of BASE64 encoding with other addAttachment() method.
865    * @param data byte array from InputStream
866    * @param fileName virtual or real file name (wihouth path). Correct information
867    * about name; extension of file name is especially important. Name
868    * is used in construction of "name" parameter in Content-Type header line of
869    * body parts of mime message. Extension of file is used in detection of the
870    * appropriate mime-type.
871    * @exception SMIMEException caused by non SMIMEException which is
872    * MessagingException
873    */

874   public void addAttachment (InputStream JavaDoc data, String JavaDoc fileName) throws SMIMEException
875   {
876     MimeBodyPart JavaDoc attachment = new MimeBodyPart JavaDoc();
877     try {
878       StreamDataSource streamDataSrc = new StreamDataSource( data, fileName );
879       attachment.setDataHandler( new DataHandler JavaDoc(streamDataSrc) );
880       attachment.setDisposition(attachment.ATTACHMENT);
881       attachment.setFileName(fileName);
882
883       ContentAnalyzer analyzer = new ContentAnalyzer();
884       analyzer.isFor7BitEncoding(streamDataSrc.getInputStream());
885       String JavaDoc encoding = analyzer.getPreferedEncoding();
886       if( !streamDataSrc.getContentType().toLowerCase().startsWith("text/") &&
887          encoding.equalsIgnoreCase("quoted-printable") )
888         encoding = "base64";
889
890       attachment.setHeader("Content-Transfer-Encoding", encoding);
891
892       bodyPartArray.addElement(attachment);
893     }
894     catch(Exception JavaDoc e) {
895       throw SMIMEException.getInstance(this, e, "addAttachment");
896     }
897   }
898
899   /**
900    * Adds data from InputStream as attachment to email message with given value
901    * for prefered Content-Transfer-Encoding argument. Attachment will be encoded
902    * acording to this parameter. Be carful with usage of this method, because you
903    * must know nature and content of file well, to avoid errors in encoding. Since
904    * this method does not employ automatic detection of Content-Transfer-Encoding,
905    * it is sligtly faster. Note that for linux textual files, if they are encoded
906    * as quoted-printable, line breaks (LF) will be transformed to Windows line
907    * breaks (CRLF). Same transformation will be performed to single Carriage
908    * Return (CR) character. If linux textual file should be transported
909    * via email untouched, the best solution is to force usage of BASE64 encoding.
910    * @param fileName path and file name used for attachment
911    * @param encoding Content-Transfer-Encoding value. It can take values: 7bit,
912    * quoted-printable, and base64
913    * @exception SMIMEException if passed file (as File object) does not exist in
914    * the file sistem. Also, it can be caused by non SMIMEException which is
915    * MessagingException
916    */

917   public void addAttachment (InputStream JavaDoc data, String JavaDoc fileName, String JavaDoc encoding) throws SMIMEException
918   {
919     MimeBodyPart JavaDoc attachment = new MimeBodyPart JavaDoc();
920     try {
921       StreamDataSource streamDataSrc = new StreamDataSource( data, fileName );
922       attachment.setDataHandler( new DataHandler JavaDoc(streamDataSrc) );
923       attachment.setDisposition(attachment.ATTACHMENT);
924       attachment.setFileName(fileName);
925
926       if( !(encoding.equalsIgnoreCase("7bit") ||
927             encoding.equalsIgnoreCase("quoted-printable") ||
928             encoding.equalsIgnoreCase("base64")) )
929         throw new SMIMEException(this, 1036);
930
931       attachment.setHeader("Content-Transfer-Encoding", encoding);
932
933       bodyPartArray.addElement(attachment);
934     }
935     catch(Exception JavaDoc e) {
936       throw SMIMEException.getInstance(this, e, "addAttachment");
937     }
938   }
939
940
941   /**
942    * Adds recipient address, type and .cer file of email recipient to encrypted
943    * message. Certificate file should be passed only in the case of Enveloped
944    * message or Signed and Enveloped message. Otherwise this argument represents
945    * redundance value and it should be given as null.
946    * @param recipientAddress email address of recipent (fields TO or CC or BCC
947    * in email message header)
948    * @param type should be TO, CC or BCC
949    * @param cerFileName path and file name with certificate corresponding
950    * to recipient (file with .cer extension)
951    * @exception SMIMEException if type of addressing of messages is not TO, CC
952    * or BCC. Also, it can be caused by non SMIMEException which can be one of
953    * the following: IOException, MessagingException, FileNotFoundException,
954    * NoSuchProviderException, CertificateException.
955    */

956   protected void addRecipient (String JavaDoc recipientAddress, String JavaDoc type, String JavaDoc cerFileName)
957       throws SMIMEException
958   {
959     try {
960       if (!type.equalsIgnoreCase("TO") && !type.equalsIgnoreCase("BCC") && !type.equalsIgnoreCase("CC"))
961         throw new SMIMEException(this, 1042);
962       if (type.equalsIgnoreCase("TO")) {
963         message.addRecipients(MimeMessage.RecipientType.TO, recipientAddress);
964         indicatorTo = true;
965       }
966       else if (type.equalsIgnoreCase("CC"))
967         message.addRecipients(MimeMessage.RecipientType.CC, recipientAddress);
968       else if (type.equalsIgnoreCase("BCC"))
969         message.addRecipients(MimeMessage.RecipientType.BCC, recipientAddress);
970
971       if(cerFileName != null) {
972         X509Certificate JavaDoc cert = null;
973         InputStream JavaDoc inStream = new FileInputStream JavaDoc(cerFileName);
974         CertificateFactory JavaDoc cf = CertificateFactory.getInstance("X.509", "BC");
975         cert = (X509Certificate JavaDoc) cf.generateCertificate(inStream);
976         inStream.close();
977
978         certArray.addElement(cert); // adding X509Certificate of .cer file in stack
979
}
980     }
981     catch(Exception JavaDoc e) {
982       throw SMIMEException.getInstance(this, e, "addRecipient");
983     }
984   }
985
986
987   /**
988    * Adds recipient address, type and recipient's certificate via KeyStore
989    * object and apropriate alias. Certificate information should be passed only
990    * in the case of Enveloped message or Signed and Enveloped message. Otherwise
991    * those arguments represent redundance values and they should be given as
992    * null.
993    * @param recipientAddress email address of recipent (fields TO or CC or BCC
994    * in email message header)
995    * @param type should be TO, CC or BCC
996    * @param kStore instance of KeyStore class which represents an in-memory
997    * collection of keys and certificates.
998    * @param alias alias name which corresponds to desired certificate. If alias
999    * is given as null, then reading results are unpredictable.
1000   * @exception SMIMEException if type of addressing of messages is not TO, CC
1001   * or BCC. Also, it can be caused by non SMIMEException which can be one of
1002   * the following: IOException, MessagingException, FileNotFoundException,
1003   * NoSuchProviderException, CertificateException.
1004   */

1005  protected void addRecipient (String JavaDoc recipientAddress, String JavaDoc type, KeyStore JavaDoc kStore, String JavaDoc alias)
1006      throws SMIMEException
1007  {
1008    try {
1009      if (!type.equalsIgnoreCase("TO") && !type.equalsIgnoreCase("BCC") && !type.equalsIgnoreCase("CC"))
1010        throw new SMIMEException(this, 1042);
1011      if (type.equalsIgnoreCase("TO")) {
1012        message.addRecipients(MimeMessage.RecipientType.TO, recipientAddress);
1013        indicatorTo = true;
1014      }
1015      else if (type.equalsIgnoreCase("CC"))
1016        message.addRecipients(MimeMessage.RecipientType.CC, recipientAddress);
1017      else if (type.equalsIgnoreCase("BCC"))
1018        message.addRecipients(MimeMessage.RecipientType.BCC, recipientAddress);
1019
1020      if(kStore != null) {
1021        X509Certificate JavaDoc cert = null;
1022        if (alias != null)
1023          cert = (X509Certificate JavaDoc) kStore.getCertificate(alias);
1024        else {
1025          X509Certificate JavaDoc[] temp509 = PFXUtils.getCertificateChain(kStore);
1026          if (temp509 != null && temp509.length > 0)
1027            cert = temp509[0];
1028          else
1029            cert = PFXUtils.getPFXOwnerX509Certificate(kStore);
1030        }
1031
1032        certArray.addElement(cert); // adding X509Certificate of .cer file in stack
1033
}
1034
1035    }
1036    catch(Exception JavaDoc e) {
1037      throw SMIMEException.getInstance(this, e, "addRecipient");
1038    }
1039  }
1040
1041
1042  /**
1043   * Adds recipient address, type and recipient's certificate via path to the
1044   * KeyStore file, KeyStore type, password and apropriate alias. Certificate
1045   * information should be passed only in the case of Enveloped message or
1046   * Signed and Enveloped message. Otherwise those arguments represent
1047   * redundance values and they should be given as null.
1048   * @param recipientAddress email address of recipent (fields TO or CC or BCC
1049   * in email message header)
1050   * @param type should be TO, CC or BCC
1051   * @param ksPath is path to the file representation of KeyStore which holds
1052   * collection of keys and certificates. This file can be PKCS12 type (file
1053   * with .p12 or .pfx extension) or can be key store of other types readable
1054   * by 'BouncyCastle' or 'Sun' KeyStore implementation.
1055   * @param ksType is type of KeyStore. It can be one of the following types:
1056   * JKS for 'Sun' KeyStore, 'BKS', 'PKCS12' or 'UBER') for 'BouncyCastle'
1057   * KeyStore. If ksType is given as null it will be assumed that .cer file is
1058   * in use, and alias parameter will be ignored, so this method becomes
1059   * equivalent to addRecipient() method which deal only with .cer files.
1060   * @param password password used to access the corresponding private key,
1061   * stored in given KeyStore file.
1062   * @param alias alias name which corresponds to desired private key. If alias
1063   * is given as null, then reading results are unpredictable.
1064   * to recipient (file with .cer extension)
1065   * @exception SMIMEException if type of addressing of messages is not TO, CC
1066   * or BCC. Also, it can be caused by non SMIMEException which can be one of
1067   * the following: IOException, MessagingException, FileNotFoundException,
1068   * NoSuchProviderException, CertificateException.
1069   */

1070  protected void addRecipient (String JavaDoc recipientAddress, String JavaDoc type, String JavaDoc ksPath,
1071                               String JavaDoc ksType, String JavaDoc password, String JavaDoc alias ) throws SMIMEException
1072  {
1073    try {
1074      char[] paswCh = password.toCharArray();
1075      File JavaDoc fks = new File JavaDoc(ksPath);
1076      if (! (fks.exists() && fks.isFile()))
1077        throw new SMIMEException(this, 1034);
1078
1079      if (ksPath != null) {
1080        if (ksType == null) // assumed cer file
1081
this.addRecipient(recipientAddress, type, ksPath);
1082        else {
1083          FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fks);
1084          KeyStore JavaDoc kStore = KeyStore.getInstance(ksType);
1085          kStore.load(fis, paswCh);
1086          fis.close();
1087
1088          this.addRecipient(recipientAddress, type, kStore, alias);
1089        }
1090      }
1091      else
1092          this.addRecipient(recipientAddress, type, null, null);
1093    }
1094    catch(Exception JavaDoc e) {
1095      throw SMIMEException.getInstance(this, e, "addRecipient");
1096    }
1097  }
1098
1099
1100
1101  /**
1102   * Sets REPLY TO field in message header.
1103   * @param replyAddress email address used in reply
1104   * @exception SMIMEException caused by non SMIMEException which is
1105   * MessagingException. Also, javax.mail.internet.AddressException is thrown
1106   * from instances of InternetAddress class (but AddressException extends
1107   * MessagingException).
1108   */

1109  public void setReply (String JavaDoc replyAddress) throws SMIMEException
1110  {
1111    try {
1112      InternetAddress JavaDoc reply[] = new InternetAddress JavaDoc[1];
1113      reply[0] = new InternetAddress JavaDoc(replyAddress);
1114      message.setReplyTo(reply);
1115    }
1116    catch(Exception JavaDoc e) {
1117      throw SMIMEException.getInstance(this, e, "addRecipient");
1118    }
1119  }
1120
1121  /**
1122   * Returns MimeMessage.
1123   * @return Signed S/MIME message
1124   */

1125  public MimeMessage JavaDoc getMimeMessage() {
1126    return message;
1127  }
1128
1129
1130  /**
1131   * Resets all attributes in BaseSMIMEObject to their initial values. The
1132   * attributes have the same values as when simple construcor is invoked. It
1133   * means that after this method call, MIME message object should be rebuild
1134   * again by calling initMimeMessage() method and optionaly other setContent()
1135   * and addAttachment() method calls.
1136   */

1137  public void reset() {
1138    this.contentPresence = false;
1139    this.externalMessagePresence = false;
1140    this.bodyPartArray.removeAllElements();
1141    this.certArray .removeAllElements();
1142    this.indicatorTo = false;
1143  }
1144
1145  /**
1146   * Sends S/MIME message to SMTP host.
1147   * @exception MessagingException caused by use of methods from objects of class
1148   * Transport.
1149   */

1150  public void send() throws MessagingException JavaDoc{
1151    Transport.send(message);
1152  }
1153
1154}
1155
Popular Tags