001: package org.claros.commons.mail.protocols;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.util.ArrayList;
006: import java.util.Date;
007: import java.util.HashMap;
008: import java.util.Properties;
009:
010: import javax.activation.DataHandler;
011: import javax.activation.DataSource;
012: import javax.mail.Address;
013: import javax.mail.BodyPart;
014: import javax.mail.Message;
015: import javax.mail.SendFailedException;
016: import javax.mail.Session;
017: import javax.mail.Transport;
018: import javax.mail.internet.MimeBodyPart;
019: import javax.mail.internet.MimeMultipart;
020: import javax.mail.internet.MimeUtility;
021:
022: import org.claros.commons.auth.models.AuthProfile;
023: import org.claros.commons.configuration.PropertyFile;
024: import org.claros.commons.mail.models.ByteArrayDataSource;
025: import org.claros.commons.mail.models.ConnectionProfile;
026: import org.claros.commons.mail.models.Email;
027: import org.claros.commons.mail.models.EmailPart;
028: import org.claros.commons.mail.models.EmailPriority;
029: import org.claros.commons.mail.models.EmailSensitivity;
030: import org.claros.commons.mail.utility.SmtpAuthenticator;
031:
032: import com.sun.mail.smtp.SMTPMessage;
033:
034: /**
035: * @author Umut Gokbayrak
036: *
037: */
038: public class Smtp {
039:
040: private Session session = null;
041:
042: public Smtp(ConnectionProfile profile, AuthProfile auth) {
043: Properties props = new Properties();
044: props.put("mail.smtp.host", profile.getSmtpServer());
045: props.put("mail.smtp.port", Integer.toString(profile
046: .getISmtpPort()));
047:
048: if (profile.getSmtpAuthenticated() != null
049: && profile.getSmtpAuthenticated().equals("true")) {
050: props.setProperty("mail.smtp.auth", "true");
051: SmtpAuthenticator authenticator = new SmtpAuthenticator(
052: auth.getUsername(), auth.getPassword());
053: session = Session.getInstance(props, authenticator);
054: } else {
055: session = Session.getInstance(props, null);
056: }
057: }
058:
059: public HashMap send(Email msg, boolean simulate) throws Exception {
060: Address from = msg.getBaseHeader().getFrom()[0];
061: Address[] to = msg.getBaseHeader().getTo();
062: Address[] cc = msg.getBaseHeader().getCc();
063: Address[] bcc = msg.getBaseHeader().getBcc();
064: Address[] replyTo = msg.getBaseHeader().getReplyTo();
065: Boolean requestReceiptNotification = msg.getBaseHeader()
066: .getRequestReceiptNotification();
067: short priority = msg.getBaseHeader().getPriority();
068: short sensitivity = msg.getBaseHeader().getSensitivity();
069:
070: SMTPMessage mimeMsg = new SMTPMessage(session);
071: String subject = msg.getBaseHeader().getSubject();
072:
073: mimeMsg.setFrom(from);
074: if (to != null) {
075: mimeMsg.setRecipients(Message.RecipientType.TO, to);
076: }
077: if (cc != null) {
078: mimeMsg.setRecipients(Message.RecipientType.CC, cc);
079: }
080: if (bcc != null) {
081: mimeMsg.setRecipients(Message.RecipientType.BCC, bcc);
082: }
083: if (replyTo != null) {
084: mimeMsg.setReplyTo(replyTo);
085: }
086:
087: mimeMsg.setSentDate(new Date());
088: if (subject == null || subject.length() == 0) {
089: subject = "No subject";
090: }
091:
092: if (requestReceiptNotification != null) {
093: mimeMsg.addHeader("Disposition-Notification-To", from
094: .toString());
095: }
096:
097: if (priority > 0) {
098: mimeMsg.addHeader("X-Priority", String.valueOf(priority));
099: mimeMsg.addHeader("X-MSMail-Priority", EmailPriority
100: .toStringValue(priority));
101: }
102:
103: if (sensitivity > 0) {
104: mimeMsg.addHeader("Sensitivity", EmailSensitivity
105: .toStringValue(sensitivity));
106: }
107:
108: String charset = PropertyFile.getConfiguration(
109: "/config/config.xml")
110: .getString("common-params.charset");
111:
112: mimeMsg.setSubject(MimeUtility.encodeText(subject, charset,
113: null));
114: ArrayList parts = msg.getParts();
115: EmailPart bodyPart = (EmailPart) parts.get(0);
116:
117: boolean isTextBody = (bodyPart.isHTMLText()) ? false : true;
118:
119: if (parts.size() == 1 && isTextBody) {
120: mimeMsg.setText((String) bodyPart.getContent(), charset);
121: } else {
122: BodyPart bp = new MimeBodyPart();
123: bp.setContent((String) bodyPart.getContent(), bodyPart
124: .getContentType());
125: bp.setHeader("Content-Type", bodyPart.getContentType());
126:
127: // attachments are added here.
128: MimeMultipart multipart = new MimeMultipart();
129: multipart.addBodyPart(bp);
130:
131: // text body is added if it is a HTML message.
132: /*
133: if (!isTextBody) {
134: BodyPart bp2 = new MimeBodyPart();
135: bp2.setHeader("Content-Type", "text/plain; charset=" + charset);
136: String txtBody = (String)bodyPart.getContent();
137: txtBody = Utility.replaceAllOccurances(txtBody, "<br />", "\n");
138: txtBody = Utility.replaceAllOccurances(txtBody, "<p>", "\n");
139: txtBody = Utility.replaceAllOccurances(txtBody, "</p>", "\n");
140: txtBody = Utility.replaceAllOccurances(txtBody, " ", " ");
141: txtBody = Utility.replaceAllOccurances(txtBody, "ü", "ü");
142: txtBody = Utility.replaceAllOccurances(txtBody, "Ü", "Ãœ");
143: txtBody = Utility.replaceAllOccurances(txtBody, "ç", "ç");
144: txtBody = Utility.replaceAllOccurances(txtBody, "Ç", "Ç");
145: txtBody = Utility.replaceAllOccurances(txtBody, "ö", "ö");
146: txtBody = Utility.replaceAllOccurances(txtBody, "Ö", "Ö");
147: bp2.setContent(org.claros.commons.mail.utility.Utility.stripHTMLTags(txtBody), "text/plain; charset=" + charset);
148: multipart.addBodyPart(bp2);
149: }
150: */
151:
152: // other attachments will follow
153: MimeBodyPart attPart = null;
154: EmailPart myPart = null;
155: DataSource ds = null;
156: String tmpContType = null;
157: int pos = -1;
158: for (int i = 1; i < msg.getParts().size(); i++) {
159: myPart = (EmailPart) msg.getParts().get(i);
160: attPart = new MimeBodyPart();
161:
162: ds = myPart.getDataSource();
163: if (ds == null) {
164: if (myPart.getContent() instanceof ByteArrayOutputStream) {
165: ByteArrayOutputStream bos = (ByteArrayOutputStream) myPart
166: .getContent();
167: ds = new ByteArrayDataSource(bos.toByteArray(),
168: myPart.getContentType(), myPart
169: .getFileName());
170: attPart.setDataHandler(new DataHandler(ds));
171: bos.close();
172: } else if (myPart.getContent() instanceof ByteArrayInputStream) {
173: ByteArrayInputStream bis = (ByteArrayInputStream) myPart
174: .getContent();
175: ByteArrayOutputStream bos = new ByteArrayOutputStream();
176: int j = -1;
177: while ((j = bis.read()) != -1) {
178: bos.write(j);
179: }
180: ds = new ByteArrayDataSource(bos.toByteArray(),
181: myPart.getContentType(), myPart
182: .getFileName());
183: attPart.setDataHandler(new DataHandler(ds));
184: bos.close();
185: bis.close();
186: } else {
187: attPart.setContent(myPart.getContent(), myPart
188: .getContentType());
189: }
190: } else {
191: attPart.setDataHandler(new DataHandler(ds));
192: }
193:
194: attPart.setDisposition(myPart.getDisposition());
195: attPart.setFileName(MimeUtility.encodeText(myPart
196: .getFilename(), charset, null));
197:
198: tmpContType = (myPart.getContentType() == null) ? "application/octet-stream"
199: : myPart.getContentType();
200:
201: pos = tmpContType.indexOf(";");
202: if (pos >= 0) {
203: tmpContType = tmpContType.substring(0, pos);
204: }
205: attPart.setHeader("Content-Type", tmpContType);
206: multipart.addBodyPart(attPart);
207: }
208:
209: // setting the content and finished
210: mimeMsg.setContent(multipart);
211: }
212: mimeMsg.addHeader("X-Mailer",
213: "Claros inTouch (http://www.claros.org)");
214: mimeMsg.saveChanges();
215:
216: // we are sending the message and generating a sent report on the fly.
217: HashMap out = new HashMap();
218: out.put("msg", mimeMsg);
219: if (!simulate) {
220: try {
221: mimeMsg.setSendPartial(true);
222: mimeMsg.setSentDate(new Date());
223: Transport.send(mimeMsg);
224: Address[] sent = mimeMsg.getAllRecipients();
225: out.put("sent", sent);
226: } catch (SendFailedException se) {
227: Address[] sent = se.getValidSentAddresses();
228: Address[] invalid = se.getInvalidAddresses();
229: Address[] fail = se.getValidUnsentAddresses();
230: out.put("sent", sent);
231: out.put("invalid", invalid);
232: out.put("fail", fail);
233: }
234: }
235: return out;
236: }
237: }
|