KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > MailProvider


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

17
18 package org.efs.openreports.providers;
19
20 import java.util.Properties JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 import javax.activation.DataHandler JavaDoc;
24 import javax.activation.DataSource JavaDoc;
25 import javax.activation.FileDataSource JavaDoc;
26 import javax.mail.*;
27 import javax.mail.internet.*;
28
29 import com.opensymphony.xwork.ActionContext;
30 import com.opensymphony.xwork.interceptor.component.ComponentManager;
31
32 import org.apache.log4j.Logger;
33
34 import org.efs.openreports.objects.MailMessage;
35 import org.efs.openreports.objects.ORProperty;
36 import org.efs.openreports.util.SMTPAuthenticator;
37
38 public class MailProvider implements PropertiesProviderAware
39 {
40     protected static Logger log = Logger.getLogger(MailProvider.class.getName());
41
42     private PropertiesProvider propertiesProvider;
43
44     private String JavaDoc mailHost;
45     private boolean useMailAuthenticator;
46     private String JavaDoc userName;
47     private String JavaDoc password;
48     
49     // constructor for Spring IOC
50
public MailProvider(PropertiesProvider propertiesProvider) throws ProviderException
51     {
52         this.propertiesProvider = propertiesProvider;
53         init();
54     }
55
56     // constructor for WebWork IOC
57
public MailProvider() throws ProviderException
58     {
59         ComponentManager container = (ComponentManager) ActionContext.getContext().get(
60                 "com.opensymphony.xwork.interceptor.component.ComponentManager");
61
62         container.initializeObject(this);
63         
64         init();
65     }
66     
67     protected void init() throws ProviderException
68     {
69         mailHost = null;
70         useMailAuthenticator = false;
71         userName = null;
72         password = null;
73
74         ORProperty property = propertiesProvider.getProperty(ORProperty.MAIL_SMTP_HOST);
75         if (property != null) mailHost = property.getValue();
76
77         property = propertiesProvider.getProperty(ORProperty.MAIL_AUTH_USER);
78         if (property != null) useMailAuthenticator = Boolean.getBoolean(property.getValue());
79         
80         property = propertiesProvider.getProperty(ORProperty.MAIL_AUTH_USER);
81         if (property != null) userName = property.getValue();
82         
83         property = propertiesProvider.getProperty(ORProperty.MAIL_AUTH_PASSWORD);
84         if (property != null) password = property.getValue();
85
86         log.info("Created");
87     }
88
89     public void sendMail(MailMessage mail) throws ProviderException
90     {
91         try
92         {
93             // create a session
94
Properties JavaDoc mailProps = new Properties JavaDoc();
95             mailProps.put("mail.smtp.host", mailHost);
96
97             Session mailSession = null;
98             if (useMailAuthenticator)
99             {
100                 mailSession = Session.getInstance(mailProps, new SMTPAuthenticator(userName, password));
101             }
102             else
103             {
104                 mailSession = Session.getInstance(mailProps, null);
105             }
106
107             // construct internet addresses
108
InternetAddress from = new InternetAddress(mail.getSender());
109
110             Vector JavaDoc recipients = mail.getRecipients();
111             InternetAddress[] to = new InternetAddress[recipients.size()];
112             for (int i = 0; i < recipients.size(); i++)
113             {
114                 to[i] = new InternetAddress((String JavaDoc) recipients.get(i));
115             }
116
117             // create multipart
118
Multipart multipart = new MimeMultipart();
119
120             // add text part
121
if (mail.getText() != null)
122             {
123                 MimeBodyPart mbpText = new MimeBodyPart();
124                 mbpText.setText(mail.getText());
125                 multipart.addBodyPart(mbpText);
126             }
127
128             // add file attachments
129
Vector JavaDoc attachments = mail.getAttachments();
130             for (int i = 0; i < attachments.size(); i++)
131             {
132                 String JavaDoc fileAttachment = (String JavaDoc) attachments.get(i);
133                 FileDataSource JavaDoc source = new FileDataSource JavaDoc(fileAttachment);
134
135                 MimeBodyPart mbpAttachment = new MimeBodyPart();
136                 mbpAttachment.setDataHandler(new DataHandler JavaDoc(source));
137                 mbpAttachment.setFileName(fileAttachment);
138
139                 multipart.addBodyPart(mbpAttachment);
140             }
141
142             // add byteArrayAttachment
143
if (mail.getByteArrayDataSource().getContentType().equals("text/html"))
144             {
145                 Multipart htmlMP = new MimeMultipart("related");
146                 MimeBodyPart htmlBP = new MimeBodyPart();
147                 htmlBP.setDataHandler(new DataHandler JavaDoc(mail.getByteArrayDataSource()));
148                 htmlMP.addBodyPart(htmlBP);
149
150                 // Add images
151
Vector JavaDoc images = mail.getHtmlImageDataSources();
152                 for (int i = 0; i < images.size(); i++)
153                 {
154                     DataSource JavaDoc imageDS = (DataSource JavaDoc) images.get(i);
155
156                     MimeBodyPart imageBodyPart = new MimeBodyPart();
157                     imageBodyPart.setFileName(imageDS.getName());
158                     imageBodyPart.setText(imageDS.getName());
159                     imageBodyPart.setDataHandler(new DataHandler JavaDoc(imageDS));
160                     imageBodyPart.setHeader("Content-ID", "<" + imageDS.getName() + ">");
161                     imageBodyPart.setDisposition(javax.mail.Part.INLINE);
162
163                     htmlMP.addBodyPart(imageBodyPart);
164                 }
165
166                 BodyPart completeHtmlBP = new MimeBodyPart();
167                 completeHtmlBP.setContent(htmlMP);
168                 multipart.addBodyPart(completeHtmlBP);
169             }
170             else
171             {
172                 MimeBodyPart mbpAttachment = new MimeBodyPart();
173                 mbpAttachment.setDataHandler(new DataHandler JavaDoc(mail.getByteArrayDataSource()));
174                 mbpAttachment.setFileName(mail.getByteArrayDataSource().getName());
175
176                 multipart.addBodyPart(mbpAttachment);
177             }
178
179             // create message
180
Message msg = new MimeMessage(mailSession);
181             msg.setFrom(from);
182             msg.setRecipients(Message.RecipientType.TO, to);
183             msg.setSubject(mail.getSubject());
184             msg.setContent(multipart);
185
186             Transport.send(msg);
187         }
188         catch (Exception JavaDoc e)
189         {
190             log.error(e.toString());
191             throw new ProviderException(e.getMessage());
192         }
193     }
194     
195     public void setMailHost(String JavaDoc mailHost)
196     {
197         this.mailHost = mailHost;
198     }
199
200     public void setPassword(String JavaDoc password)
201     {
202         this.password = password;
203     }
204
205     public void setUseMailAuthenticator(boolean useMailAuthenticator)
206     {
207         this.useMailAuthenticator = useMailAuthenticator;
208     }
209
210     public void setUserName(String JavaDoc userName)
211     {
212         this.userName = userName;
213     }
214
215     public void setPropertiesProvider(PropertiesProvider propertiesProvider)
216     {
217         this.propertiesProvider = propertiesProvider;
218     }
219
220 }
Popular Tags