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