0001: /*
0002: * Title: Oyster Project
0003: * Description: S/MIME email sending capabilities
0004: * @Author Vladimir radisic
0005: * @Version 2.1.6
0006: */
0007:
0008: package org.enhydra.oyster.smime;
0009:
0010: import org.enhydra.oyster.exception.SMIMEException;
0011: import org.enhydra.oyster.activation.StreamDataSource;
0012: import org.enhydra.oyster.mail.MultipartGenerator;
0013: import org.enhydra.oyster.util.ConvertAssist;
0014: import org.enhydra.oyster.util.PFXUtils;
0015: import org.enhydra.oyster.mail.ContentAnalyzer;
0016: import org.enhydra.oyster.crypto.consts.KeyStoreConstants;
0017: import javax.mail.Session;
0018: import javax.mail.Transport;
0019: import javax.mail.MessagingException;
0020: import javax.mail.internet.MimeMessage;
0021: import javax.mail.internet.MimeBodyPart;
0022: import javax.mail.internet.MimeMultipart;
0023: import javax.mail.internet.InternetAddress;
0024: import javax.activation.DataHandler;
0025: import javax.activation.FileDataSource;
0026: import javax.activation.MimetypesFileTypeMap;
0027: import java.util.Vector;
0028: import java.util.Properties;
0029: import java.io.File;
0030: import java.io.InputStream;
0031: import java.io.FileInputStream;
0032: import java.io.ByteArrayInputStream;
0033: import java.security.Security;
0034: import sun.security.provider.Sun;
0035: import org.bouncycastle.jce.provider.BouncyCastleProvider;
0036: import java.security.KeyStore;
0037: import java.security.cert.X509Certificate;
0038: import java.security.cert.CertificateFactory;
0039:
0040: import java.io.*;
0041:
0042: /**
0043: * This class is used as super class for all four email types with SMIME
0044: * cryptography capabilities (Enveloped, Signed Signed and Enveloped and Enveloped
0045: * and Signed). It contains the common methods for all this types. Also, this
0046: * class is used as a super class for class PureMIME which can be used in creation
0047: * and sending of pure MIME messages withouth cryptography posibilities.
0048: */
0049: public class BaseSMIMEObject implements KeyStoreConstants {
0050:
0051: /**
0052: * Container for MIME message
0053: */
0054: protected MimeMessage message;
0055:
0056: /**
0057: * Indicator of the email message content part presence (it can be plain text or
0058: * html content). Other part of message contains attachements.
0059: */
0060: protected boolean contentPresence = false;
0061:
0062: /**
0063: * Indicator of the external MimeMessage object presence. This parameter is true
0064: * when constructor with MimeMessage object is used.
0065: */
0066: protected boolean externalMessagePresence = false;
0067:
0068: /**
0069: * Storage for MIME bodyparts
0070: */
0071: protected Vector bodyPartArray = new Vector(0, 1);
0072:
0073: /**
0074: * Storage for .cer files coresponding to appropriate enveloping session
0075: */
0076: protected Vector certArray = new Vector(0, 1);
0077:
0078: /**
0079: * Indication that at least one recipient must be TO (others may be CC or BCC)
0080: */
0081: protected boolean indicatorTo = false;
0082:
0083: /**
0084: * Character set definition. The given Unicode strings will be charset-encoded
0085: * by using theis attribute. The charset is also used to set the "charset"
0086: * parameter. For example German letters should be encoded by usage of
0087: * 'ISO-8859-1' charset. If charset parameter is null and subject or content
0088: * contains non US-ASCII characters, it will be encoded using the platform's
0089: * default charset. For more information about character sets refer to
0090: * 'Character Encodings' chapter of package java.lang Java documentation page,
0091: * or to RFC2278.
0092: */
0093: protected String charsetEnc = null;
0094:
0095: /**
0096: * Simple constructor. Dynamically loads the BC and SUN provider necessary for
0097: * cryptography processing. This constructor does not create MIME message
0098: * object, so it is obligatory to invoke initMimeMessage() method after this
0099: * constructor.
0100: */
0101: protected BaseSMIMEObject() {
0102: Security.addProvider(new BouncyCastleProvider()); // Dynamic loading the BC provider
0103: Security.addProvider(new Sun()); // Dynamic loading the SUN provider necessary for SHA1withRSA
0104: }
0105:
0106: /**
0107: * Initializes the JavaMail session for SMTP and the MimeMessage object for message
0108: * which will be sent. Dynamically loads the BC and SUN provider necessary for
0109: * cryptography processing. This constructor is used for creating message with
0110: * text/plain content. For creating html formated content (text/html), other
0111: * constructor should be used in combination with one of setContent methods.
0112: * Note that after using this constructor setContent method can be used only
0113: * if "content" argument of constructor was given as null, otherwise setContent
0114: * method can't be used because content is already set as text/plain.
0115: * @param smtpHost name of SMTP host used for sending email
0116: * @param fromAddress email address of sender (FROM field in email header)
0117: * @param subject subject of email (SUBJECT field in email header). This
0118: * argument can be null, but email message will be sent withouth SUBJECT.
0119: * @param content text/plain content of email message. This argument can be
0120: * null, but later one of setContent() methods or one of addAttachment()
0121: * methods should be called
0122: * @param charset character set for passed subject and content. The given
0123: * Unicode string will be charset-encoded using the specified charset. The
0124: * charset is also used to set the "charset" parameter. For example German
0125: * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
0126: * parameter is null and subject or content contains non US-ASCII characters,
0127: * it will be encoded using the platform's default charset.
0128: * @exception SMIMEException if smtpHost or fromAddress parameters are null.
0129: * Also, it can be caused by non SMIMEException which is MessagingException.
0130: */
0131: protected BaseSMIMEObject(String smtpHost, String fromAddress,
0132: String subject, String content, String charset)
0133: throws SMIMEException {
0134: this ();
0135: try {
0136: initMimeMessage(smtpHost, fromAddress, subject, content,
0137: charset);
0138: } catch (Exception e) {
0139: throw new SMIMEException(e);
0140: }
0141: }
0142:
0143: /**
0144: * Initializes the JavaMail session for SMTP and the MimeMessage object for message
0145: * which will be sent. Dynamically loads the BC and SUN provider necessary for
0146: * cryptography processing. This constructor does not create content of message
0147: * and it can be set later with one of setContent methods. Also, message can be
0148: * left withouth content, but then at least one attachement must be added.
0149: * @param smtpHost name of SMTP host used for sending email
0150: * @param fromAddress email address of sender (FROM field in email header)
0151: * @param subject subject of email (SUBJECT field in email header). This
0152: * argument can be null, but email message will be sent withouth SUBJECT.
0153: * @param charset character set for passed subject and content. The given
0154: * Unicode string will be charset-encoded using the specified charset. The
0155: * charset is also used to set the "charset" parameter. For example German
0156: * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
0157: * parameter is null and subject or content contains non US-ASCII characters,
0158: * it will be encoded using the platform's default charset.
0159: * @exception SMIMEException if smtpHost or fromAddress parameters are null.
0160: * Also, it can be caused by non SMIMEException which is MessagingException.
0161: */
0162: protected BaseSMIMEObject(String smtpHost, String fromAddress,
0163: String subject, String charset) throws SMIMEException {
0164: this (smtpHost, fromAddress, subject, null, charset);
0165: }
0166:
0167: /**
0168: * Construction of message with external prepared MimeMessage object. Usage of
0169: * this constructor disables usage of setContent() and addAttachment() methods.
0170: * Also, all recipients (TO, CC or BCC type) must be declared again via
0171: * setRecipient() method, even if they were previously set. Be very carefull
0172: * with usage of this constructor because all MimeBodyPart objects and
0173: * MimeMultipart objects used in construction of given MimeMessage object,
0174: * must have correct defined Content header arguments, and contents. Contents
0175: * must be formed in format which can be recognised and appropriate interpreted
0176: * in the process of sending mail. If there is any special content object
0177: * added to MimeBodyPart object or MimeMultipart object, the appropriate
0178: * DataContent handler must be created for that object and set to corresponding
0179: * BodyPart.
0180: * @param mimeMessage external created MimeMessage object
0181: * @exception SMIMEException if smtpHost or fromAddress parameter is null.
0182: * Also, it can be caused by non SMIMEException which is MessagingException.
0183: */
0184: protected BaseSMIMEObject(MimeMessage mimeMessage)
0185: throws SMIMEException {
0186: try {
0187: Security.addProvider(new BouncyCastleProvider()); // Dynamic loading the BC provider
0188: Security.addProvider(new Sun()); // Dynamic loading the SUN provider necessary for SHA1withRSA
0189:
0190: this .message = new MimeMessage(mimeMessage);
0191: message.removeHeader("TO");
0192: message.removeHeader("CC");
0193: message.removeHeader("BCC");
0194:
0195: externalMessagePresence = true;
0196: } catch (Exception e) {
0197: throw new SMIMEException(e);
0198: }
0199: }
0200:
0201: /**
0202: * Initializes the JavaMail session for SMTP and the MimeMessage object for
0203: * message which will be sent. If the argument 'content' is not given as null
0204: * this method is used for creating new message with text/plain content. For
0205: * creating html formated content (text/html), argument 'content' should be
0206: * given as null, and content of message should be set with one of setContent
0207: * methods. Note that after using initMimeMessage method, setContent method
0208: * can be used only if 'content' argument of constructor was given as null,
0209: * otherwise setContent method can't be used because content is already set as
0210: * text/plain.
0211: * @param smtpHost name of SMTP host used for sending email
0212: * @param fromAddress email address of sender (FROM field in email header)
0213: * @param subject subject of email (SUBJECT field in email header). This
0214: * argument can be null, but email message will be sent withouth SUBJECT.
0215: * @param content text/plain content of email message
0216: * @param charset character set for passed subject and content. The given
0217: * Unicode string will be charset-encoded using the specified charset. The
0218: * charset is also used to set the "charset" parameter. For example German
0219: * letters should be encoded by usage of 'ISO-8859-1' charset. If 'charset'
0220: * argument is null and subject or content contains non US-ASCII characters,
0221: * it will be encoded using the platform's default charset.
0222: * @exception SMIMEException if smtpHost or fromAddress parameters are null.
0223: * Also, it can be caused by non SMIMEException which is MessagingException.
0224: */
0225: public void initMimeMessage(String smtpHost, String fromAddress,
0226: String subject, String content, String charset)
0227: throws SMIMEException {
0228: if (smtpHost == null | fromAddress == null)
0229: throw new SMIMEException(1041);
0230:
0231: this .charsetEnc = charset;
0232:
0233: Properties sesProp = new Properties();
0234: sesProp.setProperty("mail.smtp.host", smtpHost);
0235: Session ses = Session.getInstance(sesProp);
0236: message = new MimeMessage(ses);
0237: try {
0238: InternetAddress from = new InternetAddress(fromAddress);
0239: message.setFrom(from);
0240: if (subject != null) {
0241: if (charset != null)
0242: message.setSubject(subject, charset);
0243: else
0244: message.setSubject(subject);
0245: }
0246: if (content != null) {
0247: MimeBodyPart mbp = new MimeBodyPart();
0248: if (charset != null)
0249: mbp.setText(content, charset);
0250: else
0251: mbp.setText(content);
0252: mbp.setText(content);
0253: bodyPartArray.addElement(mbp);
0254: contentPresence = true;
0255: }
0256: } catch (Exception e) {
0257: throw new SMIMEException(e);
0258: }
0259: }
0260:
0261: /**
0262: * Sets the character set encoding. This attribute also can be set during the
0263: * construction of object, or by invoking initMimeMessage() method. By
0264: * invoking this method, previously set parameter will be overriden.
0265: * @param charset characterset encoding definition which will be used in
0266: * process of string to byte array (and vice versa) transformation. The
0267: * 'charset' argument is also used to set the 'charset' parameter in message
0268: * or body header. For example German letters should be encoded by usage of
0269: * 'ISO-8859-1' charset. If 'charset' argument is null and String contains non
0270: * US-ASCII characters it will be encoded using the platform's default charset.
0271: */
0272: public void setCharsetEncoding(String charset) {
0273: this .charsetEnc = charset;
0274: }
0275:
0276: /**
0277: * Sets message content. Message content can be given in two differrent forms:
0278: * text and html code. If content is type of text, parameter "type" should be
0279: * "text/plain" and other two parameters are not in use (should be set to null).
0280: * If content is type of html code, parameter "type" should be set as "text/html",
0281: * otherwise (if it is set as "text/plain") html code is processed as plain
0282: * text. This method can be performed only once.<BR>
0283: * <BR>
0284: * In case of html content, it is essential to (on appropriate way) associate some
0285: * attributes of particular elements in html code ("background" or "src" atributes)
0286: * with corresponding ressources (URL-s, relative file addresses or byte array
0287: * streams). This resources should all be sent with message to enable recipient
0288: * to see complete html message. Location of resources can be given in few
0289: * different forms and depending on that, allocation resolving can be successful or
0290: * not. Following text represents different possibilities for defining locations
0291: * of resources (pictures, animations, sound...) inside of html code passed to
0292: * this method, and necessery passed additional parameters used for resolving
0293: * this resource locations.<BR>
0294: * <BR>
0295: * <UL>
0296: * <LI>URL defined as: http://... is left unchanged. This resource is not sent
0297: * with the message and it couldn't be seen by recipient if it is not online on
0298: * the internet.</LI>
0299: * <LI>URL defined as: file://... is transformed to corresponding Content ID if
0300: * the resource can be found on specified location and it is sent with message.</LI>
0301: * <LI>Absolute path, for example defined as: "c:\tmp\test\picture.bmp", is
0302: * transformed to corresponding Content ID if the resource can be found on
0303: * specified location, and is sent with message. If all resources in html
0304: * code are specified with its absolute path, the 3rd parameter in this method
0305: * can be null.</LI>
0306: * <LI>Relative path of all resources specified in html code, for example
0307: * defined as: ".\test\picture.bmp" and ".\example\flush.swf", must be defined
0308: * to be relative to same directory path (in this case it is c:\tmp). This parameter
0309: * (common directory path) is given as 3rd parameter in this method, and is named
0310: * "path". If html code is obtained from .html file, necessery common directory
0311: * path is usually path to this .html file. Location of resource is transformed
0312: * to corresponding Content ID if the resource can be found on specified location.
0313: * This location is sent with the message.</LI>
0314: * <LI>Byte array stream as resource for html attribute must be referenced from
0315: * html code as: <BR>
0316: * <BR><PRE>
0317: * *****nnn<virtual_file_name><BR>
0318: * <BR></PRE>
0319: * Five '*' characters (must be five) define that it is resource expected from
0320: * the stream. Other three characters must be digits (000-999) and represent
0321: * index of corresponding stream in stream array. virtual_file_name is name and
0322: * extension assigned to data passed from stream. Name is used in construction of
0323: * "name" parameter in Content-Type, while extension of file name is used in
0324: * detection of appropriate mime-type. Lenght of virtual_file_name is not
0325: * important. If there is no data referenced from byte array stream ,4th
0326: * parameter of this method named "resources" can be null. Also, if all resources
0327: * are passed through the array of streams, 3th parameter ("path") can be null.
0328: * Location of resource is transformed to corresponding Content ID if no error
0329: * has occured during the process of allocation.</LI>
0330: * </UL>
0331: * <BR>
0332: * All mentioned resource allocation types can be combined together in the same
0333: * html code, and all will be processed with appropriate use of this method.<BR>
0334: * <BR>
0335: * Note that number of resource references that are defined in html code by
0336: * using virtual_file_names must be greater than or equal to number of elements
0337: * in array of InputStream (4th parameter). If one resource (one element in array
0338: * of IputStream) is used in html code more than once, it is advisable to use
0339: * same virtual_file_name in html code because message is then sent with only
0340: * one attached resource (image, movie...). It is essetntial that desired resource
0341: * in input stream array corresponds to specified "nnn" part of virtual_file_name.<BR>
0342: * <BR>
0343: * If resources specified on any described name can not be found or resolved,
0344: * or if any exception has occured during its processing, they won't be added and
0345: * html message will be sent withouth them.
0346: * @param content String representation of message content (text or html code).
0347: * This argument is mandatory.
0348: * @param type type of given content. It can take values: "text/plain" or
0349: * "text/html". This argument is mandatory.
0350: * @param path common directory path for relative file locations in html code.
0351: * It can be null if all resources set absolute path or are defined by
0352: * byte array streams, or if sending resources with relative address it is not desired.
0353: * Also, it is set to null in case of text/plain message.
0354: * @param resources way for representing resources used in the given html code
0355: * which will be added to message as array of InputStream. Detail use
0356: * of this argument is described above. It can be null if no resources as byte
0357: * array stream are used, or if sending resources given in that way is not desired.
0358: * Also, it is set to null in case of text/plain message.
0359: * @param externalPlainText external text/plain message represented as String.
0360: * This argument is used as alternative message to given html message in the process
0361: * of generation multipart/alternative MimeMultipart message. If this argument
0362: * has value null then autogeneration of text/plain message is performed
0363: * according to passed html code (content argument). Also, it is set to null
0364: * in case of text/plain message.
0365: * @exception SMIMEException if content is tried to be added twice, or in case of
0366: * wrong "type" parameter. Also, it can be caused by non SMIMEException which can
0367: * be one of the following: MessagingException UnsoportedEncodingException.
0368: */
0369: public void setContent(String content, String type, String path,
0370: InputStream[] resources, String externalPlainText)
0371: throws SMIMEException {
0372:
0373: if (content != null) {
0374: ByteArrayInputStream bais = null;
0375: try {
0376: if (this .charsetEnc != null)
0377: bais = new ByteArrayInputStream(content
0378: .getBytes(this .charsetEnc));
0379: else
0380: bais = new ByteArrayInputStream(content
0381: .getBytes("ISO-8859-1"));
0382: } catch (Exception e) {
0383: throw new SMIMEException(e);
0384: }
0385: this .setContent(bais, type, path, resources,
0386: externalPlainText);
0387:
0388: } else
0389: throw new SMIMEException(1035);
0390: }
0391:
0392: /**
0393: * Sets message content from String. This method can be performed only once.
0394: * Method is the same as corresponding method setContent with five parameters,
0395: * where fifth parameter is set to null. This setContent method (in case that second
0396: * argument - type has value text/html) is used with autogeneration of text/plain
0397: * message according to given html code in the process of generation
0398: * multipart/alternative MimeMultipart message. For further information refer
0399: * to setContent method with five arguments.
0400: * @param content String representation of message content (text or html code).
0401: * This argument is mandatory.
0402: * @param type type of given content. It can take values: "text/plain" or
0403: * "text/html". This argument is mandatory.
0404: * @param path common directory path for relative file locations in html code.
0405: * It can be null if all resources set absolute path or are defined by
0406: * byte array streams, or if sending resources with relative address it is not desired.
0407: * Also, it is set to null in case of text/plain message.
0408: * @param resources way for representing resources used in the given html code
0409: * which will be added to message as array of InputStream. Detail use
0410: * of this argument is described above. It can be null if no resources as byte
0411: * array stream are used, or if sending resources given in that way is not desired.
0412: * Also, it is set to null in case of text/plain message.
0413: * @exception SMIMEException if content is tried to be added twice, or in case of
0414: * wrong "type" parameter. Also, it can be caused by non SMIMEException which can
0415: * be one of the following: MessagingException UnsoportedEncodingException.
0416: */
0417: public void setContent(String content, String type, String path,
0418: InputStream[] resources) throws SMIMEException {
0419:
0420: this .setContent(content, type, path, resources, null);
0421: }
0422:
0423: /**
0424: * Sets message content from InputStream. This method can be performed only once.
0425: * Message content can be given in two differrent forms: text and html code. If
0426: * content is type of text, parameter "type" should be "text/plain", while if
0427: * content is type of html code, parameter "type" should be set as "html/code".
0428: * For further information refer to setContent method with five arguments
0429: * (String, String, String, InputStream[], String) which is called by this method.
0430: * @param content message content data given from any InputStream.
0431: * Data can be text or html code and will be interpreted according to second
0432: * parameter: "type".
0433: * @param type type of given content. It can take values: "text/plain" or
0434: * "text/html". This argument is mandatory.
0435: * @param path common directory path for relative file locations in html code.
0436: * It can be null if all resources in html code have set absolute path or are
0437: * defined by byte array streams, or if sending resources with relative address
0438: * is not desired. Also, it is set to null in case of text/plain message.
0439: * @param resources way for representing resources used in the given html code
0440: * which will be added to message as array of InputStreams. Detail use
0441: * of this argument is described in other setContent methods mentioned before.
0442: * It can be null if no resources as byte array stream are used, or if sending
0443: * resources given in that way is not desired. Also, it is set to null in case
0444: * of text/plain message.
0445: * @param externalPlainText external text/plain message represented as String.
0446: * This argument is used as alternative message to given html message in the process
0447: * of generation multipart/alternative MimeMultipart message. If this argument
0448: * has value null then autogeneration of text/plain message is performed
0449: * according to passed html code (content argument). Also, it is set to null
0450: * in case of text/plain message.
0451: * @exception SMIMEException if content is tried to be added twice , in case of
0452: * wrong "type" parameter or in case when parameter content is null. Also, it can
0453: * be caused by non SMIMEException which is MessagingException.
0454: */
0455: public void setContent(InputStream content, String type,
0456: String path, InputStream[] resources,
0457: String externalPlainText) throws SMIMEException {
0458: if (contentPresence)
0459: throw new SMIMEException(1049);
0460: if (content != null) {
0461: try {
0462: if (type.equalsIgnoreCase("text/plain")) {
0463: MimeBodyPart mbp = new MimeBodyPart();
0464: String temp = null;
0465: if (this .charsetEnc != null) {
0466: temp = new String(ConvertAssist
0467: .inStreamToByteArray(content),
0468: this .charsetEnc);
0469: mbp.setText(temp, this .charsetEnc);
0470: } else {
0471: temp = new String(ConvertAssist
0472: .inStreamToByteArray(content),
0473: "ISO-8859-1");
0474: mbp.setText(temp, "ISO-8859-1");
0475: }
0476: bodyPartArray.add(0, mbp);
0477: contentPresence = true;
0478: } else if (type.equalsIgnoreCase("text/html")) {
0479: MimeMultipart htmlMultipart = MultipartGenerator
0480: .getHtmlMultipart(content, path, resources,
0481: externalPlainText);
0482: bodyPartArray.add(0, htmlMultipart);
0483: contentPresence = true;
0484: } else
0485: throw new SMIMEException(1048);
0486: } catch (Exception e) {
0487: throw new SMIMEException(e);
0488: }
0489: } else
0490: throw new SMIMEException(1035);
0491: }
0492:
0493: /**
0494: * Sets message content from InputStream. This method can be performed only once.
0495: * Method is same as corresponding method setContent with five parameters, where
0496: * fifth parameter is set to null. This setContent method (in case that second
0497: * argument - type has value text/html) is used with autogeneration of text/plain
0498: * message according to given html code in process of generation multipart/alternative
0499: * MimeMultipart message. For further information refer to setContent method with
0500: * five arguments (InputStream, String, String, InputStream[], String) which is
0501: * called by this method.
0502: * @param content message content data given from any InputStream.
0503: * Data can be text or html code and will be interpreted according to second
0504: * parameter: "type".
0505: * @param type type of given content. It can take values: "text/plain" or
0506: * "text/html". This argument is mandatory.
0507: * @param path common directory path for relative file locations in html code.
0508: * It can be null if all resources in html code have set absolute path or are
0509: * defined by byte array streams, or if sending resources with relative address
0510: * is not desired. Also, it is set to null in case of text/plain message.
0511: * @param resources way for representing resources used in the given html code
0512: * which will be added to message as array of InputStreams. Detail use
0513: * of this argument is described in other setContent methods mentioned before.
0514: * It can be null if no resources as byte array stream are used, or if sending
0515: * resources given in that way is not desired. Also, it is set to null in case
0516: * of text/plain message.
0517: * @exception SMIMEException if content is tried to be added twice , in case of
0518: * wrong "type" parameter or in case when parameter content is null. Also, it can
0519: * be caused by non SMIMEException which is MessagingException.
0520: */
0521: public void setContent(InputStream content, String type,
0522: String path, InputStream[] resources) throws SMIMEException {
0523: this .setContent(content, type, path, resources, null);
0524: }
0525:
0526: /**
0527: * Sets message content from InputStream. This method can be performed only once.
0528: * Message content can be given in two differrent forms: text and html code. If
0529: * content is type of text, parameter "type" should be "text/plain", while if
0530: * content is type of html code, parameter "type" should be set as "html/code".
0531: * If html code content is set by this method, message will be generated withouth
0532: * inclusion of the resources defined in paricular html element's attribute ("src" and
0533: * "background"). Only plain html code will be sent and any reference to local
0534: * file system resources will be useless for recipient of the message. HTTP referenced
0535: * resources can still be available if recipient is online on Internet. Message
0536: * generated on this way is smaller so encrypting process should be faster.
0537: * @param content message content data given from any InputStream.
0538: * Data could be text or html code and will be interpreted according to second
0539: * parameter: "type". This argument is mandatory.
0540: * @param type type of given content. It can take values: "text/plain" or
0541: * "text/html". This argument is mandatory.
0542: * @param externalPlainText external text/plain message represented as String.
0543: * This argument is used as alternative message to given html message in the process
0544: * of generation multipart/alternative MimeMultipart message. If this argument
0545: * has value null then autogeneration of text/plain message is performed
0546: * according to passed html code (content argument). Also, it is set to null
0547: * in case of text/plain message.
0548: * @exception SMIMEException if content is tried to be added twice , in case of
0549: * wrong "type" parameter or in case when parameter content is null. Also, it can
0550: * be caused by non SMIMEException which is MessagingException.
0551: */
0552: public void setContent(InputStream content, String type,
0553: String externalPlainText) throws SMIMEException {
0554: if (contentPresence)
0555: throw new SMIMEException(1049);
0556: if (content != null) {
0557: try {
0558: if (type.equalsIgnoreCase("text/plain")) {
0559: MimeBodyPart mbp = new MimeBodyPart();
0560: String temp = null;
0561: if (this .charsetEnc != null) {
0562: temp = new String(ConvertAssist
0563: .inStreamToByteArray(content),
0564: this .charsetEnc);
0565: mbp.setText(temp, this .charsetEnc);
0566: } else {
0567: temp = new String(ConvertAssist
0568: .inStreamToByteArray(content),
0569: "ISO-8859-1");
0570: mbp.setText(temp, "ISO-8859-1");
0571: }
0572:
0573: bodyPartArray.add(0, mbp);
0574: contentPresence = true;
0575: } else if (type.equalsIgnoreCase("text/html")) {
0576: MimeMultipart htmlMultipart = MultipartGenerator
0577: .getHtmlMultipart(content);
0578: bodyPartArray.add(0, htmlMultipart);
0579: contentPresence = true;
0580: } else
0581: throw new SMIMEException(1048);
0582: } catch (Exception e) {
0583: throw new SMIMEException(e);
0584: }
0585: } else
0586: throw new SMIMEException(1035);
0587: }
0588:
0589: /**
0590: * Sets message content from InputStream. This method can be performed only once.
0591: * Method is the same as corresponding method setContent with three parameters,
0592: * where third parameter is set to null. This setContent method (in case that second
0593: * argument - type has value text/html) is used with autogeneration of text/plain
0594: * message according to given html code in the process of generation
0595: * multipart/alternative MimeMultipart message. For further information refer
0596: * to setContent method with thre arguments.
0597: * @param content message content data given from any InputStream.
0598: * Data could be text or html code and will be interpreted according to second
0599: * parameter: "type". This argument is mandatory.
0600: * @param type type of given content. It can take values: "text/plain" or
0601: * "text/html". This argument is mandatory.
0602: * @exception SMIMEException if content is tried to be added twice , in case of
0603: * wrong "type" parameter or in case when parameter content is null. Also, it can
0604: * be caused by non SMIMEException which is MessagingException.
0605: */
0606: public void setContent(InputStream content, String type)
0607: throws SMIMEException {
0608: this .setContent(content, type, null);
0609: }
0610:
0611: /**
0612: * Sets message content from String. This method can be performed only once.
0613: * Message content can be given in two differrent forms: text and html code. If
0614: * content is type of text, parameter "type" should be "text/plain", while if
0615: * content is type of html code, parameter "type" should be set as "html/code".
0616: * If html code content is set by this method, message will be generated withouth
0617: * inclusion of the resources defined in paricular html element's attribute ("src" or
0618: * "background"). Only plain html code will be sent and any reference to local
0619: * file system resources will be useless for recipient of the message. HTTP referenced
0620: * resources can still be available if recipient is online on Internet. Message
0621: * generated on this way is smaller, so encrypting process should be faster.
0622: * @param content message content data given as String which can
0623: * be text or html code and will be interpreted according to second parameter:
0624: * "type". This argument is mandatory.
0625: * @param type type of given content. It can take values: "text/plain" or
0626: * "text/html". This argument is mandatory.
0627: * @param externalPlainText external text/plain message represented as String.
0628: * This argument is used as alternative message to given html message in the process
0629: * of generation multipart/alternative MimeMultipart message. If this argument
0630: * has value null then autogeneration of text/plain message is performed
0631: * according to passed html code (content argument). Also, it is set to null
0632: * in case of text/plain message.
0633: * @exception SMIMEException if content is tried to be added twice, or in case of
0634: * wrong "type" parameter. Also, it can be caused by non SMIMEException which can
0635: * be one of the following: MessagingException UnsoportedEncodingException.
0636: */
0637: public void setContent(String content, String type,
0638: String externalPlainText) throws SMIMEException {
0639:
0640: if (content != null) {
0641: ByteArrayInputStream bais = null;
0642: try {
0643: if (this .charsetEnc != null)
0644: bais = new ByteArrayInputStream(content
0645: .getBytes(this .charsetEnc));
0646: else
0647: bais = new ByteArrayInputStream(content
0648: .getBytes("ISO-8859-1"));
0649: } catch (Exception e) {
0650: throw new SMIMEException(e);
0651: }
0652: this .setContent(bais, type);
0653:
0654: } else
0655: throw new SMIMEException(1035);
0656: }
0657:
0658: /**
0659: * Sets message content from String. This method can be performed only once.
0660: * Method is same as corresponding method setContent with three parameters,
0661: * where third parameter is set to null. This setContent method (in case that second
0662: * argument - type has value text/html) is used with autogeneration of text/plain
0663: * message according to given html code in the process of generation
0664: * multipart/alternative MimeMultipart message. For further information refer
0665: * to setContent method with three arguments.
0666: * @param content message content data given as String which can
0667: * be text or html code and will be interpreted according to second parameter:
0668: * "type". This argument is mandatory.
0669: * @param type type of given content. It can take values: "text/plain" or
0670: * "text/html". This argument is mandatory.
0671: * @exception SMIMEException if content is tried to be added twice , in case of
0672: * wrong "type" parameter or in case when parameter content is null. Also, it can
0673: * be caused by non SMIMEException which is MessagingException.
0674: */
0675: public void setContent(String content, String type)
0676: throws SMIMEException {
0677:
0678: this .setContent(content, type, null);
0679: }
0680:
0681: /**
0682: * Sets message content from file represented by File object. This method can be
0683: * performed only once. Message content can be given in two differrent forms:
0684: * text and html code. If content is type of text, parameter "type" should be
0685: * "text/plain", while if content is type of html code, parameter "type" should
0686: * be set as "html/code". Note that for this method, location of resources
0687: * needed by html file (pictures, sounds... ) shouldn't be defined in html code
0688: * as they should be passed as InputStream arrays. For further information refer
0689: * to setContent method with five arguments (String, String, String, InputStream[], String)
0690: * which is called by this method.
0691: * @param inFile location of file which is used for content of the message.
0692: * This argument is mandatory.
0693: * @param type type of given content. It can take values: "text/plain" or
0694: * "text/html". This argument is mandatory.
0695: * @param externalPlainText external text/plain message represented as String.
0696: * This argument is used as alternative message to given html message in the process
0697: * of generation multipart/alternative MimeMultipart message. If this argument
0698: * has value null then autogeneration of text/plain message is performed
0699: * according to passed html code (content argument). Also, it is set to null
0700: * in case of text/plain message.
0701: * @exception SMIMEException if content is tried to be added twice, in case of
0702: * wrong "type" parameter, or if passed file (as File object) does not exist in
0703: * file sistem. Also, it can be caused by non SMIMEException which can be one of
0704: * the following: MessagingException or IOException.
0705: */
0706: public void setContent(File inFile, String type,
0707: String externalPlainText) throws SMIMEException {
0708:
0709: if (contentPresence)
0710: throw new SMIMEException(1049);
0711: if (inFile != null && inFile.exists()) {
0712: try {
0713: File inFileAbs = inFile.getAbsoluteFile()
0714: .getCanonicalFile();
0715: String content = ConvertAssist.fileToString(inFileAbs);
0716: this .setContent(content, type, inFile.getParent(),
0717: null, externalPlainText);
0718: } catch (Exception e) {
0719: throw new SMIMEException(e);
0720: }
0721: } else
0722: throw new SMIMEException(1034);
0723: }
0724:
0725: /**
0726: * Sets message content from file represented by File object. This method can be
0727: * performed only once. Method is the same as corresponding method setContent with
0728: * three parameters, where third parameter is set to null. This setContent
0729: * method (in case that second argument - type has value text/html) is used
0730: * with autogeneration of text/plain message according to given html code in
0731: * the process of generation multipart/alternative MimeMultipart message. For further
0732: * information refer to corresponding setContent method with thre arguments.
0733: * @param inFile location of file which is used for content of the message.
0734: * This argument is mandatory.
0735: * @param type type of given content. It can take values: "text/plain" or
0736: * "text/html".
0737: * @exception SMIMEException if content is tried to be added twice , in case of
0738: * wrong "type" parameter or in case when parameter content is null. Also, it can
0739: * be caused by non SMIMEException which is MessagingException.
0740: */
0741: public void setContent(File inFile, String type)
0742: throws SMIMEException {
0743: this .setContent(inFile, type, null);
0744: }
0745:
0746: /**
0747: * Adds file as attachment to email message. Content-Type will be automatically
0748: * detected. Note that for linux files, if they are recognised as files with
0749: * text/... MIME Content-Type, line breaks (LF) will be transformed to Windows
0750: * line breaks (CRLF). Same transformation will be performed to single Carriage
0751: * Return (CR) character. If linux textual file should be transported via
0752: * email untouched, the best solution is to force usage of BASE64 encoding with
0753: * other addAttachment() method.
0754: * @param fileName path and file name used for attachment
0755: * @exception SMIMEException if passed file (as File object) does not exist in
0756: * the file sistem. Also, it can be caused by non SMIMEException which is
0757: * MessagingException
0758: */
0759: public void addAttachment(String fileName) throws SMIMEException {
0760: File fn = new File(fileName);
0761: this .addAttachment(fn);
0762: }
0763:
0764: /**
0765: * Adds file as attachment to email message with given value for prefered
0766: * Content-Transfer-Encoding argument. Attachment will be encoded acording to
0767: * this parameter. Be carful with usage of this method, because you must know
0768: * nature and content of file well, to avoid errors in encoding. Since this
0769: * method does not employ automatic detection of Content-Transfer-Encoding,
0770: * it is sligtly faster. Note that for linux textual files, if they are encoded
0771: * as quoted-printable, line breaks (LF) will be transformed to Windows line
0772: * breaks (CRLF). Same transformation will be performed to single Carriage
0773: * Return (CR) character. If linux textual file should be transported
0774: * via email untouched, the best solution is to force usage of BASE64 encoding.
0775: * @param fileName path and file name used for attachment
0776: * @param encoding Content-Transfer-Encoding value. It can take values: 7bit,
0777: * quoted-printable, and base64
0778: * @exception SMIMEException if passed file (as File object) does not exist in
0779: * the file sistem. Also, it can be caused by non SMIMEException which is
0780: * MessagingException
0781: */
0782: public void addAttachment(String fileName, String encoding)
0783: throws SMIMEException {
0784: File fn = new File(fileName);
0785: this .addAttachment(fn, encoding);
0786: }
0787:
0788: /**
0789: * Adds file as attachment to email message. Content-Type will be automatically
0790: * detected. Note that for linux files, if they are recognised as files with
0791: * text/... MIME Content-Type, line breaks (LF) will be transformed to Windows
0792: * line breaks (CRLF). Same transformation will be performed to single Carriage
0793: * Return (CR) character. If linux textual file should be transported via
0794: * email untouched, the best solution is to force usage of BASE64 encoding with
0795: * other addAttachment() method.
0796: * @param file used for attachment represented as File object
0797: * @exception SMIMEException if passed file (as File object) does not exist in
0798: * the file sistem. Also, it can be caused by non SMIMEException which is
0799: * MessagingException or IOException
0800: */
0801: public void addAttachment(File file) throws SMIMEException {
0802: if (!file.exists())
0803: throw new SMIMEException(1034);
0804: try {
0805: FileInputStream fis = new FileInputStream(file);
0806: int u = fis.available();
0807: this .addAttachment(fis, file.getName());
0808: } catch (Exception e) {
0809: throw new SMIMEException(e);
0810: }
0811: }
0812:
0813: /**
0814: * Adds file as attachment to email message with given value for prefered
0815: * Content-Transfer-Encoding argument. Attachment will be encoded acording to
0816: * this parameter. Be carful with usage of this method, because you must know
0817: * nature and content of file well, to avoid errors in encoding. Since this
0818: * method does not employ automatic detection of Content-Transfer-Encoding,
0819: * it is sligtly faster. Note that for linux textual files, if they are encoded
0820: * as quoted-printable, line breaks (LF) will be transformed to Windows line
0821: * breaks (CRLF). Same transformation will be performed to single Carriage
0822: * Return (CR) character. If linux textual file should be transported
0823: * via email untouched, the best solution is to force usage of BASE64 encoding.
0824: * @param file path and file name used for attachment
0825: * @param encoding Content-Transfer-Encoding value. It can take values: 7bit,
0826: * quoted-printable, and base64
0827: * @exception SMIMEException if passed file (as File object) does not exist in
0828: * the file sistem. Also, it can be caused by non SMIMEException which is
0829: * MessagingException
0830: */
0831: public void addAttachment(File file, String encoding)
0832: throws SMIMEException {
0833: if (!file.exists())
0834: throw new SMIMEException(1034);
0835: try {
0836: FileInputStream fis = new FileInputStream(file);
0837: int u = fis.available();
0838: this .addAttachment(fis, file.getName(), encoding);
0839: } catch (Exception e) {
0840: throw new SMIMEException(e);
0841: }
0842: }
0843:
0844: /**
0845: * Adds data from InputStream as attachment to email message. Content-Type
0846: * will be automaticaly detected. Note that for linux files (virtual or real),
0847: * if they are recognised as files with text/... MIME Content-Type, line breaks
0848: * (LF) will be transformed to Windows line breaks (CRLF). Same transformation
0849: * will be performed to single Carriage Return (CR) character. If linux textual
0850: * file should be transported via email untouched, the best solution is to force
0851: * usage of BASE64 encoding with other addAttachment() method.
0852: * @param data byte array from InputStream
0853: * @param fileName virtual or real file name (wihouth path). Correct information
0854: * about name; extension of file name is especially important. Name
0855: * is used in construction of "name" parameter in Content-Type header line of
0856: * body parts of mime message. Extension of file is used in detection of the
0857: * appropriate mime-type.
0858: * @exception SMIMEException caused by non SMIMEException which is
0859: * MessagingException
0860: */
0861: public void addAttachment(InputStream data, String fileName)
0862: throws SMIMEException {
0863: MimeBodyPart attachment = new MimeBodyPart();
0864: try {
0865: StreamDataSource streamDataSrc = new StreamDataSource(data,
0866: fileName);
0867: attachment.setDataHandler(new DataHandler(streamDataSrc));
0868: attachment.setDisposition(attachment.ATTACHMENT);
0869: attachment.setFileName(fileName);
0870:
0871: ContentAnalyzer analyzer = new ContentAnalyzer();
0872: analyzer.isFor7BitEncoding(streamDataSrc.getInputStream());
0873: String encoding = analyzer.getPreferedEncoding();
0874: if (!streamDataSrc.getContentType().toLowerCase()
0875: .startsWith("text/")
0876: && encoding.equalsIgnoreCase("quoted-printable"))
0877: encoding = "base64";
0878:
0879: attachment.setHeader("Content-Transfer-Encoding", encoding);
0880:
0881: bodyPartArray.addElement(attachment);
0882: } catch (Exception e) {
0883: throw new SMIMEException(e);
0884: }
0885: }
0886:
0887: /**
0888: * Adds data from InputStream as attachment to email message with given value
0889: * for prefered Content-Transfer-Encoding argument. Attachment will be encoded
0890: * acording to this parameter. Be carful with usage of this method, because you
0891: * must know nature and content of file well, to avoid errors in encoding. Since
0892: * this method does not employ automatic detection of Content-Transfer-Encoding,
0893: * it is sligtly faster. Note that for linux textual files, if they are encoded
0894: * as quoted-printable, line breaks (LF) will be transformed to Windows line
0895: * breaks (CRLF). Same transformation will be performed to single Carriage
0896: * Return (CR) character. If linux textual file should be transported
0897: * via email untouched, the best solution is to force usage of BASE64 encoding.
0898: * @param fileName path and file name used for attachment
0899: * @param encoding Content-Transfer-Encoding value. It can take values: 7bit,
0900: * quoted-printable, and base64
0901: * @exception SMIMEException if passed file (as File object) does not exist in
0902: * the file sistem. Also, it can be caused by non SMIMEException which is
0903: * MessagingException
0904: */
0905: public void addAttachment(InputStream data, String fileName,
0906: String encoding) throws SMIMEException {
0907: MimeBodyPart attachment = new MimeBodyPart();
0908: try {
0909: StreamDataSource streamDataSrc = new StreamDataSource(data,
0910: fileName);
0911: attachment.setDataHandler(new DataHandler(streamDataSrc));
0912: attachment.setDisposition(attachment.ATTACHMENT);
0913: attachment.setFileName(fileName);
0914:
0915: if (!(encoding.equalsIgnoreCase("7bit")
0916: || encoding.equalsIgnoreCase("quoted-printable") || encoding
0917: .equalsIgnoreCase("base64")))
0918: throw new SMIMEException(1036);
0919:
0920: attachment.setHeader("Content-Transfer-Encoding", encoding);
0921:
0922: bodyPartArray.addElement(attachment);
0923: } catch (Exception e) {
0924: throw new SMIMEException(e);
0925: }
0926: }
0927:
0928: /**
0929: * Adds recipient address, type and .cer file of email recipient to encrypted
0930: * message. Certificate file should be passed only in the case of Enveloped
0931: * message or Signed and Enveloped message. Otherwise this argument represents
0932: * redundance value and it should be given as null.
0933: * @param recipientAddress email address of recipent (fields TO or CC or BCC
0934: * in email message header)
0935: * @param type should be TO, CC or BCC
0936: * @param cerFileName path and file name with certificate corresponding
0937: * to recipient (file with .cer extension)
0938: * @exception SMIMEException if type of addressing of messages is not TO, CC
0939: * or BCC. Also, it can be caused by non SMIMEException which can be one of
0940: * the following: IOException, MessagingException, FileNotFoundException,
0941: * NoSuchProviderException, CertificateException.
0942: */
0943: protected void addRecipient(String recipientAddress, String type,
0944: String cerFileName) throws SMIMEException {
0945: try {
0946: if (!type.equalsIgnoreCase("TO")
0947: && !type.equalsIgnoreCase("BCC")
0948: && !type.equalsIgnoreCase("CC"))
0949: throw new SMIMEException(1042);
0950: if (type.equalsIgnoreCase("TO")) {
0951: message.addRecipients(MimeMessage.RecipientType.TO,
0952: recipientAddress);
0953: indicatorTo = true;
0954: } else if (type.equalsIgnoreCase("CC"))
0955: message.addRecipients(MimeMessage.RecipientType.CC,
0956: recipientAddress);
0957: else if (type.equalsIgnoreCase("BCC"))
0958: message.addRecipients(MimeMessage.RecipientType.BCC,
0959: recipientAddress);
0960:
0961: if (cerFileName != null) {
0962: X509Certificate cert = null;
0963: InputStream inStream = new FileInputStream(cerFileName);
0964: CertificateFactory cf = CertificateFactory.getInstance(
0965: "X.509", "BC");
0966: cert = (X509Certificate) cf
0967: .generateCertificate(inStream);
0968: inStream.close();
0969:
0970: certArray.addElement(cert); // adding X509Certificate of .cer file in stack
0971: }
0972: } catch (Exception e) {
0973: throw new SMIMEException(e);
0974: }
0975: }
0976:
0977: /**
0978: * Adds recipient address, type and recipient's certificate via KeyStore
0979: * object and apropriate alias. Certificate information should be passed only
0980: * in the case of Enveloped message or Signed and Enveloped message. Otherwise
0981: * those arguments represent redundance values and they should be given as
0982: * null.
0983: * @param recipientAddress email address of recipent (fields TO or CC or BCC
0984: * in email message header)
0985: * @param type should be TO, CC or BCC
0986: * @param kStore instance of KeyStore class which represents an in-memory
0987: * collection of keys and certificates.
0988: * @param alias alias name which corresponds to desired certificate. If alias
0989: * is given as null, then reading results are unpredictable.
0990: * @exception SMIMEException if type of addressing of messages is not TO, CC
0991: * or BCC. Also, it can be caused by non SMIMEException which can be one of
0992: * the following: IOException, MessagingException, FileNotFoundException,
0993: * NoSuchProviderException, CertificateException.
0994: */
0995: protected void addRecipient(String recipientAddress, String type,
0996: KeyStore kStore, String alias) throws SMIMEException {
0997: try {
0998: if (!type.equalsIgnoreCase("TO")
0999: && !type.equalsIgnoreCase("BCC")
1000: && !type.equalsIgnoreCase("CC"))
1001: throw new SMIMEException(1042);
1002: if (type.equalsIgnoreCase("TO")) {
1003: message.addRecipients(MimeMessage.RecipientType.TO,
1004: recipientAddress);
1005: indicatorTo = true;
1006: } else if (type.equalsIgnoreCase("CC"))
1007: message.addRecipients(MimeMessage.RecipientType.CC,
1008: recipientAddress);
1009: else if (type.equalsIgnoreCase("BCC"))
1010: message.addRecipients(MimeMessage.RecipientType.BCC,
1011: recipientAddress);
1012:
1013: if (kStore != null) {
1014: X509Certificate cert = null;
1015: if (alias != null)
1016: cert = (X509Certificate) kStore
1017: .getCertificate(alias);
1018: else {
1019: X509Certificate[] temp509 = PFXUtils
1020: .getCertificateChain(kStore);
1021: if (temp509 != null && temp509.length > 0)
1022: cert = temp509[0];
1023: else
1024: cert = PFXUtils
1025: .getPFXOwnerX509Certificate(kStore);
1026: }
1027:
1028: certArray.addElement(cert); // adding X509Certificate of .cer file in stack
1029: }
1030:
1031: } catch (Exception e) {
1032: throw new SMIMEException(e);
1033: }
1034: }
1035:
1036: /**
1037: * Adds recipient address, type and recipient's certificate via path to the
1038: * KeyStore file, KeyStore type, password and apropriate alias. Certificate
1039: * information should be passed only in the case of Enveloped message or
1040: * Signed and Enveloped message. Otherwise those arguments represent
1041: * redundance values and they should be given as null.
1042: * @param recipientAddress email address of recipent (fields TO or CC or BCC
1043: * in email message header)
1044: * @param type should be TO, CC or BCC
1045: * @param ksPath is path to the file representation of KeyStore which holds
1046: * collection of keys and certificates. This file can be PKCS12 type (file
1047: * with .p12 or .pfx extension) or can be key store of other types readable
1048: * by 'BouncyCastle' or 'Sun' KeyStore implementation.
1049: * @param ksType is type of KeyStore. It can be one of the following types:
1050: * JKS for 'Sun' KeyStore, 'BKS', 'PKCS12' or 'UBER') for 'BouncyCastle'
1051: * KeyStore. If ksType is given as null it will be assumed that .cer file is
1052: * in use, and alias parameter will be ignored, so this method becomes
1053: * equivalent to addRecipient() method which deal only with .cer files.
1054: * @param password password used to access the corresponding private key,
1055: * stored in given KeyStore file.
1056: * @param alias alias name which corresponds to desired private key. If alias
1057: * is given as null, then reading results are unpredictable.
1058: * to recipient (file with .cer extension)
1059: * @exception SMIMEException if type of addressing of messages is not TO, CC
1060: * or BCC. Also, it can be caused by non SMIMEException which can be one of
1061: * the following: IOException, MessagingException, FileNotFoundException,
1062: * NoSuchProviderException, CertificateException.
1063: */
1064: protected void addRecipient(String recipientAddress, String type,
1065: String ksPath, String ksType, String password, String alias)
1066: throws SMIMEException {
1067: try {
1068: char[] paswCh = password.toCharArray();
1069: File fks = new File(ksPath);
1070: if (!(fks.exists() && fks.isFile()))
1071: throw new SMIMEException(1034);
1072:
1073: if (ksPath != null) {
1074: if (ksType == null) // assumed cer file
1075: this .addRecipient(recipientAddress, type, ksPath);
1076: else {
1077: FileInputStream fis = new FileInputStream(fks);
1078: KeyStore kStore = KeyStore.getInstance(ksType);
1079: kStore.load(fis, paswCh);
1080: fis.close();
1081:
1082: this .addRecipient(recipientAddress, type, kStore,
1083: alias);
1084: }
1085: } else
1086: this .addRecipient(recipientAddress, type, null, null);
1087: } catch (Exception e) {
1088: throw new SMIMEException(e);
1089: }
1090: }
1091:
1092: /**
1093: * Sets REPLY TO field in message header.
1094: * @param replyAddress email address used in reply
1095: * @exception SMIMEException caused by non SMIMEException which is
1096: * MessagingException. Also, javax.mail.internet.AddressException is thrown
1097: * from instances of InternetAddress class (but AddressException extends
1098: * MessagingException).
1099: */
1100: public void setReply(String replyAddress) throws SMIMEException {
1101: try {
1102: InternetAddress reply[] = new InternetAddress[1];
1103: reply[0] = new InternetAddress(replyAddress);
1104: message.setReplyTo(reply);
1105: } catch (Exception e) {
1106: throw new SMIMEException(e);
1107: }
1108: }
1109:
1110: /**
1111: * Returns MimeMessage.
1112: * @return Signed S/MIME message
1113: */
1114: public MimeMessage getMimeMessage() {
1115: return message;
1116: }
1117:
1118: /**
1119: * Resets all attributes in BaseSMIMEObject to their initial values. The
1120: * attributes have the same values as when simple construcor is invoked. It
1121: * means that after this method call, MIME message object should be rebuild
1122: * again by calling initMimeMessage() method and optionaly other setContent()
1123: * and addAttachment() method calls.
1124: */
1125: public void reset() {
1126: this .contentPresence = false;
1127: this .externalMessagePresence = false;
1128: this .bodyPartArray.removeAllElements();
1129: this .certArray.removeAllElements();
1130: this .indicatorTo = false;
1131: }
1132:
1133: /**
1134: * Sends S/MIME message to SMTP host.
1135: * @exception MessagingException caused by use of methods from objects of class
1136: * Transport.
1137: */
1138: public void send() throws MessagingException {
1139: Transport.send(message);
1140: }
1141:
1142: }
|