KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtMail


1 package Jt;
2
3 import java.util.*;
4 import java.io.*;
5 import javax.mail.*;
6 import javax.mail.internet.*;
7 import javax.activation.*;
8
9 /**
10  * Jt Adapter for the JavaMail API.
11  */

12
13 public class JtMail extends JtObject {
14 private String JavaDoc from;
15 private String JavaDoc to;
16 private String JavaDoc message;
17 private String JavaDoc subject;
18 private String JavaDoc server;
19 private String JavaDoc cc;
20 private String JavaDoc attachment;
21 private String JavaDoc username;
22 private String JavaDoc password;
23 private Session ses = null;
24
25
26    void activate () {
27
28    if (server == null || from == null || to == null ||
29     message == null) {
30     handleTrace ("JtMail: null attribute(s)");
31     return; // check subject
32
}
33    try {
34           Authenticator auth = null;
35
36        if (ses == null) {
37           if (username != null || password != null)
38             auth = new SMTPAuthenticator();
39           // Get system properties
40
Properties props = System.getProperties();
41           // Setup mail server
42
props.put("mail.smtp.host", server);
43           if (username != null || password != null)
44             props.put("mail.smtp.auth", "true");
45
46
47            // Get session
48
ses = Session.getDefaultInstance(props, auth);
49
50            if (this.getObjTrace() == 1)
51              ses.setDebug(true);
52         }
53
54         // Define message
55

56         MimeMessage msg = new MimeMessage(ses);
57         msg.setFrom(new InternetAddress(from));
58         msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
59         if (cc != null && !cc.equals (""))
60            msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc));
61            
62            msg.setSubject(subject);
63            if (attachment == null)
64              msg.setText(message);
65            else {
66              BodyPart messageBodyPart = new MimeBodyPart();
67              // Fill the message
68
messageBodyPart.setText(message);
69              Multipart multipart = new MimeMultipart();
70              multipart.addBodyPart(messageBodyPart); // Part two is attachment
71
messageBodyPart = new MimeBodyPart();
72              DataSource source = new FileDataSource(attachment);
73              messageBodyPart.setDataHandler(new DataHandler(source));
74              messageBodyPart.setFileName(attachment);
75              multipart.addBodyPart(messageBodyPart);
76              msg.setContent(multipart);
77
78            }
79
80  
81            //msg.setText(message);
82
msg.setSentDate(new Date());
83
84
85            // Send message
86
Transport.send(msg);
87    }
88    catch (Exception JavaDoc e) {
89      handleException (e);
90    }
91    }
92
93   /**
94     * Process object messages.
95     * <ul>
96     * <li> JtACTIVATE - Sends an email message using the JavaMail API
97     * </ul>
98     * @param message message
99     */

100
101   public Object JavaDoc processMessage (Object JavaDoc message) {
102   String JavaDoc content;
103   String JavaDoc query;
104   JtMessage e = (JtMessage) message;
105
106
107       if (e == null || (e.getMsgId() == null))
108           return (null);
109
110      // Remove this object
111
if (e.getMsgId().equals ("JtREMOVE")) {
112        return (null);
113      }
114
115       // establish connection
116
if (e.getMsgId().equals("JtACTIVATE")) {
117     activate ();
118     return (null);
119       }
120
121       handleError
122     ("processMessage: invalid message id:"+
123         e.getMsgId());
124       return (null);
125   }
126
127   /**
128     * Sets the "from" attribute (sender).
129     */

130
131   public void setFrom (String JavaDoc newFrom) {
132     from = newFrom;
133   }
134
135   /**
136     * Returns the "from" attribute (sender).
137     */

138
139   public String JavaDoc getFrom () {
140     return (from);
141   }
142
143
144   /**
145     * Sets the "to" attribute (recipient).
146     */

147
148   public void setTo (String JavaDoc newTo) {
149     to = newTo;
150   }
151
152   /**
153     * Gets the "to" attribute (recipient).
154     */

155
156   public String JavaDoc getTo () {
157     return (to);
158   }
159
160   /**
161    * Sets the CC attribute
162    */

