KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sendmail > SendableMail


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.sendmail;
20
21 import java.util.*;
22 import java.io.*;
23
24 import javax.mail.*;
25 import javax.mail.internet.*;
26 import javax.activation.*;
27
28 /**
29  * A utility class to send mails
30  */

31 public class SendableMail
32 {
33     private MimeMultipart mime;
34     private MimeMessage message;
35     private ArrayList tempfiles;
36
37     /**
38      * Constructor.
39      * Creates an empty mail.
40      */

41     public SendableMail()
42     {
43         tempfiles = new ArrayList();
44
45         //session creation
46
Properties props = new Properties();
47         props.put("mail.smtp.host", SendMailService.SMTP_HOST);
48         Session session = Session.getDefaultInstance(props);
49
50         //message creation
51
message = new MimeMessage(session);
52         mime = new MimeMultipart();
53     }
54
55     /**
56      * Set the from field
57      *
58      * @param from the "from:" email
59      */

60     public void setFrom(String JavaDoc from)
61     throws MessagingException
62     {
63         message.setFrom(new InternetAddress(from));
64     }
65
66     /**
67      * Set the subjet field
68      *
69      * @param subject the subject
70      */

71     public void setSubject(String JavaDoc subject)
72     throws MessagingException
73     {
74         message.setSubject(subject, "iso-8859-15");
75     }
76
77     /**
78      * Add To: receivers
79      *
80      * @param to
81      */

82     public void addTo(String JavaDoc to)
83     throws MessagingException
84     {
85         StringTokenizer str = new StringTokenizer(to, ";,");
86         while (str.hasMoreElements())
87             message.addRecipient(Message.RecipientType.TO, new InternetAddress((String JavaDoc)str.nextElement()));
88     }
89
90     /**
91      * Add Cc: receivers
92      *
93      * @param cc
94      */

95     public void addCc(String JavaDoc cc)
96     throws MessagingException
97     {
98         StringTokenizer str = new StringTokenizer(cc, ";,");
99         while (str.hasMoreElements())
100             message.addRecipient(Message.RecipientType.CC, new InternetAddress((String JavaDoc)str.nextElement()));
101     }
102
103     /**
104      * Add Bcc: receivers
105      *
106      * @param bcc
107      */

108     public void addBcc(String JavaDoc bcc)
109     throws MessagingException
110     {
111         StringTokenizer str = new StringTokenizer(bcc, ";,");
112         while (str.hasMoreElements())
113             message.addRecipient(Message.RecipientType.BCC, new InternetAddress((String JavaDoc)str.nextElement()));
114     }
115
116     /**
117      * Set the content of the mail
118      *
119      * @param content the data
120      * @param type the data type (text/plain, text/html, ...)
121      */

122     public void setContent(String JavaDoc content, String JavaDoc type)
123     throws MessagingException
124     {
125         MimeBodyPart mbp = new MimeBodyPart();
126         mbp.setContent(content, type);
127         mime.addBodyPart(mbp);
128     }
129
130     /**
131      * Remove temporary files (used for attachments)
132      */

133     protected void finalize() throws Throwable JavaDoc
134     {
135         for (int i = 0; i < tempfiles.size(); i++)
136              ((File)tempfiles.get(i)).delete();
137     }
138
139     /**
140      * Attach a file
141      *
142      * @param filename the filename to be displayed
143      * @param content the attach content
144      */

145     public void attach(String JavaDoc filename, String JavaDoc content)
146     throws IOException, MessagingException
147     {
148         MimeBodyPart mbp = new MimeBodyPart();
149
150         File file = File.createTempFile("mail", ".tmp");
151         tempfiles.add(file);
152
153         FileWriter fw = null;
154         try {
155             fw = new FileWriter(file);
156             fw.write(content);
157         } finally {
158             if(fw != null)
159                 fw.close();
160         }
161         
162         FileDataSource fds = new FileDataSource(file);
163         DataHandler dh = new DataHandler(fds);
164         mbp.setFileName(filename);
165         mbp.setDisposition(Part.ATTACHMENT);
166         mbp.setDataHandler(dh);
167
168         mime.addBodyPart(mbp);
169     }
170
171     /**
172      * Send the message
173      */

174     public void send()
175     throws MessagingException
176     {
177         message.setContent(mime);
178         message.setSentDate(new Date());
179         Transport.send(message);
180     }
181 }
182
Popular Tags