001: /*
002: * Copyright (C) 2003 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it under the terms of the
005: * GNU General Public License as published by the Free Software Foundation; either version 2 of the
006: * License, or (at your option) any later version.
007: *
008: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
009: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
010: * the GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License along with this program; if
013: * not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
014: * 02111-1307, USA.
015: *
016: */
017:
018: package org.efs.openreports.providers;
019:
020: import java.util.ArrayList;
021: import java.util.Date;
022: import java.util.Properties;
023: import java.util.StringTokenizer;
024:
025: import javax.activation.DataHandler;
026: import javax.activation.DataSource;
027: import javax.activation.FileDataSource;
028: import javax.mail.*;
029: import javax.mail.Message.RecipientType;
030: import javax.mail.internet.*;
031:
032: import org.apache.log4j.Logger;
033:
034: import org.efs.openreports.objects.MailMessage;
035: import org.efs.openreports.objects.ORProperty;
036: import org.efs.openreports.util.ByteArrayDataSource;
037: import org.efs.openreports.util.SMTPAuthenticator;
038:
039: public class MailProvider {
040: protected static Logger log = Logger.getLogger(MailProvider.class
041: .getName());
042:
043: private PropertiesProvider propertiesProvider;
044:
045: private String mailHost;
046: private boolean useMailAuthenticator;
047: private String userName;
048: private String password;
049: private Session mailSession;
050:
051: public MailProvider(PropertiesProvider propertiesProvider)
052: throws ProviderException {
053: this .propertiesProvider = propertiesProvider;
054: init();
055: }
056:
057: protected void init() throws ProviderException {
058: mailHost = null;
059: useMailAuthenticator = false;
060: userName = null;
061: password = null;
062:
063: ORProperty property = propertiesProvider
064: .getProperty(ORProperty.MAIL_SMTP_HOST);
065: if (property != null)
066: mailHost = property.getValue();
067:
068: property = propertiesProvider
069: .getProperty(ORProperty.MAIL_SMTP_AUTH);
070: if (property != null)
071: useMailAuthenticator = Boolean.valueOf(property.getValue())
072: .booleanValue();
073:
074: property = propertiesProvider
075: .getProperty(ORProperty.MAIL_AUTH_USER);
076: if (property != null)
077: userName = property.getValue();
078:
079: property = propertiesProvider
080: .getProperty(ORProperty.MAIL_AUTH_PASSWORD);
081: if (property != null)
082: password = property.getValue();
083:
084: log.info("Created: Use Mail Authenticator = "
085: + useMailAuthenticator);
086: }
087:
088: public void sendMail(MailMessage mail) throws ProviderException {
089: try {
090: if (mailSession == null) {
091: // create a session
092: Properties mailProps = new Properties();
093: mailProps.put("mail.smtp.host", mailHost);
094:
095: if (mail.getBounceAddress() != null
096: && mail.getBounceAddress().trim().length() > 0) {
097: mailProps.setProperty("mail.smtp.from", mail
098: .getBounceAddress());
099: }
100:
101: if (useMailAuthenticator) {
102: mailSession = Session.getInstance(mailProps,
103: new SMTPAuthenticator(userName, password));
104: } else {
105: mailSession = Session.getInstance(mailProps, null);
106: }
107: }
108:
109: // create multipart
110: Multipart multipart = new MimeMultipart();
111:
112: // add text part
113: if (mail.getText() != null) {
114: MimeBodyPart mbpText = new MimeBodyPart();
115: mbpText.setText(mail.getText());
116: multipart.addBodyPart(mbpText);
117: }
118:
119: // add file attachments
120: ArrayList<String> attachments = mail.getAttachments();
121: for (int i = 0; i < attachments.size(); i++) {
122: String fileAttachment = attachments.get(i);
123: FileDataSource source = new FileDataSource(
124: fileAttachment);
125:
126: MimeBodyPart mbpAttachment = new MimeBodyPart();
127: mbpAttachment.setDataHandler(new DataHandler(source));
128: mbpAttachment.setFileName(fileAttachment);
129:
130: multipart.addBodyPart(mbpAttachment);
131: }
132:
133: // add byteArrayAttachment
134: if (mail.getByteArrayDataSource() != null) {
135: String contentType = mail.getByteArrayDataSource()
136: .getContentType();
137: if (contentType != null
138: && contentType.equals("text/html")) {
139: Multipart htmlMP = new MimeMultipart("related");
140: MimeBodyPart htmlBP = new MimeBodyPart();
141: htmlBP.setDataHandler(new DataHandler(mail
142: .getByteArrayDataSource()));
143: htmlMP.addBodyPart(htmlBP);
144:
145: // Add images
146: ArrayList<ByteArrayDataSource> images = mail
147: .getHtmlImageDataSources();
148: for (int i = 0; i < images.size(); i++) {
149: DataSource imageDS = images.get(i);
150:
151: MimeBodyPart imageBodyPart = new MimeBodyPart();
152: imageBodyPart.setFileName(imageDS.getName());
153: imageBodyPart.setText(imageDS.getName());
154: imageBodyPart.setDataHandler(new DataHandler(
155: imageDS));
156: imageBodyPart.setHeader("Content-ID", "<"
157: + imageDS.getName() + ">");
158: imageBodyPart
159: .setDisposition(javax.mail.Part.INLINE);
160:
161: htmlMP.addBodyPart(imageBodyPart);
162: }
163:
164: BodyPart completeHtmlBP = new MimeBodyPart();
165: completeHtmlBP.setContent(htmlMP);
166: multipart.addBodyPart(completeHtmlBP);
167: } else {
168: MimeBodyPart mbpAttachment = new MimeBodyPart();
169: mbpAttachment.setDataHandler(new DataHandler(mail
170: .getByteArrayDataSource()));
171: mbpAttachment.setFileName(mail
172: .getByteArrayDataSource().getName());
173:
174: multipart.addBodyPart(mbpAttachment);
175: }
176: }
177:
178: // create message
179: Message msg = new MimeMessage(mailSession);
180: msg.setFrom(new InternetAddress(mail.getSender()));
181: msg.setSubject(mail.getSubject());
182: msg.setContent(multipart);
183: msg.setSentDate(new Date());
184:
185: ArrayList<String> recipients = mail.getRecipients();
186: for (int i = 0; i < recipients.size(); i++) {
187: RecipientType recipientType = RecipientType.TO;
188:
189: StringTokenizer tokenizer = new StringTokenizer(
190: recipients.get(i), ":");
191: if (tokenizer.countTokens() == 2) {
192: String type = tokenizer.nextToken();
193: if ("TO".equalsIgnoreCase(type)) {
194: recipientType = RecipientType.TO;
195: } else if ("CC".equalsIgnoreCase(type)) {
196: recipientType = RecipientType.CC;
197: } else if ("BCC".equalsIgnoreCase(type)) {
198: recipientType = RecipientType.BCC;
199: }
200: }
201:
202: msg.addRecipient(recipientType, new InternetAddress(
203: tokenizer.nextToken()));
204: }
205:
206: Transport.send(msg);
207: } catch (Exception e) {
208: e.printStackTrace();
209: log.error(e.toString());
210: throw new ProviderException(e.getMessage());
211: }
212: }
213:
214: public void setMailHost(String mailHost) {
215: this .mailHost = mailHost;
216: }
217:
218: public void setPassword(String password) {
219: this .password = password;
220: }
221:
222: public void setUseMailAuthenticator(boolean useMailAuthenticator) {
223: this .useMailAuthenticator = useMailAuthenticator;
224: }
225:
226: public void setUserName(String userName) {
227: this .userName = userName;
228: }
229:
230: public void setPropertiesProvider(
231: PropertiesProvider propertiesProvider) {
232: this .propertiesProvider = propertiesProvider;
233: }
234:
235: public void setMailSession(Session mailSession) {
236: this.mailSession = mailSession;
237: }
238:
239: }
|