163
164   public void setCc (String JavaDoc cc) {
165     this.cc = cc;
166   }
167
168   /**
169    * Returns the CC attribute.
170    */

171
172   public String JavaDoc getCc () {
173     return (cc);
174   }
175
176   /**
177    * Specifies the content of the email message.
178    */

179
180   public void setMessage (String JavaDoc newMessage) {
181     message = newMessage;
182   }
183
184
185   /**
186     * Returns the content of the email message.
187     */

188
189   public String JavaDoc getMessage () {
190     return (message);
191   }
192
193
194   /**
195     * Specifies the subject of the email message.
196     */

197
198   public void setSubject (String JavaDoc newSubject) {
199     subject = newSubject;
200   }
201
202
203   /**
204     * Returns the subject of the email message.
205     */

206
207   public String JavaDoc getSubject () {
208     return (subject);
209   }
210
211   /**
212     * Sets the user name (for authentication).
213     */

214
215   public void setUsername (String JavaDoc newUsername) {
216     username = newUsername;
217   }
218
219
220   /**
221     * Returns the user name (for authentication).
222     */

223
224   public String JavaDoc getUsername () {
225     return (username);
226   }
227
228   /**
229     * Sets the password (for authentication).
230     */

231
232   public void setPassword (String JavaDoc newPassword) {
233     password = newPassword;
234   }
235
236
237   /**
238     * Returns the password (for authentication).
239     */

240
241   public String JavaDoc getPassword () {
242     return (password);
243   }
244
245   /**
246     * Specifies the SMTP server.
247     */

248
249   public void setServer (String JavaDoc newServer) {
250     server = newServer;
251   }
252
253   /**
254     * Returns the SMTP server.
255     */

256
257   public String JavaDoc getServer () {
258     return (server);
259   }
260
261   /**
262     * Specifies the attachment.
263     */

264
265   public void setAttachment (String JavaDoc attachment) {
266     this.attachment = attachment;
267   }
268
269
270   /**
271     * Returns the attachment.
272     */

273
274   public String JavaDoc getAttachment () {
275     return (attachment);
276   }
277
278
279   /**
280     * Unit tests the messages processed by JtMail.
281     */

282
283   public static void main (String JavaDoc[] args) {
284     JtObject main;
285     JtMail jtmail;
286
287     main = new JtObject ();
288     //main.setValue (main, "objTrace", "1");
289
main.createObject ("Jt.JtMessage", "message");
290
291     // Create JtMail object
292

293     jtmail = (JtMail) main.createObject ("Jt.JtMail", "jtmail");
294
295
296     // Set JtMail attributes
297

298     main.setValue ("jtmail", "from", "Jt@fsw.com");
299
300     main.setValue ("jtmail", "subject", "Hello World!");
301     main.setValue ("jtmail", "message", "Jt message");
302
303
304     // remove comment from the following line if
305
// an attachement is required
306
// main.setValue ("jtmail", "attachment", "test.txt");
307

308     main.setValue ("jtmail", "to", "Jt@fsw.com");
309
310
311     // Activate JtMail (send email)
312

313     main.setValue ("message", "msgId", "JtACTIVATE");
314     main.sendMessage ("jtmail", "message");
315
316
317     // Check for exceptions (if any)
318

319     if (jtmail.getObjException() == null)
320     System.out.println ("JtMail: GO");
321     else
322     System.out.println ("JtMail: FAIL");
323    
324
325     //main.handleTrace ("jtmail:Exception:"
326
// + jtmail.getObjException());
327

328
329     // Remove JtMail object
330

331     main.removeObject ("jtmail");
332
333
334   }
335
336 /**
337 * SimpleAuthenticator is used to do simple authentication
338 * when the SMTP server requires it.
339 */

340 private class SMTPAuthenticator extends javax.mail.Authenticator JavaDoc
341 {
342
343     public PasswordAuthentication getPasswordAuthentication()
344     {
345         //String username = SMTP_AUTH_USER;
346
//String password = SMTP_AUTH_PWD;
347
return new PasswordAuthentication(username, password);
348     }
349 }
350 }
Popular Tags