KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > oyster > mail > PureMIME


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

7
8 package org.enhydra.oyster.mail;
9
10 import org.enhydra.oyster.smime.BaseSMIMEObject;
11 import org.enhydra.oyster.exception.SMIMEException;
12 import javax.mail.Multipart JavaDoc;
13 import javax.mail.internet.MimeMessage JavaDoc;
14 import javax.mail.internet.MimeBodyPart JavaDoc;
15 import javax.mail.internet.MimeMultipart JavaDoc;
16 import java.util.SimpleTimeZone JavaDoc;
17 import java.util.GregorianCalendar JavaDoc;
18
19
20 /**
21  * PureMIME class is used for creating and sending pure (clear) MIME message
22  * (withouth any cryptographic posibilities). Initialization, creating and sending
23  * of pure (clear) MIME message has the same functionalities as S/MIME type of
24  * message.
25  */

26 public class PureMIME extends BaseSMIMEObject
27 {
28
29   /**
30    * Initializes the JavaMail session for SMTP and the MimeMessage object for message
31    * which will be sent. Dynamically loads the BC and SUN provider necessary for
32    * cryptography processing. This constructor is used for creating message with
33    * text/plain content. For creating html formated content (text/html), other
34    * constructor should be used in combination with one of setContent methods.
35    * Note that after using this constructor setContent method can be used only
36    * if "content" argument of constructor was given as null, otherwise setContent
37    * method can't be used because content is already set as text/plain.
38    * @param smtpHost name of SMTP host used for sending email
39    * @param fromAddress email address of sender (FROM field in email header)
40    * @param subject subject of email (SUBJECT field in email header). This
41    * argument can be null, but email message will be sent withouth SUBJECT.
42    * @param content text/plain content of email message. This argument can be
43    * null, but later one of setContent() methods or one of addAttachment()
44    * methods should be called
45    * @param charset character set for passed subject and content. The given
46    * Unicode string will be charset-encoded using the specified charset. The
47    * charset is also used to set the "charset" parameter. For example German
48    * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
49    * parameter is null and subject or content contains non US-ASCII characters,
50    * it will be encoded using the platform's default charset.
51    * @exception SMIMEException if smtpHost or fromAddress parameters are null.
52    * Also, it can be caused by non SMIMEException which is MessagingException.
53    */

54   public PureMIME (String JavaDoc smtpHost, String JavaDoc fromAddress, String JavaDoc subject,
55                           String JavaDoc content, String JavaDoc charset) throws SMIMEException
56   {
57     super(smtpHost, fromAddress, subject, content, charset);
58   }
59
60
61   /**
62    * Initializes the JavaMail session for SMTP and the MimeMessage object for message
63    * which will be sent. Dynamically loads the BC and SUN provider necessary for
64    * cryptography processing. This constructor does not create content of message
65    * and it can be set later with one of setContent methods. Also, message can be
66    * left withouth content, but then at least one attachement must be added.
67    * @param smtpHost name of SMTP host used for sending email
68    * @param fromAddress email address of sender (FROM field in email header)
69    * @param subject subject of email (SUBJECT field in email header). This
70    * argument can be null, but email message will be sent withouth SUBJECT.
71    * @param charset character set for passed subject and content. The given
72    * Unicode string will be charset-encoded using the specified charset. The
73    * charset is also used to set the "charset" parameter. For example German
74    * letters should be encoded by usage of 'ISO-8859-1' charset. If charset
75    * parameter is null and subject or content contains non US-ASCII characters,
76    * it will be encoded using the platform's default charset.
77    * @exception SMIMEException if smtpHost or fromAddress parameters are null.
78    * Also, it can be caused by non SMIMEException which is MessagingException.
79    */

80   public PureMIME (String JavaDoc smtpHost, String JavaDoc fromAddress, String JavaDoc subject,
81                           String JavaDoc charset) throws SMIMEException
82   {
83     super(smtpHost, fromAddress, subject, null, charset);
84   }
85
86
87   /**
88    * Construction of message with external prepared MimeMessage object. Usage of
89    * this constructor disables usage of setContent() and addAttachment() methods.
90    * Also, all recipients (TO, CC or BCC type) must be declared again via
91    * setRecipient() method, even if they were previously set. Be very carefull
92    * with usage of this constructor because all MimeBodyPart objects and
93    * MimeMultipart objects used in construction of given MimeMessage object,
94    * must have correct defined Content header arguments, and contents. Contents
95    * must be formed in format which can be recognised and appropriate interpreted
96    * in the process of sending mail. If there is any special content object
97    * added to MimeBodyPart object or MimeMultipart object, the appropriate
98    * DataContent handler must be created for that object and set to corresponding
99    * BodyPart.
100    * @param mimeMessage external created MimeMessage object
101    * @exception SMIMEException if smtpHost or fromAddress parameter is null.
102    * Also, it can be caused by non SMIMEException which is MessagingException.
103    */

104   public PureMIME (MimeMessage JavaDoc mimeMessage) throws SMIMEException
105   {
106     super(mimeMessage);
107   }
108
109
110   /**
111    * Adds recipient email address and type. At least one recipient must be declared
112    * as TO type.
113    * @param recipientAddress email address of recipent (fields TO or CC or BCC
114    * in email message header)
115    * @param type should be TO, CC or BCC.
116    * @exception SMIMEException if type of addressing of the messages is not TO, CC,
117    * or BCC. Also it can be caused by non SMIMEException which is MessagingException.
118    */

119   public void addRecipient (String JavaDoc recipientAddress, String JavaDoc type) throws SMIMEException
120   {
121     super.addRecipient(recipientAddress, type, null);
122   }
123
124 /**
125  * Creates and composes the message.
126  * @exception SMIMEException if one of recipients is not declared as TO
127  * recipient. Also, it can be caused by non SMIMEException which can be one of
128  * the following: MessagingException, or IOException.
129  */

130   public void composeMessage() throws SMIMEException {
131     try {
132       if (super.indicatorTo != true)
133         throw new SMIMEException(this, 1043);
134
135       if(!super.externalMessagePresence) { // external MimeMessage object presence cheking
136

137         if (super.contentPresence & super.bodyPartArray.size() == 1) { // message contains only content
138
if(super.bodyPartArray.elementAt(0) instanceof MimeBodyPart JavaDoc) { // text/plain message
139
MimeBodyPart JavaDoc contentBody = (MimeBodyPart JavaDoc)super.bodyPartArray.elementAt(0);
140             super.message.setContent((String JavaDoc)contentBody.getContent(), contentBody.getContentType());
141             super.message.setDisposition(super.message.INLINE);
142           }
143           else // text/html message
144
super.message.setContent((MimeMultipart JavaDoc)super.bodyPartArray.elementAt(0));
145         }
146         else if ( super.bodyPartArray.size() != 0) {
147           Multipart JavaDoc mp = new MimeMultipart JavaDoc();
148           for (int i = 0; i != super.bodyPartArray.size(); i++) {
149             if(super.bodyPartArray.elementAt(i) instanceof MimeMultipart JavaDoc) {
150               MimeBodyPart JavaDoc forMulti = new MimeBodyPart JavaDoc();
151               forMulti.setContent((MimeMultipart JavaDoc)super.bodyPartArray.elementAt(i));
152               mp.addBodyPart(forMulti);
153             }
154             else
155               mp.addBodyPart((MimeBodyPart JavaDoc)super.bodyPartArray.elementAt(i));
156           }
157           super.message.setContent(mp);
158         }
159         else
160           throw new SMIMEException(this, 1044);
161
162       }
163
164       super.message.saveChanges();
165       super.message.saveChanges();
166       SimpleTimeZone JavaDoc tz = (SimpleTimeZone JavaDoc)SimpleTimeZone.getDefault(); // Sets date and time
167
GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc(tz);
168       super.message.setSentDate(cal.getTime());
169
170       clean();
171     }
172     catch (Exception JavaDoc e) {
173       throw SMIMEException.getInstance(this, e, "composeMessage");
174     }
175
176   }
177
178   /**
179    * Releases unnecessary memory
180    */

181   private void clean () {
182     super.reset();
183     System.gc(); // Calling garbage collector
184
}
185
186 }
Popular Tags