1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.email; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.endpoint.EndpointException; |
13 | |
import org.mule.api.endpoint.EndpointURI; |
14 | |
import org.mule.api.endpoint.OutboundEndpoint; |
15 | |
import org.mule.api.transport.DispatchException; |
16 | |
import org.mule.config.i18n.CoreMessages; |
17 | |
import org.mule.config.i18n.MessageFactory; |
18 | |
import org.mule.transport.AbstractMessageDispatcher; |
19 | |
import org.mule.transport.NullPayload; |
20 | |
|
21 | |
import com.sun.mail.smtp.SMTPTransport; |
22 | |
|
23 | |
import java.net.URLDecoder; |
24 | |
import java.util.Calendar; |
25 | |
|
26 | |
import javax.mail.Message; |
27 | |
import javax.mail.MessagingException; |
28 | |
import javax.mail.Transport; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class SmtpMessageDispatcher extends AbstractMessageDispatcher |
37 | |
{ |
38 | |
private volatile Transport transport; |
39 | |
|
40 | |
public SmtpMessageDispatcher(OutboundEndpoint endpoint) |
41 | |
{ |
42 | 0 | super(endpoint); |
43 | 0 | } |
44 | |
|
45 | |
private SmtpConnector castConnector() |
46 | |
{ |
47 | 0 | return (SmtpConnector) getConnector(); |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
protected void doConnect() throws Exception |
52 | |
{ |
53 | 0 | if (transport == null) |
54 | |
{ |
55 | |
try |
56 | |
{ |
57 | |
|
58 | 0 | transport = castConnector().getSessionDetails(endpoint).newTransport(); |
59 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
60 | 0 | String encoding = endpoint.getEncoding(); |
61 | 0 | String user = (uri.getUser()!=null ? URLDecoder.decode(uri.getUser(), encoding) : null); |
62 | 0 | String pass = (uri.getPassword()!=null ? URLDecoder.decode(uri.getPassword(), encoding) : null); |
63 | 0 | transport.connect(uri.getHost(), uri.getPort(), user, pass); |
64 | |
} |
65 | 0 | catch (Exception e) |
66 | |
{ |
67 | 0 | throw new EndpointException( |
68 | |
MessageFactory.createStaticMessage("Unable to connect to mail transport."), e); |
69 | 0 | } |
70 | |
} |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
protected void doDisconnect() throws Exception |
75 | |
{ |
76 | 0 | if (null != transport) |
77 | |
{ |
78 | |
try |
79 | |
{ |
80 | 0 | transport.close(); |
81 | |
} |
82 | |
finally |
83 | |
{ |
84 | 0 | transport = null; |
85 | 0 | } |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
@Override |
90 | |
protected void doDispatch(MuleEvent event) throws Exception |
91 | |
{ |
92 | 0 | Object data = event.getMessage().getPayload(); |
93 | |
|
94 | 0 | if (!(data instanceof Message)) |
95 | |
{ |
96 | 0 | throw new DispatchException( |
97 | |
CoreMessages.transformUnexpectedType(data.getClass(), Message.class), |
98 | |
event, this); |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | |
|
103 | 0 | sendMailMessage((Message) data); |
104 | |
} |
105 | 0 | } |
106 | |
|
107 | |
@Override |
108 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
109 | |
{ |
110 | 0 | doDispatch(event); |
111 | 0 | return new DefaultMuleMessage(NullPayload.getInstance(), connector.getMuleContext()); |
112 | |
} |
113 | |
|
114 | |
protected void sendMailMessage(Message message) throws MessagingException |
115 | |
{ |
116 | |
|
117 | 0 | message.setSentDate(Calendar.getInstance().getTime()); |
118 | |
|
119 | |
|
120 | |
|
121 | 0 | if (isTransportConnected() == false) |
122 | |
{ |
123 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
124 | 0 | if (logger.isInfoEnabled()) |
125 | |
{ |
126 | 0 | logger.info("Connection closed by remote server. Reconnecting."); |
127 | |
} |
128 | 0 | transport.connect(uri.getHost(), uri.getPort(), uri.getUser(), uri.getPassword()); |
129 | |
} |
130 | |
|
131 | 0 | transport.sendMessage(message, message.getAllRecipients()); |
132 | |
|
133 | 0 | if (logger.isDebugEnabled()) |
134 | |
{ |
135 | 0 | StringBuffer msg = new StringBuffer(); |
136 | 0 | msg.append("Email message sent with subject'").append(message.getSubject()).append("' sent- "); |
137 | 0 | msg.append(", From: ").append(MailUtils.mailAddressesToString(message.getFrom())).append(" "); |
138 | 0 | msg.append(", To: ").append( |
139 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.TO))).append(" "); |
140 | 0 | msg.append(", Cc: ").append( |
141 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.CC))).append(" "); |
142 | 0 | msg.append(", Bcc: ") |
143 | |
.append(MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.BCC))) |
144 | |
.append(" "); |
145 | 0 | msg.append(", ReplyTo: ").append(MailUtils.mailAddressesToString(message.getReplyTo())); |
146 | |
|
147 | 0 | logger.debug(msg.toString()); |
148 | |
} |
149 | |
|
150 | 0 | } |
151 | |
|
152 | |
|
153 | |
|
154 | |
protected boolean isTransportConnected() |
155 | |
{ |
156 | 0 | boolean isConnected = false; |
157 | |
|
158 | 0 | isConnected = transport.isConnected(); |
159 | 0 | if (isConnected) |
160 | |
{ |
161 | 0 | SMTPTransport smtpTransport = (SMTPTransport) transport; |
162 | |
|
163 | 0 | String lastServerResponse = smtpTransport.getLastServerResponse(); |
164 | 0 | if (lastServerResponse.startsWith("250") == false) |
165 | |
{ |
166 | 0 | isConnected = false; |
167 | |
try |
168 | |
{ |
169 | 0 | smtpTransport.close(); |
170 | |
} |
171 | 0 | catch (MessagingException me) |
172 | |
{ |
173 | 0 | if (logger.isInfoEnabled()) |
174 | |
{ |
175 | 0 | logger.info("Unable to close SMTP Transport", me); |
176 | |
} |
177 | 0 | } |
178 | |
} |
179 | |
} |
180 | |
|
181 | 0 | return isConnected; |
182 | |
} |
183 | |
|
184 | |
@Override |
185 | |
protected void doDispose() |
186 | |
{ |
187 | |
|
188 | 0 | } |
189 | |
|
190 | |
} |