KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > framework > AceMailMessage


1
2 /*
3  * MailMessage.java
4  *
5  * Created on November 16, 2002, 10:13 PM
6  */

7
8 package com.quikj.server.framework;
9
10 import java.util.*;
11 import javax.mail.*;
12 import javax.mail.internet.*;
13 import java.io.*;
14
15
16 /**
17  *
18  * @author bhm
19  */

20 public class AceMailMessage implements Serializable
21 {
22     
23     /** Creates a new instance of MailMessage */
24     public AceMailMessage()
25     {
26     }
27     
28     public javax.mail.Message JavaDoc toEmail(javax.mail.Session JavaDoc session)
29     // This method performs validation and returns null if the message shouldn't be sent
30
// Call getErrorMessage() in that case, to get the failure string.
31
{
32         MimeMessage emsg = new MimeMessage(session);
33         
34         String JavaDoc field = "\"from\" = " + from;
35         if (from != null)
36         {
37             if (addressValid(from) == true)
38             {
39                 try
40                 {
41                     emsg.setFrom(new InternetAddress(from));
42                 }
43                 catch (Exception JavaDoc ex)
44                 { // discard & proceed
45
}
46             }
47         }
48         
49         int size = to.size();
50         for (int i = 0; i < size; i++)
51         {
52             String JavaDoc value = (String JavaDoc)to.elementAt(i);
53             field = "\"to\" = " + value;
54             if (addressValid(value) == false)
55             {
56                 continue;
57             }
58             
59             try
60             {
61                 emsg.addRecipient(Message.RecipientType.TO, new InternetAddress(value));
62             }
63             catch (Exception JavaDoc ex)
64             { // discard & proceed
65
}
66         }
67         
68         size = cc.size();
69         for (int i = 0; i < size; i++)
70         {
71             String JavaDoc value = (String JavaDoc)cc.elementAt(i);
72             field = "\"cc\" = " + value;
73             if (addressValid(value) == false)
74             {
75                 continue;
76             }
77             
78             try
79             {
80                 emsg.addRecipient(Message.RecipientType.CC, new InternetAddress(value));
81             }
82             catch (Exception JavaDoc ex)
83             { // discard & proceed
84
}
85         }
86         
87         size = bcc.size();
88         for (int i = 0; i < size; i++)
89         {
90             String JavaDoc value = (String JavaDoc)bcc.elementAt(i);
91             field = "\"bcc\" = " + value;
92             if (addressValid(value) == false)
93             {
94                 continue;
95             }
96             
97             try
98             {
99                 emsg.addRecipient(Message.RecipientType.BCC, new InternetAddress(value));
100             }
101             catch (Exception JavaDoc ex)
102             { // discard & proceed
103
}
104         }
105         
106         field = "\"subject\" = " + subject;
107         if (subject != null)
108         {
109             try
110             {
111                 emsg.setSubject(subject);
112             }
113             catch (Exception JavaDoc ex)
114             {
115                 errorMessage = "Error setting " + field + " : " + ex.getClass().getName() + " : " + ex.getMessage();
116                 return null;
117             }
118         }
119         
120         size = replyTo.size();
121         if (size > 0)
122         {
123             field = "\"reply-to\"";
124             
125             ArrayList reply_to = new ArrayList();
126             for (int i = 0; i < size; i++)
127             {
128                 String JavaDoc value = (String JavaDoc)replyTo.elementAt(i);
129                 if (addressValid(value) == false)
130                 {
131                     continue;
132                 }
133                 
134                 try
135                 {
136                     reply_to.add(new InternetAddress(value));
137                 }
138                 catch (Exception JavaDoc ex)
139                 { // discard & proceed
140
}
141             }
142             
143             if (reply_to.size() > 0)
144             {
145                 try
146                 {
147                     InternetAddress[] temp = new InternetAddress[reply_to.size()];
148                     reply_to.toArray(temp);
149                     emsg.setReplyTo(temp);
150                 }
151                 catch (Exception JavaDoc ex)
152                 {
153                     errorMessage = "Error setting " + field + " : " + ex.getClass().getName() + " : " + ex.getMessage();
154                     return null;
155                 }
156             }
157         }
158         
159         field = "\"body\" = " + body;
160         if (body != null)
161         {
162             try
163             {
164                 emsg.setText(body);
165             }
166             catch (Exception JavaDoc ex)
167             {
168                 errorMessage = "Error setting " + field + " : " + ex.getClass().getName() + " : " + ex.getMessage();
169                 return null;
170             }
171         }
172         else
173         {
174             errorMessage = "Invalid body : null";
175             return null;
176         }
177         
178         
179         // check for at least one recipient
180

181         try
182         {
183             Address[] recipients = emsg.getAllRecipients();
184             
185             if (recipients == null || recipients.length == 0)
186             {
187                 errorMessage = "No valid recipients";
188                 return null;
189             }
190         }
191         catch (Exception JavaDoc ex)
192         {
193             errorMessage = "Error getting recipients for validation : " + ex.getClass().getName() + " : " + ex.getMessage();
194             return null;
195         }
196         
197         
198         return emsg;
199     }
200     
201     public static boolean addressValid(String JavaDoc addr)
202     {
203         // check for domain presence
204
StringTokenizer tok = new StringTokenizer(addr, "@");
205         if (tok.countTokens() < 2)
206         {
207             return false;
208         }
209         
210         tok.nextToken();
211         String JavaDoc domain = tok.nextToken();
212         
213         StringTokenizer tok2 = new StringTokenizer(domain, ".");
214         if (tok2.countTokens() < 2)
215         {
216             return false;
217         }
218         
219         return true;
220     }
221     
222     /** Getter for property bcc.
223      * @return Value of property bcc.
224      */

225     public java.util.Vector JavaDoc getBcc()
226     {
227         return bcc;
228     }
229     
230     /** Setter for property bcc.
231      * @param bcc New value of property bcc.
232      */

233     public void setBcc(java.util.Vector JavaDoc bcc)
234     {
235         this.bcc = bcc;
236     }
237     
238     public void addBcc(String JavaDoc bcc)
239     {
240         (this.bcc).addElement(new String JavaDoc(bcc));
241     }
242     
243     public int numBcc()
244     {
245         return bcc.size();
246     }
247     
248     /** Getter for property body.
249      * @return Value of property body.
250      */

251     public java.lang.String JavaDoc getBody()
252     {
253         return body;
254     }
255     
256     /** Setter for property body.
257      * @param body New value of property body.
258      */

259     public void setBody(java.lang.String JavaDoc body)
260     {
261         this.body = body;
262     }
263     
264     /** Getter for property cc.
265      * @return Value of property cc.
266      */

267     public java.util.Vector JavaDoc getCc()
268     {
269         return cc;
270     }
271     
272     /** Setter for property cc.
273      * @param cc New value of property cc.
274      */

275     public void setCc(java.util.Vector JavaDoc cc)
276     {
277         this.cc = cc;
278     }
279     
280     public void addCc(String JavaDoc cc)
281     {
282         (this.cc).addElement(new String JavaDoc(cc));
283     }
284     
285     public int numCc()
286     {
287         return cc.size();
288     }
289     
290     /** Getter for property from.
291      * @return Value of property from.
292      */

293     public java.lang.String JavaDoc getFrom()
294     {
295         return from;
296     }
297     
298     /** Setter for property from.
299      * @param from New value of property from.
300      */

301     public void setFrom(java.lang.String JavaDoc from)
302     {
303         this.from = from;
304     }
305     
306     /** Getter for property subject.
307      * @return Value of property subject.
308      */

309     public java.lang.String JavaDoc getSubject()
310     {
311         return subject;
312     }
313     
314     /** Setter for property subject.
315      * @param subject New value of property subject.
316      */

317     public void setSubject(java.lang.String JavaDoc subject)
318     {
319         this.subject = subject;
320     }
321     
322     /** Getter for property to.
323      * @return Value of property to.
324      */

325     public java.util.Vector JavaDoc getTo()
326     {
327         return to;
328     }
329     
330     /** Setter for property to.
331      * @param to New value of property to.
332      */

333     public void setTo(java.util.Vector JavaDoc to)
334     {
335         this.to = to;
336     }
337     
338     public void addTo(String JavaDoc to)
339     {
340         (this.to).addElement(new String JavaDoc(to));
341     }
342     
343     public int numTo()
344     {
345         return to.size();
346     }
347     
348     /** Getter for property replyTo.
349      * @return Value of property replyTo.
350      */

351     public java.util.Vector JavaDoc getReplyTo()
352     {
353         return replyTo;
354     }
355     
356     /** Setter for property replyTo.
357      * @param replyTo New value of property replyTo.
358      */

359     public void setReplyTo(java.util.Vector JavaDoc replyTo)
360     {
361         this.replyTo = replyTo;
362     }
363     
364     public void addReplyTo(String JavaDoc reply_to)
365     {
366         (this.replyTo).addElement(new String JavaDoc(reply_to));
367     }
368     
369     public int numReplyTo()
370     {
371         return replyTo.size();
372     }
373     
374     /** Getter for property errorMessage.
375      * @return Value of property errorMessage.
376      *
377      */

378     public java.lang.String JavaDoc getErrorMessage()
379     {
380         return errorMessage;
381     }
382     
383     
384     private String JavaDoc from = null;
385     private Vector to = new Vector();
386     private Vector cc = new Vector();
387     private Vector bcc = new Vector();
388     private String JavaDoc subject = null;
389     private Vector replyTo = new Vector();
390     private String JavaDoc body = null;
391     
392     private String JavaDoc errorMessage = "";
393     
394 }
395
Popular Tags