001: /*
002: * $Id: SmtpMessageDispatcher.java 10961 2008-02-22 19:01:02Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.email;
012:
013: import org.mule.api.MuleEvent;
014: import org.mule.api.MuleMessage;
015: import org.mule.api.endpoint.EndpointException;
016: import org.mule.api.endpoint.EndpointURI;
017: import org.mule.api.endpoint.OutboundEndpoint;
018: import org.mule.api.transport.DispatchException;
019: import org.mule.config.i18n.CoreMessages;
020: import org.mule.transport.AbstractMessageDispatcher;
021:
022: import java.util.Calendar;
023:
024: import javax.mail.Message;
025: import javax.mail.MessagingException;
026: import javax.mail.Transport;
027:
028: /**
029: * <code>SmtpMessageDispatcher</code> will dispatch Mule events as Mime email
030: * messages over an SMTP gateway.
031: *
032: * This contains a reference to a transport (and endpoint and connector, via superclasses)
033: */
034: public class SmtpMessageDispatcher extends AbstractMessageDispatcher {
035: private volatile Transport transport;
036:
037: public SmtpMessageDispatcher(OutboundEndpoint endpoint) {
038: super (endpoint);
039: }
040:
041: private SmtpConnector castConnector() {
042: return (SmtpConnector) getConnector();
043: }
044:
045: protected void doConnect() throws Exception {
046: if (transport == null) {
047: try {
048: transport = castConnector().getSessionDetails(endpoint)
049: .newTransport();
050: EndpointURI uri = endpoint.getEndpointURI();
051: transport.connect(uri.getHost(), uri.getPort(), uri
052: .getUser(), uri.getPassword());
053: } catch (Exception e) {
054: throw new EndpointException(
055: org.mule.config.i18n.MessageFactory
056: .createStaticMessage("Unable to connect to mail transport."),
057: e);
058: }
059: }
060: }
061:
062: protected void doDisconnect() throws Exception {
063: if (null != transport) {
064: try {
065: transport.close();
066: } finally {
067: transport = null;
068: }
069: }
070: }
071:
072: protected void doDispatch(MuleEvent event) throws Exception {
073: Object data = event.transformMessage();
074:
075: if (!(data instanceof Message)) {
076: throw new DispatchException(CoreMessages
077: .transformUnexpectedType(data.getClass(),
078: Message.class), event.getMessage(), event
079: .getEndpoint());
080: } else {
081: // Check the message for any unset data and use defaults
082: sendMailMessage((Message) data);
083: }
084: }
085:
086: protected MuleMessage doSend(MuleEvent event) throws Exception {
087: doDispatch(event);
088: return null;
089: }
090:
091: protected void sendMailMessage(Message message)
092: throws MessagingException {
093: // sent date
094: message.setSentDate(Calendar.getInstance().getTime());
095:
096: /*
097: * Double check that the transport is still connected as some SMTP servers may
098: * disconnect idle connections.
099: */
100: if (!transport.isConnected()) {
101: EndpointURI uri = endpoint.getEndpointURI();
102: if (logger.isInfoEnabled()) {
103: logger
104: .info("Connection closed by remote server. Reconnecting.");
105: }
106: transport.connect(uri.getHost(), uri.getPort(), uri
107: .getUser(), uri.getPassword());
108: }
109:
110: transport.sendMessage(message, message.getAllRecipients());
111:
112: if (logger.isDebugEnabled()) {
113: StringBuffer msg = new StringBuffer();
114: msg.append("Email message sent with subject'").append(
115: message.getSubject()).append("' sent- ");
116: msg.append(", From: ").append(
117: MailUtils.mailAddressesToString(message.getFrom()))
118: .append(" ");
119: msg.append(", To: ").append(
120: MailUtils.mailAddressesToString(message
121: .getRecipients(Message.RecipientType.TO)))
122: .append(" ");
123: msg.append(", Cc: ").append(
124: MailUtils.mailAddressesToString(message
125: .getRecipients(Message.RecipientType.CC)))
126: .append(" ");
127: msg.append(", Bcc: ").append(
128: MailUtils.mailAddressesToString(message
129: .getRecipients(Message.RecipientType.BCC)))
130: .append(" ");
131: msg.append(", ReplyTo: ").append(
132: MailUtils.mailAddressesToString(message
133: .getReplyTo()));
134:
135: logger.debug(msg.toString());
136: }
137:
138: }
139:
140: protected void doDispose() {
141: // nothing doing
142: }
143:
144: }
|