001: /*
002: Copyright (c) 2003 eInnovation Inc. All rights reserved
003:
004: This library is free software; you can redistribute it and/or modify it under the terms
005: of the GNU Lesser General Public License as published by the Free Software Foundation;
006: either version 2.1 of the License, or (at your option) any later version.
007:
008: This library 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.
010: See the GNU Lesser General Public License for more details.
011: */
012:
013: package com.openedit.modules.email;
014:
015: import java.io.UnsupportedEncodingException;
016: import java.util.Date;
017: import java.util.Properties;
018:
019: import javax.mail.BodyPart;
020: import javax.mail.Message;
021: import javax.mail.MessagingException;
022: import javax.mail.Multipart;
023: import javax.mail.PasswordAuthentication;
024: import javax.mail.Session;
025: import javax.mail.Transport;
026: import javax.mail.internet.InternetAddress;
027: import javax.mail.internet.MimeBodyPart;
028: import javax.mail.internet.MimeMessage;
029: import javax.mail.internet.MimeMultipart;
030:
031: import com.openedit.OpenEditRuntimeException;
032: import com.openedit.page.manage.PageManager;
033:
034: public class PostMail {
035: protected String fieldSmtpUsername;
036: protected String fieldSmtpPassword;
037: protected String fieldSmtpServer = "localhost";
038: protected int fieldPort = 25;
039: protected boolean fieldSmtpSecured = false;
040: protected PageManager fieldPageManager;
041:
042: public PageManager getPageManager() {
043: return fieldPageManager;
044: }
045:
046: public void setPageManager(PageManager inPageManager) {
047: fieldPageManager = inPageManager;
048: }
049:
050: public String getSmtpPassword() {
051: return fieldSmtpPassword;
052: }
053:
054: public void setSmtpPassword(String inSmtpPassword) {
055: this .fieldSmtpPassword = inSmtpPassword;
056: }
057:
058: public boolean isSmtpSecured() {
059: return fieldSmtpSecured;
060: }
061:
062: public void setSmtpSecured(boolean inSmtpSecured) {
063: this .fieldSmtpSecured = inSmtpSecured;
064: }
065:
066: public String getSmtpUsername() {
067: return fieldSmtpUsername;
068: }
069:
070: public void setSmtpUsername(String inSmtpUsername) {
071: this .fieldSmtpUsername = inSmtpUsername;
072: }
073:
074: public void postMail(String recipient, String subject,
075: String message, String from) throws MessagingException {
076: postMail(new String[] { recipient }, subject, message, null,
077: from);
078: }
079:
080: //returns a new template web email instance preconfigured with sping settings.
081: public TemplateWebEmail getTemplateWebEmail() {
082: TemplateWebEmail email = new TemplateWebEmail();
083: email.setPostMail(this );
084: email.setPageManager(getPageManager());
085: return email;
086: }
087:
088: public void postMail(String[] recipients, String subject,
089: String inHtml, String inText, String from)
090: throws MessagingException {
091: //Set the host smtp address
092: Properties props = new Properties();
093: // create some properties and get the default Session
094: props.put("mail.smtp.host", fieldSmtpServer);
095: props.put("mail.smtp.port", String.valueOf(fieldPort));
096: props.put("mail.smtp.auth", new Boolean(fieldSmtpSecured)
097: .toString());
098: //props.put("mail.host", server);
099: Session session;
100: //If we need to authenticate, create the authenticator
101: if (fieldSmtpSecured) {
102:
103: SmtpAuthenticator auth = new SmtpAuthenticator();
104: session = Session.getDefaultInstance(props, auth);
105: } else {
106: session = Session.getDefaultInstance(props);
107: }
108: //session.setDebug(debug);
109:
110: // create a message
111: Message msg = new MimeMessage(session);
112:
113: //msg.setDataHandler(new DataHandler(new ByteArrayDataSource(message, "text/html")));
114: if (inText != null && inHtml != null) {
115: // Create an "Alternative" Multipart message
116: Multipart mp = new MimeMultipart("alternative");
117:
118: BodyPart messageBodyPart = new MimeBodyPart();
119: messageBodyPart.setContent(inText, "text/plain");
120: mp.addBodyPart(messageBodyPart);
121:
122: messageBodyPart = new MimeBodyPart();
123: messageBodyPart.setContent(inHtml, "text/html");
124: mp.addBodyPart(messageBodyPart);
125:
126: msg.setContent(mp);
127: } else if (inHtml != null) {
128: msg.setContent(inHtml, "text/html");
129: } else {
130: msg.setContent(inText, "text/plain");
131: }
132: // set the from and to address
133: InternetAddress addressFrom = new InternetAddress(from);
134: msg.setFrom(addressFrom);
135: msg.setSentDate(new Date());
136: if (recipients == null || recipients.length == 0) {
137: throw new MessagingException("No recipients specified");
138: }
139: InternetAddress[] addressTo = new InternetAddress[recipients.length];
140:
141: for (int i = 0; i < recipients.length; i++) {
142: String rec = recipients[i];
143: addressTo[i] = new InternetAddress(rec);
144: String personal = addressTo[i].getPersonal();
145: if (personal != null && personal.indexOf("\"") == -1) {
146: //check for commas or . and quote it if found
147: if (personal.indexOf(",") > -1
148: || personal.indexOf(".") > -1) {
149: personal = "\"" + personal + "\"";
150: try {
151: addressTo[i].setPersonal(personal);
152: } catch (UnsupportedEncodingException ex) {
153: throw new OpenEditRuntimeException(ex);
154: }
155: }
156: }
157: }
158:
159: msg.setRecipients(Message.RecipientType.TO, addressTo);
160:
161: // Optional : You can also set your custom headers in the Email if you Want
162: // msg.addHeader("MyHeaderName", "myHeaderValue");
163: // Setting the Subject and Content Type
164: msg.setSubject(subject);
165:
166: //Transport tr = session.getTransport("smtp");
167: //tr.connect(serverandport[0], null, null);
168: //msg.saveChanges(); // don't forget this
169: //tr.sendMessage(msg, msg.getAllRecipients());
170: //tr.close();
171: //msg.setContent(msg, "text/plain");
172:
173: Transport.send(msg);
174: }
175:
176: public int getPort() {
177: return fieldPort;
178: }
179:
180: public void setPort(int inPort) {
181: this .fieldPort = inPort;
182: }
183:
184: public String getSmtpServer() {
185: return fieldSmtpServer;
186: }
187:
188: public void setSmtpServer(String inSmtpServer) {
189: this .fieldSmtpServer = inSmtpServer;
190: }
191:
192: public class SmtpAuthenticator extends javax.mail.Authenticator {
193: public PasswordAuthentication getPasswordAuthentication() {
194: return new PasswordAuthentication(fieldSmtpUsername,
195: fieldSmtpPassword);
196: }
197: }
198: }
|