001: /*
002: * Title: Oyster Project
003: * Description: S/MIME email sending capabilities
004: * @Author Vladan Obradovic, Vladimir Radisic
005: * @Version 2.1.6
006: */
007:
008: package org.enhydra.oyster.mail;
009:
010: import org.enhydra.oyster.smime.BaseSMIMEObject;
011: import org.enhydra.oyster.exception.SMIMEException;
012: import javax.mail.Multipart;
013: import javax.mail.internet.MimeMessage;
014: import javax.mail.internet.MimeBodyPart;
015: import javax.mail.internet.MimeMultipart;
016: import java.util.SimpleTimeZone;
017: import java.util.GregorianCalendar;
018:
019: /**
020: * PureMIME class is used for creating and sending pure (clear) MIME message
021: * (withouth any cryptographic posibilities). Initialization, creating and sending
022: * of pure (clear) MIME message has the same functionalities as S/MIME type of
023: * message.
024: */
025: public class PureMIME extends BaseSMIMEObject {
026:
027: /**
028: * Initializes the JavaMail session for SMTP and the MimeMessage object for message
029: * which will be sent. Dynamically loads the BC and SUN provider necessary for
030: * cryptography processing. This constructor is used for creating message with
031: * text/plain content. For creating html formated content (text/html), other
032: * constructor should be used in combination with one of setContent methods.
033: * Note that after using this constructor setContent method can be used only
034: * if "content" argument of constructor was given as null, otherwise setContent
035: * method can't be used because content is already set as text/plain.
036: * @param smtpHost name of SMTP host used for sending email
037: * @param fromAddress email address of sender (FROM field in email header)
038: * @param subject subject of email (SUBJECT field in email header). This
039: * argument can be null, but email message will be sent withouth SUBJECT.
040: * @param content text/plain content of email message. This argument can be
041: * null, but later one of setContent() methods or one of addAttachment()
042: * methods should be called
043: * @param charset character set for passed subject and content. The given
044: * Unicode string will be charset-encoded using the specified charset. The
045: * charset is also used to set the "charset" parameter. For example German
046: * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
047: * parameter is null and subject or content contains non US-ASCII characters,
048: * it will be encoded using the platform's default charset.
049: * @exception SMIMEException if smtpHost or fromAddress parameters are null.
050: * Also, it can be caused by non SMIMEException which is MessagingException.
051: */
052: public PureMIME(String smtpHost, String fromAddress,
053: String subject, String content, String charset)
054: throws SMIMEException {
055: super (smtpHost, fromAddress, subject, content, charset);
056: }
057:
058: /**
059: * Initializes the JavaMail session for SMTP and the MimeMessage object for message
060: * which will be sent. Dynamically loads the BC and SUN provider necessary for
061: * cryptography processing. This constructor does not create content of message
062: * and it can be set later with one of setContent methods. Also, message can be
063: * left withouth content, but then at least one attachement must be added.
064: * @param smtpHost name of SMTP host used for sending email
065: * @param fromAddress email address of sender (FROM field in email header)
066: * @param subject subject of email (SUBJECT field in email header). This
067: * argument can be null, but email message will be sent withouth SUBJECT.
068: * @param charset character set for passed subject and content. The given
069: * Unicode string will be charset-encoded using the specified charset. The
070: * charset is also used to set the "charset" parameter. For example German
071: * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
072: * parameter is null and subject or content contains non US-ASCII characters,
073: * it will be encoded using the platform's default charset.
074: * @exception SMIMEException if smtpHost or fromAddress parameters are null.
075: * Also, it can be caused by non SMIMEException which is MessagingException.
076: */
077: public PureMIME(String smtpHost, String fromAddress,
078: String subject, String charset) throws SMIMEException {
079: super (smtpHost, fromAddress, subject, null, charset);
080: }
081:
082: /**
083: * Construction of message with external prepared MimeMessage object. Usage of
084: * this constructor disables usage of setContent() and addAttachment() methods.
085: * Also, all recipients (TO, CC or BCC type) must be declared again via
086: * setRecipient() method, even if they were previously set. Be very carefull
087: * with usage of this constructor because all MimeBodyPart objects and
088: * MimeMultipart objects used in construction of given MimeMessage object,
089: * must have correct defined Content header arguments, and contents. Contents
090: * must be formed in format which can be recognised and appropriate interpreted
091: * in the process of sending mail. If there is any special content object
092: * added to MimeBodyPart object or MimeMultipart object, the appropriate
093: * DataContent handler must be created for that object and set to corresponding
094: * BodyPart.
095: * @param mimeMessage external created MimeMessage object
096: * @exception SMIMEException if smtpHost or fromAddress parameter is null.
097: * Also, it can be caused by non SMIMEException which is MessagingException.
098: */
099: public PureMIME(MimeMessage mimeMessage) throws SMIMEException {
100: super (mimeMessage);
101: }
102:
103: /**
104: * Adds recipient email address and type. At least one recipient must be declared
105: * as TO type.
106: * @param recipientAddress email address of recipent (fields TO or CC or BCC
107: * in email message header)
108: * @param type should be TO, CC or BCC.
109: * @exception SMIMEException if type of addressing of the messages is not TO, CC,
110: * or BCC. Also it can be caused by non SMIMEException which is MessagingException.
111: */
112: public void addRecipient(String recipientAddress, String type)
113: throws SMIMEException {
114: super .addRecipient(recipientAddress, type, null);
115: }
116:
117: /**
118: * Creates and composes the message.
119: * @exception SMIMEException if one of recipients is not declared as TO
120: * recipient. Also, it can be caused by non SMIMEException which can be one of
121: * the following: MessagingException, or IOException.
122: */
123: public void composeMessage() throws SMIMEException {
124: try {
125: if (super .indicatorTo != true)
126: throw new SMIMEException(1043);
127:
128: if (!super .externalMessagePresence) { // external MimeMessage object presence cheking
129:
130: if (super .contentPresence
131: & super .bodyPartArray.size() == 1) { // message contains only content
132: if (super .bodyPartArray.elementAt(0) instanceof MimeBodyPart) { // text/plain message
133: MimeBodyPart contentBody = (MimeBodyPart) super .bodyPartArray
134: .elementAt(0);
135: super .message.setContent((String) contentBody
136: .getContent(), contentBody
137: .getContentType());
138: super .message
139: .setDisposition(super .message.INLINE);
140: } else
141: // text/html message
142: super .message
143: .setContent((MimeMultipart) super .bodyPartArray
144: .elementAt(0));
145: } else if (super .bodyPartArray.size() != 0) {
146: Multipart mp = new MimeMultipart();
147: for (int i = 0; i != super .bodyPartArray.size(); i++) {
148: if (super .bodyPartArray.elementAt(i) instanceof MimeMultipart) {
149: MimeBodyPart forMulti = new MimeBodyPart();
150: forMulti
151: .setContent((MimeMultipart) super .bodyPartArray
152: .elementAt(i));
153: mp.addBodyPart(forMulti);
154: } else
155: mp
156: .addBodyPart((MimeBodyPart) super .bodyPartArray
157: .elementAt(i));
158: }
159: super .message.setContent(mp);
160: } else
161: throw new SMIMEException(1044);
162:
163: }
164:
165: super .message.saveChanges();
166: super .message.saveChanges();
167: SimpleTimeZone tz = (SimpleTimeZone) SimpleTimeZone
168: .getDefault(); // Sets date and time
169: GregorianCalendar cal = new GregorianCalendar(tz);
170: super .message.setSentDate(cal.getTime());
171:
172: clean();
173: } catch (Exception e) {
174: throw new SMIMEException(e);
175: }
176:
177: }
178:
179: /**
180: * Releases unnecessary memory
181: */
182: private void clean() {
183: super .reset();
184: System.gc(); // Calling garbage collector
185: }
186:
187: }
|