KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > mailer > MailTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.mailer;
18
19 import java.io.IOException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Properties JavaDoc;
22 import javax.mail.Session JavaDoc;
23 import javax.mail.internet.MimeBodyPart JavaDoc;
24 import javax.mail.internet.MimeMessage JavaDoc;
25 import javax.mail.internet.MimePartDataSource JavaDoc;
26 import javax.naming.Context JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
31
32 /**
33  * MailTag - JSP tag <b>Mail</b> is used to construct an email message.
34  *
35  * <tag>
36  * <name>mail</name>
37  * <tagclass>org.apache.taglibs.MailTag</tagclass>
38  * <bodycontent>JSP</bodycontent>
39  * <info>Construct an email message</info>
40  *
41  * <attribute>
42  * <name>to</name>
43  * <required>false</required>
44  * <rtexprval>false</rtexprval>
45  * </attribute>
46  * <attribute>
47  * <name>from</name>
48  * <required>false</required>
49  * <rtexprval>false</rtexprval>
50  * </attribute>
51  * <attribute>
52  * <name>cc</name>
53  * <required>false</required>
54  * <rtexprval>false</rtexprval>
55  * </attribute>
56  * <attribute>
57  * <name>bcc</name>
58  * <required>false</required>
59  * <rtexprval>false</rtexprval>
60  * </attribute>
61  * <attribute>
62  * <name>subject</name>
63  * <required>false</required>
64  * <rtexprval>false</rtexprval>
65  * </attribute>
66  * <attribute>
67  * <name>user</name>
68  * <required>false</required>
69  * <rtexprvalue>false</rtexprvalue>
70  * </attribute>
71  * <attribute>
72  * <name>password</name>
73  * <required>false</required>
74  * <rtexprvalue>false</rtexprvalue>
75  * </attribute>
76  * <attribute>
77  * <name>server</name>
78  * <required>false</required>
79  * <rtexprvalue>false</rtexprvalue>
80  * </attribute>
81  * <attribute>
82  * <name>port</name>
83  * <required>false</required>
84  * <rtexprvalue>false</rtexprvalue>
85  * </attribute>
86  * <attribute>
87  * <name>session</name>
88  * <required>false</required>
89  * <rtexprvalue>false</rtexprvalue>
90  * </attribute>
91  * <attribute>
92  * <name>mimeMessage</name>
93  * <required>false</required>
94  * <rtexprvalue>false</rtexprvalue>
95  * </attribute>
96  * <attribute>
97  * <name>authenticate</name>
98  * <required>false</required>
99  * <rtexprvalue>false</rtexprvalue>
100  * </attribute>
101  * </tag>
102  *
103  * @author Rich Catlett
104  *
105  * @version 1.0
106  *
107  */

