Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/lucane/plugins/sendmail/SendableMail.java


1   /*
2    * Lucane - a collaborative platform
3    * Copyright (C) 2003  Vincent Fiack <vincent@lucane.org>
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.plugins.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 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 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 to) 
83    throws MessagingException
84    {
85      StringTokenizer str = new StringTokenizer(to, ";,");
86      while (str.hasMoreElements())
87        message.addRecipient(Message.RecipientType.TO, new InternetAddress((String)str.nextElement()));
88    }
89  
90    /**
91     * Add Cc: receivers
92     * 
93     * @param cc
94     */
95    public void addCc(String cc) 
96    throws MessagingException
97    {
98      StringTokenizer str = new StringTokenizer(cc, ";,");
99      while (str.hasMoreElements())
100       message.addRecipient(Message.RecipientType.CC, new InternetAddress((String)str.nextElement()));
101   }
102 
103   /**
104    * Add Bcc: receivers
105    * 
106    * @param bcc
107    */
108   public void addBcc(String bcc) 
109   throws MessagingException
110   {
111     StringTokenizer str = new StringTokenizer(bcc, ";,");
112     while (str.hasMoreElements())
113       message.addRecipient(Message.RecipientType.BCC, new InternetAddress((String)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 content, String 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
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 filename, String 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 = new FileWriter(file);
154                 System.out.println("content = " + content);
155     fw.write(content);
156     fw.close();
157 
158     FileDataSource fds = new FileDataSource(file);
159     DataHandler dh = new DataHandler(fds);
160     mbp.setFileName(filename);
161     mbp.setDisposition(Part.ATTACHMENT);
162     mbp.setDataHandler(dh);
163 
164     mime.addBodyPart(mbp);
165   }
166 
167   /**
168    * Send the message
169    */
170   public void send() 
171   throws MessagingException
172   {
173     message.setContent(mime);
174     message.setSentDate(new Date());
175     Transport.send(message);
176   }
177 }