KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > SendTag


1 /*
2  * @(#)SendTag.java 1.3 02/04/04
3  *
4  * Copyright 2001-2002 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  *
38  */

39
40 package demo;
41
42 import java.util.*;
43 import java.net.*;
44 import javax.mail.*;
45 import javax.mail.internet.*;
46 import javax.servlet.jsp.*;
47 import javax.servlet.jsp.tagext.*;
48
49 /**
50  * Custom tag for sending messages.
51  */

52 public class SendTag extends BodyTagSupport {
53     private String JavaDoc body;
54     private String JavaDoc cc;
55     private String JavaDoc host;
56     private String JavaDoc recipients;
57     private String JavaDoc sender;
58     private String JavaDoc subject;
59
60     /**
61      * host attribute setter method.
62      */

63     public void setHost(String JavaDoc host) {
64         this.host = host;
65     }
66     
67     /**
68      * recipient attribute setter method.
69      */

70     public void setRecipients(String JavaDoc recipients) {
71         this.recipients = recipients;
72     }
73
74     /**
75      * sender attribute setter method.
76      */

77     public void setSender(String JavaDoc sender) {
78         this.sender = sender;
79     }
80
81     /**
82      * cc attribute setter method.
83      */

84     public void setCc(String JavaDoc cc) {
85         this.cc = cc;
86     }
87
88     /**
89      * subject attribute setter method.
90      */

91     public void setSubject(String JavaDoc subject) {
92         this.subject = subject;
93     }
94
95     /**
96      * Method for processing the end of the tag.
97      */

98     public int doEndTag() throws JspException {
99         Properties props = System.getProperties();
100         
101         try {
102             if (host != null)
103                 props.put("mail.smtp.host", host);
104             else if (props.getProperty("mail.smtp.host") == null)
105                 props.put("mail.smtp.host", InetAddress.getLocalHost().
106                     getHostName());
107         } catch (Exception JavaDoc ex) {
108             throw new JspException(ex.getMessage());
109         }
110         Session session = Session.getDefaultInstance(props, null);
111         
112     Message msg = new MimeMessage(session);
113     InternetAddress[] toAddrs = null, ccAddrs = null;
114
115         try {
116         if (recipients != null) {
117             toAddrs = InternetAddress.parse(recipients, false);
118             msg.setRecipients(Message.RecipientType.TO, toAddrs);
119         } else
120             throw new JspException("No recipient address specified");
121
122             if (sender != null)
123                 msg.setFrom(new InternetAddress(sender));
124             else
125                 throw new JspException("No sender address specified");
126
127         if (cc != null) {
128                 ccAddrs = InternetAddress.parse(cc, false);
129             msg.setRecipients(Message.RecipientType.CC, ccAddrs);
130         }
131
132         if (subject != null)
133             msg.setSubject(subject);
134
135         if ((body = getBodyContent().getString()) != null)
136             msg.setText(body);
137             else
138                 msg.setText("");
139
140             Transport.send(msg);
141     
142         } catch (Exception JavaDoc ex) {
143             throw new JspException(ex.getMessage());
144         }
145
146         return(EVAL_PAGE);
147    }
148 }
149
150
Popular Tags