108
109 public class MailTag extends BodyTagSupport JavaDoc {
110
111     /**
112      * The address type string constants
113      */

114     protected final static String JavaDoc TO_ADDRESS_STRING = "to";
115     protected final static String JavaDoc CC_ADDRESS_STRING = "cc";
116     protected final static String JavaDoc BCC_ADDRESS_STRING = "bcc";
117
118     /**
119      * The address type int constants
120      */

121     protected final static int TO_ADDRESS = 1;
122     protected final static int CC_ADDRESS = 2;
123     protected final static int BCC_ADDRESS = 3;
124
125     /**
126      * the address to which the mail is to be sent
127      */

128     private String JavaDoc to = null;
129     private StringBuffer JavaDoc addTO = new StringBuffer JavaDoc();
130
131     /**
132      * the address to whom the recipient can reply to
133      */

134     private String JavaDoc from = null;
135     private String JavaDoc reset_from = null;
136
137     /**
138      * the carbon copy list addresses that the message will be sent to
139      */

140     private String JavaDoc cc = null;
141     private StringBuffer JavaDoc addCC = new StringBuffer JavaDoc();
142
143     /**
144      * the blind carbon copy list of addresses to recieve the message
145      */

146     private String JavaDoc bcc = null;
147     private StringBuffer JavaDoc addBCC = new StringBuffer JavaDoc();
148
149     /**
150      * the subject of the message
151      */

152     private String JavaDoc subject = "";
153     private String JavaDoc reset_subject;
154
155     /**
156      * the body of the email message
157      */

158     private String JavaDoc body = null;
159
160     /**
161      * or provide the server here for a new session
162      */

163     private String JavaDoc server = "localhost";
164     private String JavaDoc reset_server = null;
165
166     /**
167      * provide the server port here for a new session
168      */

169     private String JavaDoc port = "25";
170     private String JavaDoc reset_port = null;
171
172     /**
173      * can be "text" or "html" (otherwise text is default)
174      */

175     private String JavaDoc type = "text/plain";
176
177     /**
178      * character set (default is unspecified)
179      */

180     private String JavaDoc charset = null;
181
182     /**
183      * jndi name for Session object
184      */

185     private String JavaDoc session = null;
186
187     /**
188      * jndi named MimePartDataSource object
189      */

190     private String JavaDoc mimemessage = null;
191
192     /**
193      * user to login to smtp server
194      */

195     private String JavaDoc user = null;
196
197     /**
198      * simple text password to authenticate on smtp server
199      */

200     private String JavaDoc password = null;
201
202     /**
203      * The actual session object
204      */

205     private Session JavaDoc sessionobj = null;
206
207     /**
208      * the reply to address
209      */

210     private String JavaDoc replyto = null;
211     private String JavaDoc reset_replyto = null;
212
213     /**
214      * list of names extra headers to add
215      */

216     private ArrayList JavaDoc name = new ArrayList JavaDoc(10);
217
218     /**
219      * list of values of extra headers to add
220      */

221     private ArrayList JavaDoc value = new ArrayList JavaDoc(10);
222
223     /**
224      * list of attachments stored as mimebodyparts
225      */

226     private ArrayList JavaDoc bodyparts = new ArrayList JavaDoc(10);
227
228     /**
229      * flag that lets the send tag know if attachments are being sent
230      */

231     private boolean attachments = false;
232
233     /**
234      * flag determines if an error has occured
235      */

236     private boolean error = false;
237
238     /**
239      * flag that determines if the session for sending mail should have an
240      * authenticator so that user name and password can be entered if it is
241      * required for the mail server
242      */

243     private boolean authentication = false;
244
245     /**
246      * implementation of method from the Tag interface that tells the JSP what
247      * to do upon encountering the start tag for this tag set
248      *
249      * @return EVAL_BODY_TAG - integer value telling the JSP engine to evaluate
250      * the body of this tag
251      *
252      * @throws JspException thrown when error occurs in processing the body of
253      * this method
254      *
255      */

256     public int doStartTag() throws JspException JavaDoc {
257         // Reset dynamic email addresses
258
addTO.setLength(0);
259         if (to != null) {
260             addTO.append(to);
261         }
262         addCC.setLength(0);
263         if (cc != null) {
264             addCC.append(cc);
265         }
266         addBCC.setLength(0);
267         if (bcc != null) {
268             addBCC.append(bcc);
269         }
270         reset_from = null;
271         reset_replyto = null;
272         reset_subject = null;
273         reset_server = null;
274         reset_port = null;
275         name.clear();
276         value.clear();
277         bodyparts.clear();
278         attachments = false;
279         sessionobj = null;
280
281     // taglibs 1.1
282
return EVAL_BODY_TAG; // evaluate the body of this tag
283
// taglibs 1.2
284
// return EVAL_BODY_BUFFERED; // evaluate the body of this tag
285
}
286
287     /**
288      * implementation of method from the Tag interface that tells the JSP what
289      * to do upon encountering the end tag for this tag set
290      *
291      * @return - integer value telling the JSP engine whether or not to evaluate
292      * the body of this tag
293      *
294      * @throws JspException thrown when error occurs in processing the body of
295      * this method
296      *
297      */

298     public int doEndTag() throws JspException JavaDoc {
299
300     // check to see if bodycontent is just blank space
301
if (bodyContent.getString().trim().length() == 0)
302         return EVAL_PAGE;
303     else {
304         // write out to bodycontent of parent tag
305
try {
306         pageContext.getOut().write(bodyContent.getString());
307             } catch (IOException JavaDoc ie) {
308             pageContext.getServletContext().log("Mailer taglib: Error tag"
309                           + ": unable to write out to jsp.");
310         }
311         return SKIP_PAGE;
312     }
313     }
314
315     /**
316      * get the whole email message
317      *
318      * @return message - the entire email message
319      *
320      */

321     public MimeMessage JavaDoc getMessage() throws JspException JavaDoc {
322         MimeMessage JavaDoc message;
323         if (mimemessage != null) {
324             try {
325                 // get initial context from which to lookup messagedatasource
326
Context JavaDoc ctx = new InitialContext JavaDoc();
327
328                 // create mail message using preexisting jndi named message
329
MimePartDataSource JavaDoc mds =
330                                  (MimePartDataSource JavaDoc)ctx.lookup(mimemessage);
331                 sessionobj = mds.getMessageContext().getSession();
332                 message = new MimeMessage JavaDoc(sessionobj);
333             } catch (NamingException JavaDoc ne) {
334                 throw new JspException JavaDoc("Naming Exception " +
335                                        ne.getExplanation());
336             }
337         } else if (session != null) {
338             try {
339                 // get initial context from which to lookup session
340
Context JavaDoc ctx = new InitialContext JavaDoc();
341
342                 // create the mail message using the preconfigured jndi
343
// named session
344
sessionobj = (Session JavaDoc)ctx.lookup(session);
345                 message = new MimeMessage JavaDoc(sessionobj);
346             } catch (NamingException JavaDoc ne) {
347                 throw new JspException JavaDoc("Naming Exception " +
348                                            ne.getExplanation());
349             }
350         } else {
351             // if configuring one of the above objects the following list
352
// are a good set to use to take care of error handling if some
353
// addresses are not valid
354

355             // set up the smtp session that will send the message
356
Properties JavaDoc props = new Properties JavaDoc();
357             // set host to server
358
if (reset_server != null) {
359                 props.put("mail.smtp.host", reset_server);
360             } else {
361                 props.put("mail.smtp.host", server);
362             }
363             // set port to server
364
if (reset_port != null) {
365                 props.put("mail.smtp.port", reset_port);
366             } else {
367                 props.put("mail.smtp.port", port);
368             }
369             // set properties to deal will SendFailedExceptions
370
// send to all legal addresses
371
props.put("mail.smtp.sendpartial", "true");
372             // set notification
373
props.put("mail.smtp.dsn.notify", "FAILURE");
374             // set amount of message to get returned
375
props.put("mail.smtp.dsn.ret", "FULL");
376             // create new piece of mail for the smtp mail session check if
377
// authentication is required for the mail server
378

379             if (authentication) {
380                     // create the session with an authenticator object
381
// better way to do authentication
382
props.put("mail.smtp.auth", "true");
383                     sessionobj = Session.getDefaultInstance(props,
384                                          new MailAuthenticator(user, password));
385             } else
386                 sessionobj = Session.getDefaultInstance(props, null);
387
388             message = new MimeMessage JavaDoc(sessionobj);
389         }
390     return message;
391     }
392
393     /**
394      * get the list of attachments
395      *
396      * @return - an arraylist of the mimebodyparts for attachments
397      *
398      */

399     public ArrayList JavaDoc getBodyParts() {
400     return bodyparts;
401     }
402
403     /**
404      * get the attachments flat
405      *
406      * @return - the flag that lets the message know if there are attachments
407      *
408      */

409     public boolean getAttachments() {
410     return attachments;
411     }
412
413     /**
414      * get the to address for this email
415      *
416      * @return - the address to whom this mail is to be sent
417      *
418      */

419     public String JavaDoc getTo() {
420         if (addTO.length() > 0 ) {
421            return addTO.toString();
422         }
423         return null;
424     }
425
426     /**
427      * get the Reply-to address for this email
428      *
429      * @return - the address to whom this mail is to be sent
430      *
431      */

432     public String JavaDoc getReplyTo() {
433         if (reset_replyto != null) {
434             return reset_replyto;
435         }
436     return replyto;
437     }
438
439     /**
440      * get the from address for this email
441      *
442      * @return - the address from whom this mail is to be sent
443      *
444      */

445     public String JavaDoc getFrom() {
446         if (reset_from != null) {
447             return reset_from;
448         }
449     return from;
450     }
451
452     /**
453      * get the cc address/addresses for this email
454      *
455      * @return - list of carbon copy addresses to recieve
456      * this email
457      *
458      */

459     public String JavaDoc getCc() {
460         if (addCC.length() > 0 ) {
461            return addCC.toString();
462         }
463         return null;
464     }
465
466     /**
467      * get the bcc address/addresses for this email
468      *
469      * @return - list of blind carbon copy addresses to
470      * recieve this email
471      *
472      */

473     public String JavaDoc getBcc() {
474         if (addBCC.length() > 0 ) {
475        return addBCC.toString();
476         }
477         return null;
478     }
479
480     /**
481      * get the subject for this email
482      *
483      * @return - string that is the subject of this email
484      *
485      */

486     public String JavaDoc getSubject() {
487         if (reset_subject != null) {
488             return reset_subject;
489         }
490     return subject;
491     }
492
493     /**
494      * get the list names of extra headers to be set
495      *
496      * @return - list of names of extra headers to be set
497      *
498      */

499     public ArrayList JavaDoc getHeaderName() {
500     return name;
501     }
502
503     /**
504      * get the list of values of extra headers to be set
505      *
506      * @return - list of values of extra headers to be set
507      *
508      */

509     public ArrayList JavaDoc getHeaderValue() {
510     return value;
511     }
512
513     /**
514      * get the message for this email
515      *
516      * @return - string that is the body of this email
517      *
518      */

519     public String JavaDoc getBody() {
520     return body;
521     }
522
523
524     /**
525      * get the jndi named session to be used to find the mail session
526      *
527      * @return - session object for this email
528      *
529      */

530     public Session JavaDoc getSessionObj() {
531     return sessionobj;
532     }
533
534     /**
535      * get the mime type for this email text or html
536      *
537      * @return - string that is the mime type for this email
538      *
539      */

540     public String JavaDoc getType() {
541     return type;
542     }
543
544     /**
545      * get the character set for this email
546      *
547      * @return - string that is the character set for this email
548      *
549      */

550     public String JavaDoc getCharset() {
551     return charset;
552     }
553
554     /**
555      * set true if error has occured
556      *
557      * @param value value that determines if an error has occured
558      *
559      */

560     public void setError(boolean value) {
561     error = value;
562     }
563
564     /**
565      * set the to address for this email
566      *
567      * @param value string that is the address to whom this mail is to be sent
568      *
569      */

570     public void setTo(String JavaDoc value) {
571     to = value;
572     }
573
574     /**
575      * set the Reply-to address for this email
576      *
577      * @param value string that is the address to whom this mail is to be sent
578      *
579      */

580     public void setReplyTo(String JavaDoc value) {
581     replyto = value;
582     }
583
584     /**
585      * set the from address for this email
586      *
587      * @param value string that is the address from whom this mail is to be sent
588      *
589      */

590     public void setFrom(String JavaDoc value) {
591     from = value;
592     }
593
594     /**
595      * set the cc address/addresses for this email
596      *
597      * @param value string that is the list of carbon copy addresses to recieve
598      * this email
599      *
600      */

601     public void setCc(String JavaDoc value) {
602         cc = value;
603     }
604
605     /**
606      * set the bcc address/addresses for this email
607      *
608      * @param value string that is the list of blind carbon copy addresses to
609      * recieve this email
610      *
611      */

612     public void setBcc(String JavaDoc value) {
613     bcc = value;
614     }
615
616     /**
617      * set the subject for this email
618      *
619      * @param value string that is the subject of this email
620      *
621      */

622     public void setSubject(String JavaDoc value) {
623     subject = value;
624     }
625
626     /**
627      * set the name and value of any extra headers to be sent
628      *
629      * @param name string that is the name of an extra header to be sent
630      * @param value string that is the value of an extra header to be sent
631      */

632     protected void setHeader(String JavaDoc name, String JavaDoc value) {
633     this.name.add(name);
634         this.value.add(value);
635     }
636
637     /**
638      * set the message for this email
639      *
640      * @param value string that is the subject of this email
641      *
642      */

643     public void setMessage(String JavaDoc value) {
644     body = value;
645     }
646
647     /**
648      * set attachments into an arraylist
649      *
650      * @param mbp mimebodypart to be attached to the e-mail
651      *
652      */

653     public void setBodyParts(MimeBodyPart JavaDoc mbp) {
654     bodyparts.add(mbp);
655
656     // set attachments to true since the possibility of multiple attachments
657
// exists there is no sence in setting attachments to true everytime one
658
// is set
659
if (attachments == false)
660         attachments = true;
661     }
662
663     /**
664      * set the login user name for basic smtp authentication
665      *
666      * @param value login name
667      *
668      */

669     public void setUser(String JavaDoc value) {
670     user = value;
671     }
672
673     /**
674      * set the password for basic smtp authentication
675      *
676      * @param value Plain text password for authentication
677      *
678      */

679     public void setPassword(String JavaDoc value) {
680     password = value;
681     }
682
683     /**
684      * set the server to be used to find the mail session if one is not provided
685      *
686      * @param value string that is the server used to find the mail session
687      *
688      */

689     public void setServer(String JavaDoc value) {
690     server = value;
691     }
692
693     /**
694      * set the server port to be used for the mail session
695      *
696      * @param value string that is the server port to be used
697      *
698      */

699     public void setPort(String JavaDoc value) {
700     port = value;
701     }
702
703     /**
704      * set the jndi named session to be used to find the mail session
705      *
706      * @param value string that is the jndi named session
707      *
708      */

709     public void setMimeMessage(String JavaDoc value) {
710     mimemessage = value;
711     }
712
713     /**
714      * set the jndi named session to be used to find the mail session
715      *
716      * @param value string that is the jndi named session
717      *
718      */

719     public void setSession(String JavaDoc value) {
720     session = value;
721     }
722
723     /**
724      * set authentication flag
725      *
726      * @param value boolean value that marks if an authenticator object should
727      * be used in the creation of the session so that user
728      * authentication can be performed
729      *
730      */

731     public void setAuthenticate(String JavaDoc value) {
732     authentication = new Boolean JavaDoc(value).booleanValue();
733     }
734
735     /**
736      * set the mime type for this email text or html
737      *
738      * @param value string that is the mime type for this email
739      *
740      */

741     public void setType(String JavaDoc value) {
742     if (value.equalsIgnoreCase("html"))
743         type = "text/html";
744     else
745         type = "text/plain";
746     }
747
748     /**
749      * set the character set for this email
750      *
751      * @param value string that is the character set for this email
752      *
753      */

754     public void setCharset(String JavaDoc value) {
755         charset = value;
756     }
757
758     /**
759      * add a to address to the list of to addresses for this email
760      *
761      * @param value string that is an address to whom this mail is to be sent
762      *
763      */

764     protected void addTo(String JavaDoc value) {
765         if (addTO.length() > 0) {
766             addTO.append(",");
767         }
768     addTO.append(value);
769     }
770
771     /**
772      * set a cc address to the list of cc addresses for this email
773      *
774      * @param value string that is a cc address to be added to the list of cc
775      * addresses for this email
776      *
777      */

778     protected void addCc(String JavaDoc value) {
779         if (addCC.length() > 0) {
780             addCC.append(",");
781         }
782         addCC.append(value);
783     }
784
785     /**
786      * add a bcc address to the list of bcc addresses for this email
787      *
788      * @param value string that is a bcc address to be added to the list of bcc
789      * addresses for this email
790      *
791      */

792     protected void addBcc(String JavaDoc value) {
793         if (addBCC.length() > 0) {
794             addBCC.append(",");
795         }
796         addBCC.append(value);
797     }
798
799     /**
800      * reset the to address to a single email address
801      *
802      * @param value string that is an address to whom this mail is to be sent
803      */

804     protected void resetTo(String JavaDoc value) {
805         addTO.setLength(0);
806         addTO.append(value);
807     }
808
809     /**
810      * reset the cc address to a single email address
811      *
812      * @param value string that is a cc address to be used for this email
813      */

814     protected void resetCc(String JavaDoc value) {
815         addCC.setLength(0);
816         addCC.append(value);
817     }
818
819     /**
820      * reset the bcc address to a single email address
821      *
822      * @param value string that is a bcc address to be used for this email
823      */

824     protected void resetBcc(String JavaDoc value) {
825         addBCC.setLength(0);
826         addBCC.append(value);
827     }
828
829     /**
830      * reset the from address for this email
831      *
832      * @param value string that is the address from whom this mail is to be sent
833      */

834     protected void resetFrom(String JavaDoc value) {
835         reset_from = value;
836     }
837
838     /**
839      * reset the replyto address for this email
840      *
841      * @param value string that is the address for email reply to
842      */

843     protected void resetReplyTo(String JavaDoc value) {
844         reset_replyto = value;
845     }
846
847     /**
848      * reset the email subject
849      *
850      * @param value string that is the email subject
851      */

852     protected void resetSubject(String JavaDoc value) {
853         reset_subject = value;
854     }
855
856     /**
857      * reset the SMTP server hostname
858      *
859      * @param value string that is SMTP server hostname
860      */

861     protected void resetServer(String JavaDoc value) {
862         reset_server = value;
863     }
864
865     /**
866      * reset the SMTP server port
867      *
868      * @param value string that is SMTP server port
869      */

870     protected void resetPort(String JavaDoc value) {
871         reset_port = value;
872     }
873
874 }
875
876
Popular Tags