001: package org.contineo.core.communication;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005: import java.util.Properties;
006:
007: import javax.activation.DataHandler;
008: import javax.activation.DataSource;
009: import javax.activation.FileDataSource;
010: import javax.mail.Address;
011: import javax.mail.Multipart;
012: import javax.mail.Session;
013: import javax.mail.Transport;
014: import javax.mail.internet.InternetAddress;
015: import javax.mail.internet.MimeBodyPart;
016: import javax.mail.internet.MimeMessage;
017: import javax.mail.internet.MimeMultipart;
018:
019: import org.apache.commons.lang.StringUtils;
020: import org.contineo.util.Context;
021: import org.contineo.util.config.SettingsConfig;
022:
023: /**
024: * SMTP E-Mail sender service
025: *
026: * @author Michael Scholz, Marco Meschieri
027: */
028: public class EMailSender {
029:
030: private String host = "localhost";
031:
032: private String defaultAddress = "contineo@acme.com";
033:
034: private String username = "";
035:
036: private String password = "";
037:
038: private int port = 25;
039:
040: public EMailSender() {
041: }
042:
043: public String getDefaultAddress() {
044: return defaultAddress;
045: }
046:
047: public void setDefaultAddress(String fromAddress) {
048: this .defaultAddress = fromAddress;
049: }
050:
051: public String getHost() {
052: return host;
053: }
054:
055: public void setHost(String host) {
056: this .host = host;
057: }
058:
059: public String getPassword() {
060: return password;
061: }
062:
063: public void setPassword(String password) {
064: this .password = password;
065: }
066:
067: public int getPort() {
068: return port;
069: }
070:
071: public void setPort(int port) {
072: this .port = port;
073: }
074:
075: public String getUsername() {
076: return username;
077: }
078:
079: public void setUsername(String username) {
080: this .username = username;
081: }
082:
083: /**
084: * This method sends an email using the smtp-protocol. The email can be a
085: * simple mail or a multipart mail containing up to 5 attachments.
086: *
087: * @param account E-Mail account of the sender.
088: * @param email E-Mail which should be sent.
089: * @throws Exception
090: */
091: public void send(EMail email) throws Exception {
092: Properties props = new Properties();
093: props.put("mail.smtp.host", host);
094: props.put("mail.smtp.auth", "true");
095:
096: Session sess = Session.getDefaultInstance(props);
097: javax.mail.Message message = new MimeMessage(sess);
098: String frm = email.getAuthorAddress();
099: if (StringUtils.isEmpty(frm))
100: frm = defaultAddress;
101: InternetAddress from = new InternetAddress(frm);
102: InternetAddress[] to = email.getAddresses();
103: message.setFrom(from);
104: message.setRecipients(javax.mail.Message.RecipientType.TO, to);
105: message.setSubject(email.getSubject());
106:
107: if (email.getAttachmentCount() > 0) {
108: SettingsConfig conf = (SettingsConfig) Context
109: .getInstance().getBean(SettingsConfig.class);
110: String userdir = conf.getValue("userdir");
111: String filename = userdir + "/mails/"
112: + email.getMessageId() + "/";
113: MimeBodyPart body = new MimeBodyPart();
114: body.setContent(email.getMessageText(), "text/plain");
115:
116: Multipart mpMessage = new MimeMultipart();
117: mpMessage.addBodyPart(body);
118:
119: Collection attachments = email.getAttachments().values();
120: Iterator iter = attachments.iterator();
121:
122: while (iter.hasNext()) {
123: Attachment att = (Attachment) iter.next();
124: DataSource fdSource = new FileDataSource(filename
125: + att.getFilename());
126: DataHandler fdHandler = new DataHandler(fdSource);
127: MimeBodyPart part = new MimeBodyPart();
128: part.setDataHandler(fdHandler);
129: part.setFileName(att.getFilename());
130: mpMessage.addBodyPart(part);
131: }
132:
133: message.setContent(mpMessage);
134: } else {
135: message.setContent(email.getMessageText(), "text/plain");
136: }
137:
138: Transport trans = sess.getTransport("smtp");
139: trans.connect(host, port, username, password);
140:
141: Address[] adr = message.getAllRecipients();
142: trans.sendMessage(message, adr);
143: trans.close();
144: }
145: }
|