1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.email; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.providers.AbstractMessageDispatcher; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.EndpointException; |
18 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
19 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
20 | |
import org.mule.umo.provider.DispatchException; |
21 | |
|
22 | |
import java.util.Calendar; |
23 | |
|
24 | |
import javax.mail.Message; |
25 | |
import javax.mail.MessagingException; |
26 | |
import javax.mail.Transport; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class SmtpMessageDispatcher extends AbstractMessageDispatcher |
35 | |
{ |
36 | |
private volatile Transport transport; |
37 | |
|
38 | |
public SmtpMessageDispatcher(UMOImmutableEndpoint endpoint) |
39 | |
{ |
40 | 8 | super(endpoint); |
41 | 8 | } |
42 | |
|
43 | |
private SmtpConnector castConnector() |
44 | |
{ |
45 | 8 | return (SmtpConnector) getConnector(); |
46 | |
} |
47 | |
|
48 | |
protected void doConnect() throws Exception |
49 | |
{ |
50 | 8 | if (transport == null) |
51 | |
{ |
52 | |
try |
53 | |
{ |
54 | 8 | transport = castConnector().getSessionDetails(endpoint).newTransport(); |
55 | 8 | UMOEndpointURI uri = endpoint.getEndpointURI(); |
56 | 8 | transport.connect(uri.getHost(), uri.getPort(), uri.getUsername(), uri.getPassword()); |
57 | |
} |
58 | 2 | catch (Exception e) |
59 | |
{ |
60 | 2 | throw new EndpointException( |
61 | |
org.mule.config.i18n.MessageFactory.createStaticMessage("Unable to connect to mail transport."), |
62 | |
e); |
63 | 6 | } |
64 | |
} |
65 | 6 | } |
66 | |
|
67 | |
protected void doDisconnect() throws Exception |
68 | |
{ |
69 | 6 | if (null != transport) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 6 | transport.close(); |
74 | |
} |
75 | |
finally |
76 | |
{ |
77 | 6 | transport = null; |
78 | 6 | } |
79 | |
} |
80 | 6 | } |
81 | |
|
82 | |
protected void doDispatch(UMOEvent event) throws Exception |
83 | |
{ |
84 | 6 | Object data = event.getTransformedMessage(); |
85 | |
|
86 | 6 | if (!(data instanceof Message)) |
87 | |
{ |
88 | 0 | throw new DispatchException( |
89 | 0 | CoreMessages.transformUnexpectedType(data.getClass(), Message.class), |
90 | |
event.getMessage(), event.getEndpoint()); |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | |
|
95 | 6 | sendMailMessage((Message) data); |
96 | |
} |
97 | 6 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
protected UMOMessage doReceive(long timeout) throws Exception |
111 | |
{ |
112 | 0 | throw new UnsupportedOperationException("doReceive"); |
113 | |
} |
114 | |
|
115 | |
protected UMOMessage doSend(UMOEvent event) throws Exception |
116 | |
{ |
117 | 0 | doDispatch(event); |
118 | 0 | return null; |
119 | |
} |
120 | |
|
121 | |
protected void sendMailMessage(Message message) throws MessagingException |
122 | |
{ |
123 | |
|
124 | 6 | message.setSentDate(Calendar.getInstance().getTime()); |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | 6 | if (!transport.isConnected()) |
131 | |
{ |
132 | 0 | UMOEndpointURI uri = endpoint.getEndpointURI(); |
133 | 0 | if (logger.isInfoEnabled()) |
134 | |
{ |
135 | 0 | logger.info("Connection closed by remote server. Reconnecting."); |
136 | |
} |
137 | 0 | transport.connect(uri.getHost(), uri.getPort(), uri.getUsername(), uri.getPassword()); |
138 | |
} |
139 | |
|
140 | 6 | transport.sendMessage(message, message.getAllRecipients()); |
141 | |
|
142 | 6 | if (logger.isDebugEnabled()) |
143 | |
{ |
144 | 0 | StringBuffer msg = new StringBuffer(); |
145 | 0 | msg.append("Email message sent with subject'").append(message.getSubject()).append("' sent- "); |
146 | 0 | msg.append(", From: ").append(MailUtils.mailAddressesToString(message.getFrom())).append(" "); |
147 | 0 | msg.append(", To: ").append( |
148 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.TO))).append(" "); |
149 | 0 | msg.append(", Cc: ").append( |
150 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.CC))).append(" "); |
151 | 0 | msg.append(", Bcc: ") |
152 | |
.append(MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.BCC))) |
153 | |
.append(" "); |
154 | 0 | msg.append(", ReplyTo: ").append(MailUtils.mailAddressesToString(message.getReplyTo())); |
155 | |
|
156 | 0 | logger.debug(msg.toString()); |
157 | |
} |
158 | |
|
159 | 6 | } |
160 | |
|
161 | |
protected void doDispose() |
162 | |
{ |
163 | |
|
164 | 8 | } |
165 | |
|
166 | |
} |