1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.email; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.endpoint.EndpointException; |
16 | |
import org.mule.api.endpoint.EndpointURI; |
17 | |
import org.mule.api.endpoint.OutboundEndpoint; |
18 | |
import org.mule.api.transport.DispatchException; |
19 | |
import org.mule.config.i18n.CoreMessages; |
20 | |
import org.mule.transport.AbstractMessageDispatcher; |
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(OutboundEndpoint endpoint) |
39 | |
{ |
40 | 14 | super(endpoint); |
41 | 14 | } |
42 | |
|
43 | |
private SmtpConnector castConnector() |
44 | |
{ |
45 | 14 | return (SmtpConnector) getConnector(); |
46 | |
} |
47 | |
|
48 | |
protected void doConnect() throws Exception |
49 | |
{ |
50 | 14 | if (transport == null) |
51 | |
{ |
52 | |
try |
53 | |
{ |
54 | 14 | transport = castConnector().getSessionDetails(endpoint).newTransport(); |
55 | 14 | EndpointURI uri = endpoint.getEndpointURI(); |
56 | 14 | transport.connect(uri.getHost(), uri.getPort(), uri.getUser(), 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 | 12 | } |
64 | |
} |
65 | 12 | } |
66 | |
|
67 | |
protected void doDisconnect() throws Exception |
68 | |
{ |
69 | 12 | if (null != transport) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 12 | transport.close(); |
74 | |
} |
75 | |
finally |
76 | |
{ |
77 | 12 | transport = null; |
78 | 12 | } |
79 | |
} |
80 | 12 | } |
81 | |
|
82 | |
protected void doDispatch(MuleEvent event) throws Exception |
83 | |
{ |
84 | 12 | Object data = event.transformMessage(); |
85 | |
|
86 | 12 | if (!(data instanceof Message)) |
87 | |
{ |
88 | 0 | throw new DispatchException( |
89 | |
CoreMessages.transformUnexpectedType(data.getClass(), Message.class), |
90 | |
event.getMessage(), event.getEndpoint()); |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | |
|
95 | 12 | sendMailMessage((Message) data); |
96 | |
} |
97 | 12 | } |
98 | |
|
99 | |
protected MuleMessage doSend(MuleEvent event) throws Exception |
100 | |
{ |
101 | 6 | doDispatch(event); |
102 | 6 | return null; |
103 | |
} |
104 | |
|
105 | |
protected void sendMailMessage(Message message) throws MessagingException |
106 | |
{ |
107 | |
|
108 | 12 | message.setSentDate(Calendar.getInstance().getTime()); |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | 12 | if (!transport.isConnected()) |
115 | |
{ |
116 | 0 | EndpointURI uri = endpoint.getEndpointURI(); |
117 | 0 | if (logger.isInfoEnabled()) |
118 | |
{ |
119 | 0 | logger.info("Connection closed by remote server. Reconnecting."); |
120 | |
} |
121 | 0 | transport.connect(uri.getHost(), uri.getPort(), uri.getUser(), uri.getPassword()); |
122 | |
} |
123 | |
|
124 | 12 | transport.sendMessage(message, message.getAllRecipients()); |
125 | |
|
126 | 12 | if (logger.isDebugEnabled()) |
127 | |
{ |
128 | 0 | StringBuffer msg = new StringBuffer(); |
129 | 0 | msg.append("Email message sent with subject'").append(message.getSubject()).append("' sent- "); |
130 | 0 | msg.append(", From: ").append(MailUtils.mailAddressesToString(message.getFrom())).append(" "); |
131 | 0 | msg.append(", To: ").append( |
132 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.TO))).append(" "); |
133 | 0 | msg.append(", Cc: ").append( |
134 | |
MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.CC))).append(" "); |
135 | 0 | msg.append(", Bcc: ") |
136 | |
.append(MailUtils.mailAddressesToString(message.getRecipients(Message.RecipientType.BCC))) |
137 | |
.append(" "); |
138 | 0 | msg.append(", ReplyTo: ").append(MailUtils.mailAddressesToString(message.getReplyTo())); |
139 | |
|
140 | 0 | logger.debug(msg.toString()); |
141 | |
} |
142 | |
|
143 | 12 | } |
144 | |
|
145 | |
protected void doDispose() |
146 | |
{ |
147 | |
|
148 | 14 | } |
149 | |
|
150 | |
} |