| 1 |
|
package com.ozacc.mail.impl; |
| 2 |
|
|
| 3 |
|
import java.io.UnsupportedEncodingException; |
| 4 |
|
import java.util.Date; |
| 5 |
|
import java.util.Properties; |
| 6 |
|
|
| 7 |
|
import javax.mail.AuthenticationFailedException; |
| 8 |
|
import javax.mail.MessagingException; |
| 9 |
|
import javax.mail.Session; |
| 10 |
|
import javax.mail.Transport; |
| 11 |
|
import javax.mail.internet.InternetAddress; |
| 12 |
|
import javax.mail.internet.MimeMessage; |
| 13 |
|
|
| 14 |
|
import org.apache.commons.logging.Log; |
| 15 |
|
import org.apache.commons.logging.LogFactory; |
| 16 |
|
|
| 17 |
|
import com.ozacc.mail.Mail; |
| 18 |
|
import com.ozacc.mail.MailAuthenticationException; |
| 19 |
|
import com.ozacc.mail.MailBuildException; |
| 20 |
|
import com.ozacc.mail.MailException; |
| 21 |
|
import com.ozacc.mail.MailSendException; |
| 22 |
|
import com.ozacc.mail.SendMail; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
public class SendMailImpl implements SendMail { |
| 32 |
|
|
| 33 |
|
private static Log log = LogFactory.getLog(SendMailImpl.class); |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
public static final String DEFAULT_PROTOCOL = "smtp"; |
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
public static final int DEFAULT_PORT = -1; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
public static final String DEFAULT_HOST = "localhost"; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public static final String JIS_CHARSET = "ISO-2022-JP"; |
| 49 |
|
|
| 50 |
|
private static final String RETURN_PATH_KEY = "mail.smtp.from"; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
private static final int DEFAULT_CONNECTION_TIMEOUT = 5000; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private static final int DEFAULT_READ_TIMEOUT = 5000; |
| 57 |
|
|
| 58 |
|
private String protocol = DEFAULT_PROTOCOL; |
| 59 |
|
|
| 60 |
|
private String host = DEFAULT_HOST; |
| 61 |
|
|
| 62 |
|
private int port = DEFAULT_PORT; |
| 63 |
|
|
| 64 |
|
private String username; |
| 65 |
|
|
| 66 |
|
private String password; |
| 67 |
|
|
| 68 |
|
private String charset = JIS_CHARSET; |
| 69 |
|
|
| 70 |
|
private String returnPath; |
| 71 |
|
|
| 72 |
|
private String messageId; |
| 73 |
|
|
| 74 |
|
private int connectionTimeout = DEFAULT_CONNECTION_TIMEOUT; |
| 75 |
|
|
| 76 |
|
private int readTimeout = DEFAULT_READ_TIMEOUT; |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
public SendMailImpl() {} |
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
public SendMailImpl(String host) { |
| 89 |
|
this(); |
| 90 |
|
setHost(host); |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
public void send(Mail mail) throws MailException { |
| 97 |
|
send(new Mail[] { mail }); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
public void send(Mail[] mails) throws MailException { |
| 104 |
|
MimeMessageWrapper[] mmws = new MimeMessageWrapper[mails.length]; |
| 105 |
|
Session session = Session.getInstance(new Properties()); |
| 106 |
|
for (int i = 0; i < mails.length; i++) { |
| 107 |
|
Mail mail = mails[i]; |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
MimeMessage message = createMimeMessage(session); |
| 111 |
|
MimeMessageBuilder builder = new MimeMessageBuilder(message); |
| 112 |
|
try { |
| 113 |
|
builder.buildMimeMessage(mail); |
| 114 |
|
} catch (UnsupportedEncodingException e) { |
| 115 |
|
throw new MailBuildException("¥µ¥Ý¡¼¥È¤µ¤?¤Æ¤¤¤Ê¤¤Ê¸»ú¥³¡¼¥É¤¬»ØÄꤵ¤?¤Þ¤·¤¿¡£", e); |
| 116 |
|
} catch (MessagingException e) { |
| 117 |
|
throw new MailBuildException("MimeMessage¤ÎÀ¸À®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
String returnPath; |
| 122 |
|
if (mail.getReturnPath() != null) { |
| 123 |
|
returnPath = mail.getReturnPath().getAddress(); |
| 124 |
|
} else { |
| 125 |
|
returnPath = this.returnPath; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
mmws[i] = new MimeMessageWrapper(message, returnPath, mail.getEnvelopeTo()); |
| 129 |
|
} |
| 130 |
|
processSend(mmws); |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
public void send(MimeMessage message) throws MailException { |
| 137 |
|
send(new MimeMessage[] { message }); |
| 138 |
|
} |
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
public void send(MimeMessage[] messages) throws MailException { |
| 144 |
|
MimeMessageWrapper[] mmws = new MimeMessageWrapper[messages.length]; |
| 145 |
|
for (int i = 0; i < messages.length; i++) { |
| 146 |
|
mmws[i] = new MimeMessageWrapper(messages[i], returnPath); |
| 147 |
|
} |
| 148 |
|
processSend(mmws); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
private void processSend(MimeMessageWrapper[] mmws) throws MailException { |
| 152 |
|
|
| 153 |
|
Properties prop = new Properties(); |
| 154 |
|
|
| 155 |
|
prop.put("mail.smtp.connectiontimeout", String.valueOf(connectionTimeout)); |
| 156 |
|
prop.put("mail.smtp.timeout", String.valueOf(readTimeout)); |
| 157 |
|
Session session = Session.getInstance(prop); |
| 158 |
|
|
| 159 |
|
Transport transport = null; |
| 160 |
|
try { |
| 161 |
|
|
| 162 |
|
log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤¹¡£"); |
| 163 |
|
transport = session.getTransport(protocol); |
| 164 |
|
transport.connect(host, port, username, password); |
| 165 |
|
log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤ËÀܳ¤·¤Þ¤·¤¿¡£"); |
| 166 |
|
|
| 167 |
|
for (int i = 0; i < mmws.length; i++) { |
| 168 |
|
MimeMessage mimeMessage = mmws[i].getMimeMessage(); |
| 169 |
|
|
| 170 |
|
String returnPath = mmws[i].getReturnPath(); |
| 171 |
|
if (returnPath != null) { |
| 172 |
|
session.getProperties().put(RETURN_PATH_KEY, returnPath); |
| 173 |
|
log.debug("Return-Path[" + returnPath + "]¤òÀßÄꤷ¤Þ¤·¤¿¡£"); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
mimeMessage.setSentDate(new Date()); |
| 177 |
|
mimeMessage.saveChanges(); |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
log.debug("¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤¹¡£"); |
| 181 |
|
if (mmws[i].hasEnvelopeTo()) { |
| 182 |
|
log.debug("¥á¡¼¥?¤Ïenvelope-to¥¢¥É¥?¥¹¤ËÁ÷¿®¤µ¤?¤Þ¤¹¡£"); |
| 183 |
|
transport.sendMessage(mimeMessage, mmws[i].getEnvelopeTo()); |
| 184 |
|
} else { |
| 185 |
|
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); |
| 186 |
|
} |
| 187 |
|
log.debug("¥á¡¼¥?¤òÁ÷¿®¤·¤Þ¤·¤¿¡£"); |
| 188 |
|
|
| 189 |
|
|
| 190 |
|
if (returnPath != null) { |
| 191 |
|
session.getProperties().remove(RETURN_PATH_KEY); |
| 192 |
|
log.debug("Return-PathÀßÄê¤ò¥¯¥?¥¢¤·¤Þ¤·¤¿¡£"); |
| 193 |
|
} |
| 194 |
|
} |
| 195 |
|
} catch (AuthenticationFailedException ex) { |
| 196 |
|
log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤Ø¤ÎÀܳǧ¾Ú¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex); |
| 197 |
|
throw new MailAuthenticationException(ex); |
| 198 |
|
} catch (MessagingException ex) { |
| 199 |
|
log.error("¥á¡¼¥?¤ÎÁ÷¿®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex); |
| 200 |
|
throw new MailSendException("¥á¡¼¥?¤ÎÁ÷¿®¤Ë¼ºÇÔ¤·¤Þ¤·¤¿¡£", ex); |
| 201 |
|
} finally { |
| 202 |
|
if (transport != null && transport.isConnected()) { |
| 203 |
|
log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤¹¡£"); |
| 204 |
|
try { |
| 205 |
|
|
| 206 |
|
transport.close(); |
| 207 |
|
} catch (MessagingException e) { |
| 208 |
|
log.error("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£", e); |
| 209 |
|
throw new MailException("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳÀÚÃǤ˼ºÇÔ¤·¤Þ¤·¤¿¡£"); |
| 210 |
|
} |
| 211 |
|
log.debug("SMTP¥µ¡¼¥Ð[" + host + "]¤È¤ÎÀܳ¤òÀÚÃǤ·¤Þ¤·¤¿¡£"); |
| 212 |
|
} |
| 213 |
|
} |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
|
| 217 |
|
|
| 218 |
|
|
| 219 |
|
|
| 220 |
|
|
| 221 |
|
|
| 222 |
|
private MimeMessage createMimeMessage(Session session) { |
| 223 |
|
if (isMessageIdCustomized()) { |
| 224 |
|
return new OMLMimeMessage(session, messageId); |
| 225 |
|
} |
| 226 |
|
return new MimeMessage(session); |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
private boolean isMessageIdCustomized() { |
| 235 |
|
return messageId != null; |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
public String getCharset() { |
| 244 |
|
return charset; |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
|
| 250 |
|
|
| 251 |
|
|
| 252 |
|
|
| 253 |
|
|
| 254 |
|
|
| 255 |
|
public void setCharset(String charset) { |
| 256 |
|
this.charset = charset; |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
|
| 260 |
|
|
| 261 |
|
|
| 262 |
|
|
| 263 |
|
|
| 264 |
|
public String getHost() { |
| 265 |
|
return host; |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
public void setHost(String host) { |
| 275 |
|
this.host = host; |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
|
| 279 |
|
|
| 280 |
|
|
| 281 |
|
public String getPassword() { |
| 282 |
|
return password; |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
|
| 288 |
|
|
| 289 |
|
|
| 290 |
|
public void setPassword(String password) { |
| 291 |
|
this.password = password; |
| 292 |
|
} |
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
public int getPort() { |
| 298 |
|
return port; |
| 299 |
|
} |
| 300 |
|
|
| 301 |
|
|
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
public void setPort(int port) { |
| 307 |
|
this.port = port; |
| 308 |
|
} |
| 309 |
|
|
| 310 |
|
|
| 311 |
|
|
| 312 |
|
|
| 313 |
|
|
| 314 |
|
|
| 315 |
|
public String getProtocol() { |
| 316 |
|
return protocol; |
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
|
| 320 |
|
|
| 321 |
|
|
| 322 |
|
|
| 323 |
|
|
| 324 |
|
public void setProtocol(String protocol) { |
| 325 |
|
this.protocol = protocol; |
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
|
| 329 |
|
|
| 330 |
|
|
| 331 |
|
public String getReturnPath() { |
| 332 |
|
return class="keyword">returnPath; |
| 333 |
|
} |
| 334 |
|
|
| 335 |
|
|
| 336 |
|
|
| 337 |
|
|
| 338 |
|
|
| 339 |
|
|
| 340 |
|
|
| 341 |
|
|
| 342 |
|
|
| 343 |
|
public void setReturnPath(String returnPath) { |
| 344 |
|
this.returnPath = returnPath; |
| 345 |
|
} |
| 346 |
|
|
| 347 |
|
|
| 348 |
|
|
| 349 |
|
|
| 350 |
|
public String getUsername() { |
| 351 |
|
return username; |
| 352 |
|
} |
| 353 |
|
|
| 354 |
|
|
| 355 |
|
|
| 356 |
|
|
| 357 |
|
|
| 358 |
|
|
| 359 |
|
public void setUsername(String username) { |
| 360 |
|
this.username = username; |
| 361 |
|
} |
| 362 |
|
|
| 363 |
|
|
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
|
| 368 |
|
|
| 369 |
|
|
| 370 |
|
|
| 371 |
|
|
| 372 |
|
public void setConnectionTimeout(int connectionTimeout) { |
| 373 |
|
this.connectionTimeout = connectionTimeout; |
| 374 |
|
} |
| 375 |
|
|
| 376 |
|
|
| 377 |
|
|
| 378 |
|
|
| 379 |
|
|
| 380 |
|
|
| 381 |
|
|
| 382 |
|
|
| 383 |
|
|
| 384 |
|
|
| 385 |
|
|
| 386 |
|
public void setReadTimeout(int readTimeout) { |
| 387 |
|
this.readTimeout = readTimeout; |
| 388 |
|
} |
| 389 |
|
|
| 390 |
|
|
| 391 |
|
|
| 392 |
|
|
| 393 |
|
|
| 394 |
|
|
| 395 |
|
|
| 396 |
|
|
| 397 |
|
|
| 398 |
|
|
| 399 |
|
|
| 400 |
|
|
| 401 |
|
|
| 402 |
|
|
| 403 |
|
|
| 404 |
|
|
| 405 |
|
|
| 406 |
|
|
| 407 |
|
|
| 408 |
|
|
| 409 |
|
|
| 410 |
|
|
| 411 |
|
public void setMessageId(String messageId) { |
| 412 |
|
if (messageId == null || messageId.length() < 1) { |
| 413 |
|
return; |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
String[] parts = messageId.split("@"); |
| 417 |
|
if (parts.length > 2) { |
| 418 |
|
throw new IllegalArgumentException("messageId¥×¥úÁѥƥ£¤Ë'@'¤òÊ£¿ô´Þ¤à¤³¤È¤Ï¤Ç¤¤Þ¤»¤ó¡£[" + messageId |
| 419 |
|
+ "]"); |
| 420 |
|
} |
| 421 |
|
|
| 422 |
|
this.messageId = messageId; |
| 423 |
|
} |
| 424 |
|
|
| 425 |
|
|
| 426 |
|
|
| 427 |
|
|
| 428 |
|
|
| 429 |
|
|
| 430 |
|
|
| 431 |
|
private static class MimeMessageWrapper { |
| 432 |
|
|
| 433 |
|
private MimeMessage mimeMessage; |
| 434 |
|
|
| 435 |
|
private String returnPath; |
| 436 |
|
|
| 437 |
|
private InternetAddress[] envelopeTo; |
| 438 |
|
|
| 439 |
0 |
public MimeMessageWrapper(MimeMessage mimeMessage, String returnPath) { |
| 440 |
0 |
this.mimeMessage = mimeMessage; |
| 441 |
0 |
this.returnPath = returnPath; |
| 442 |
0 |
} |
| 443 |
|
|
| 444 |
3 |
public MimeMessageWrapper(MimeMessage mimeMessage, String returnPath, |
| 445 |
|
InternetAddress[] envelopeTo) { |
| 446 |
3 |
this.mimeMessage = mimeMessage; |
| 447 |
3 |
this.returnPath = returnPath; |
| 448 |
3 |
this.envelopeTo = envelopeTo; |
| 449 |
3 |
} |
| 450 |
|
|
| 451 |
|
public MimeMessage getMimeMessage() { |
| 452 |
3 |
return mimeMessage; |
| 453 |
|
} |
| 454 |
|
|
| 455 |
|
public String getReturnPath() { |
| 456 |
3 |
return class="keyword">returnPath; |
| 457 |
|
} |
| 458 |
|
|
| 459 |
|
public boolean hasEnvelopeTo() { |
| 460 |
3 |
return envelopeTo.length > 0; |
| 461 |
|
} |
| 462 |
|
|
| 463 |
|
public InternetAddress[] getEnvelopeTo() { |
| 464 |
0 |
return envelopeTo; |
| 465 |
|
} |
| 466 |
|
} |
| 467 |
|
|
| 468 |
|
} |