1 16 17 package org.springframework.mail.javamail; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.Arrays ; 22 import java.util.Date ; 23 import java.util.List ; 24 import java.util.Properties ; 25 26 import javax.activation.FileTypeMap ; 27 import javax.mail.Address ; 28 import javax.mail.Message ; 29 import javax.mail.MessagingException ; 30 import javax.mail.NoSuchProviderException ; 31 import javax.mail.Session ; 32 import javax.mail.Transport ; 33 import javax.mail.URLName ; 34 import javax.mail.internet.AddressException ; 35 import javax.mail.internet.InternetAddress ; 36 import javax.mail.internet.MimeMessage ; 37 38 import junit.framework.TestCase; 39 40 import org.springframework.mail.MailParseException; 41 import org.springframework.mail.MailSendException; 42 import org.springframework.mail.SimpleMailMessage; 43 44 48 public class JavaMailSenderTests extends TestCase { 49 50 public void testJavaMailSenderWithSimpleMessage() throws MessagingException , IOException { 51 MockJavaMailSender sender = new MockJavaMailSender(); 52 sender.setHost("host"); 53 sender.setPort(30); 54 sender.setUsername("username"); 55 sender.setPassword("password"); 56 57 SimpleMailMessage simpleMessage = new SimpleMailMessage(); 58 simpleMessage.setFrom("me@mail.org"); 59 simpleMessage.setReplyTo("reply@mail.org"); 60 simpleMessage.setTo("you@mail.org"); 61 simpleMessage.setCc(new String [] {"he@mail.org", "she@mail.org"}); 62 simpleMessage.setBcc(new String [] {"us@mail.org", "them@mail.org"}); 63 Date sentDate = new Date (2004, 1, 1); 64 simpleMessage.setSentDate(sentDate); 65 simpleMessage.setSubject("my subject"); 66 simpleMessage.setText("my text"); 67 sender.send(simpleMessage); 68 69 assertEquals("host", sender.transport.getConnectedHost()); 70 assertEquals(30, sender.transport.getConnectedPort()); 71 assertEquals("username", sender.transport.getConnectedUsername()); 72 assertEquals("password", sender.transport.getConnectedPassword()); 73 assertTrue(sender.transport.isCloseCalled()); 74 75 assertEquals(1, sender.transport.getSentMessages().size()); 76 MimeMessage sentMessage = sender.transport.getSentMessage(0); 77 List froms = Arrays.asList(sentMessage.getFrom()); 78 assertEquals(1, froms.size()); 79 assertEquals("me@mail.org", ((InternetAddress ) froms.get(0)).getAddress()); 80 List replyTos = Arrays.asList(sentMessage.getReplyTo()); 81 assertEquals("reply@mail.org", ((InternetAddress ) replyTos.get(0)).getAddress()); 82 List tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO)); 83 assertEquals(1, tos.size()); 84 assertEquals("you@mail.org", ((InternetAddress ) tos.get(0)).getAddress()); 85 List ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC)); 86 assertEquals(2, ccs.size()); 87 assertEquals("he@mail.org", ((InternetAddress ) ccs.get(0)).getAddress()); 88 assertEquals("she@mail.org", ((InternetAddress ) ccs.get(1)).getAddress()); 89 List bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC)); 90 assertEquals(2, bccs.size()); 91 assertEquals("us@mail.org", ((InternetAddress ) bccs.get(0)).getAddress()); 92 assertEquals("them@mail.org", ((InternetAddress ) bccs.get(1)).getAddress()); 93 assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime()); 94 assertEquals("my subject", sentMessage.getSubject()); 95 assertEquals("my text", sentMessage.getContent()); 96 } 97 98 public void testJavaMailSenderWithSimpleMessages() throws MessagingException , IOException { 99 MockJavaMailSender sender = new MockJavaMailSender(); 100 sender.setHost("host"); 101 sender.setUsername("username"); 102 sender.setPassword("password"); 103 104 SimpleMailMessage simpleMessage1 = new SimpleMailMessage(); 105 simpleMessage1.setTo("he@mail.org"); 106 SimpleMailMessage simpleMessage2 = new SimpleMailMessage(); 107 simpleMessage2.setTo("she@mail.org"); 108 sender.send(new SimpleMailMessage[] {simpleMessage1, simpleMessage2}); 109 110 assertEquals(sender.transport.getConnectedHost(), "host"); 111 assertEquals(sender.transport.getConnectedUsername(), "username"); 112 assertEquals(sender.transport.getConnectedPassword(), "password"); 113 assertTrue(sender.transport.isCloseCalled()); 114 115 assertEquals(2, sender.transport.getSentMessages().size()); 116 MimeMessage sentMessage1 = sender.transport.getSentMessage(0); 117 List tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO)); 118 assertEquals(1, tos1.size()); 119 assertEquals("he@mail.org", ((InternetAddress ) tos1.get(0)).getAddress()); 120 MimeMessage sentMessage2 = sender.transport.getSentMessage(1); 121 List tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO)); 122 assertEquals(1, tos2.size()); 123 assertEquals("she@mail.org", ((InternetAddress ) tos2.get(0)).getAddress()); 124 } 125 126 public void testJavaMailSenderWithMimeMessage() throws MessagingException { 127 MockJavaMailSender sender = new MockJavaMailSender(); 128 sender.setHost("host"); 129 sender.setUsername("username"); 130 sender.setPassword("password"); 131 132 MimeMessage mimeMessage = sender.createMimeMessage(); 133 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ("you@mail.org")); 134 sender.send(mimeMessage); 135 136 assertEquals(sender.transport.getConnectedHost(), "host"); 137 assertEquals(sender.transport.getConnectedUsername(), "username"); 138 assertEquals(sender.transport.getConnectedPassword(), "password"); 139 assertTrue(sender.transport.isCloseCalled()); 140 assertEquals(1, sender.transport.getSentMessages().size()); 141 assertEquals(mimeMessage, sender.transport.getSentMessage(0)); 142 } 143 144 public void testJavaMailSenderWithMimeMessages() throws MessagingException { 145 MockJavaMailSender sender = new MockJavaMailSender(); 146 sender.setHost("host"); 147 sender.setUsername("username"); 148 sender.setPassword("password"); 149 150 MimeMessage mimeMessage1 = sender.createMimeMessage(); 151 mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress ("he@mail.org")); 152 MimeMessage mimeMessage2 = sender.createMimeMessage(); 153 mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress ("she@mail.org")); 154 sender.send(new MimeMessage [] {mimeMessage1, mimeMessage2}); 155 156 assertEquals(sender.transport.getConnectedHost(), "host"); 157 assertEquals(sender.transport.getConnectedUsername(), "username"); 158 assertEquals(sender.transport.getConnectedPassword(), "password"); 159 assertTrue(sender.transport.isCloseCalled()); 160 assertEquals(2, sender.transport.getSentMessages().size()); 161 assertEquals(mimeMessage1, sender.transport.getSentMessage(0)); 162 assertEquals(mimeMessage2, sender.transport.getSentMessage(1)); 163 } 164 165 public void testJavaMailSenderWithMimeMessagePreparator() { 166 MockJavaMailSender sender = new MockJavaMailSender(); 167 sender.setHost("host"); 168 sender.setUsername("username"); 169 sender.setPassword("password"); 170 171 final List messages = new ArrayList (); 172 173 MimeMessagePreparator preparator = new MimeMessagePreparator() { 174 public void prepare(MimeMessage mimeMessage) throws MessagingException { 175 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ("you@mail.org")); 176 messages.add(mimeMessage); 177 } 178 }; 179 sender.send(preparator); 180 181 assertEquals(sender.transport.getConnectedHost(), "host"); 182 assertEquals(sender.transport.getConnectedUsername(), "username"); 183 assertEquals(sender.transport.getConnectedPassword(), "password"); 184 assertTrue(sender.transport.isCloseCalled()); 185 assertEquals(1, sender.transport.getSentMessages().size()); 186 assertEquals(messages.get(0), sender.transport.getSentMessage(0)); 187 } 188 189 public void testJavaMailSenderWithMimeMessagePreparators() { 190 MockJavaMailSender sender = new MockJavaMailSender(); 191 sender.setHost("host"); 192 sender.setUsername("username"); 193 sender.setPassword("password"); 194 195 final List messages = new ArrayList (); 196 197 MimeMessagePreparator preparator1 = new MimeMessagePreparator() { 198 public void prepare(MimeMessage mimeMessage) throws MessagingException { 199 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ("he@mail.org")); 200 messages.add(mimeMessage); 201 } 202 }; 203 MimeMessagePreparator preparator2 = new MimeMessagePreparator() { 204 public void prepare(MimeMessage mimeMessage) throws MessagingException { 205 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ("she@mail.org")); 206 messages.add(mimeMessage); 207 } 208 }; 209 sender.send(new MimeMessagePreparator[] {preparator1, preparator2}); 210 211 assertEquals(sender.transport.getConnectedHost(), "host"); 212 assertEquals(sender.transport.getConnectedUsername(), "username"); 213 assertEquals(sender.transport.getConnectedPassword(), "password"); 214 assertTrue(sender.transport.isCloseCalled()); 215 assertEquals(2, sender.transport.getSentMessages().size()); 216 assertEquals(messages.get(0), sender.transport.getSentMessage(0)); 217 assertEquals(messages.get(1), sender.transport.getSentMessage(1)); 218 } 219 220 public void testJavaMailSenderWithMimeMessageHelper() throws MessagingException { 221 MockJavaMailSender sender = new MockJavaMailSender(); 222 sender.setHost("host"); 223 sender.setUsername("username"); 224 sender.setPassword("password"); 225 226 MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage()); 227 assertNull(message.getEncoding()); 228 assertTrue(message.getFileTypeMap() instanceof ConfigurableMimeFileTypeMap); 229 230 message.setTo("you@mail.org"); 231 sender.send(message.getMimeMessage()); 232 233 assertEquals(sender.transport.getConnectedHost(), "host"); 234 assertEquals(sender.transport.getConnectedUsername(), "username"); 235 assertEquals(sender.transport.getConnectedPassword(), "password"); 236 assertTrue(sender.transport.isCloseCalled()); 237 assertEquals(1, sender.transport.getSentMessages().size()); 238 assertEquals(message.getMimeMessage(), sender.transport.getSentMessage(0)); 239 } 240 241 public void testJavaMailSenderWithMimeMessageHelperAndSpecificEncoding() throws MessagingException { 242 MockJavaMailSender sender = new MockJavaMailSender(); 243 sender.setHost("host"); 244 sender.setUsername("username"); 245 sender.setPassword("password"); 246 247 MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage(), "UTF-8"); 248 assertEquals("UTF-8", message.getEncoding()); 249 FileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap(); 250 message.setFileTypeMap(fileTypeMap); 251 assertEquals(fileTypeMap, message.getFileTypeMap()); 252 253 message.setTo("you@mail.org"); 254 sender.send(message.getMimeMessage()); 255 256 assertEquals(sender.transport.getConnectedHost(), "host"); 257 assertEquals(sender.transport.getConnectedUsername(), "username"); 258 assertEquals(sender.transport.getConnectedPassword(), "password"); 259 assertTrue(sender.transport.isCloseCalled()); 260 assertEquals(1, sender.transport.getSentMessages().size()); 261 assertEquals(message.getMimeMessage(), sender.transport.getSentMessage(0)); 262 } 263 264 public void testJavaMailSenderWithMimeMessageHelperAndDefaultEncoding() throws MessagingException { 265 MockJavaMailSender sender = new MockJavaMailSender(); 266 sender.setHost("host"); 267 sender.setUsername("username"); 268 sender.setPassword("password"); 269 sender.setDefaultEncoding("UTF-8"); 270 271 FileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap(); 272 sender.setDefaultFileTypeMap(fileTypeMap); 273 MimeMessageHelper message = new MimeMessageHelper(sender.createMimeMessage()); 274 assertEquals("UTF-8", message.getEncoding()); 275 assertEquals(fileTypeMap, message.getFileTypeMap()); 276 277 message.setTo("you@mail.org"); 278 sender.send(message.getMimeMessage()); 279 280 assertEquals(sender.transport.getConnectedHost(), "host"); 281 assertEquals(sender.transport.getConnectedUsername(), "username"); 282 assertEquals(sender.transport.getConnectedPassword(), "password"); 283 assertTrue(sender.transport.isCloseCalled()); 284 assertEquals(1, sender.transport.getSentMessages().size()); 285 assertEquals(message.getMimeMessage(), sender.transport.getSentMessage(0)); 286 } 287 288 public void testJavaMailSenderWithParseExceptionOnSimpleMessage() { 289 MockJavaMailSender sender = new MockJavaMailSender(); 290 SimpleMailMessage simpleMessage = new SimpleMailMessage(); 291 simpleMessage.setFrom(""); 292 try { 293 sender.send(simpleMessage); 294 } 295 catch (MailParseException ex) { 296 assertTrue(ex.getCause() instanceof AddressException ); 298 } 299 } 300 301 public void testJavaMailSenderWithParseExceptionOnMimeMessagePreparator() { 302 MockJavaMailSender sender = new MockJavaMailSender(); 303 MimeMessagePreparator preparator = new MimeMessagePreparator() { 304 public void prepare(MimeMessage mimeMessage) throws MessagingException { 305 mimeMessage.setFrom(new InternetAddress ("")); 306 } 307 }; 308 try { 309 sender.send(preparator); 310 } 311 catch (MailParseException ex) { 312 assertTrue(ex.getCause() instanceof AddressException ); 314 } 315 } 316 317 public void testJavaMailSenderWithCustomSession() throws MessagingException { 318 final Session session = Session.getInstance(new Properties ()); 319 MockJavaMailSender sender = new MockJavaMailSender() { 320 protected Transport getTransport(Session sess) throws NoSuchProviderException { 321 assertEquals(session, sess); 322 return super.getTransport(sess); 323 } 324 }; 325 sender.setSession(session); 326 sender.setHost("host"); 327 sender.setUsername("username"); 328 sender.setPassword("password"); 329 330 MimeMessage mimeMessage = sender.createMimeMessage(); 331 mimeMessage.setSubject("custom"); 332 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ("you@mail.org")); 333 mimeMessage.setSentDate(new Date (2005, 3, 1)); 334 sender.send(mimeMessage); 335 336 assertEquals(sender.transport.getConnectedHost(), "host"); 337 assertEquals(sender.transport.getConnectedUsername(), "username"); 338 assertEquals(sender.transport.getConnectedPassword(), "password"); 339 assertTrue(sender.transport.isCloseCalled()); 340 assertEquals(1, sender.transport.getSentMessages().size()); 341 assertEquals(mimeMessage, sender.transport.getSentMessage(0)); 342 } 343 344 public void testJavaMailProperties() throws MessagingException { 345 Properties props = new Properties (); 346 props.setProperty("bogusKey", "bogusValue"); 347 MockJavaMailSender sender = new MockJavaMailSender() { 348 protected Transport getTransport(Session sess) throws NoSuchProviderException { 349 assertEquals("bogusValue", sess.getProperty("bogusKey")); 350 return super.getTransport(sess); 351 } 352 }; 353 sender.setJavaMailProperties(props); 354 sender.setHost("host"); 355 sender.setUsername("username"); 356 sender.setPassword("password"); 357 358 MimeMessage mimeMessage = sender.createMimeMessage(); 359 mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress ("you@mail.org")); 360 sender.send(mimeMessage); 361 362 assertEquals(sender.transport.getConnectedHost(), "host"); 363 assertEquals(sender.transport.getConnectedUsername(), "username"); 364 assertEquals(sender.transport.getConnectedPassword(), "password"); 365 assertTrue(sender.transport.isCloseCalled()); 366 assertEquals(1, sender.transport.getSentMessages().size()); 367 assertEquals(mimeMessage, sender.transport.getSentMessage(0)); 368 } 369 370 public void testFailedMailServerConnect() throws Exception { 371 MockJavaMailSender sender = new MockJavaMailSender(); 372 sender.setHost(null); 373 sender.setUsername("username"); 374 sender.setPassword("password"); 375 SimpleMailMessage simpleMessage1 = new SimpleMailMessage(); 376 try { 377 sender.send(simpleMessage1); 378 fail("Should have thrown MailSendException"); 379 } 380 catch (MailSendException ex) { 381 assertTrue(ex.getFailedMessages() != null); 383 assertTrue(ex.getFailedMessages().isEmpty()); 384 } 385 } 386 387 public void testFailedSimpleMessage() throws Exception { 388 MockJavaMailSender sender = new MockJavaMailSender(); 389 sender.setHost("host"); 390 sender.setUsername("username"); 391 sender.setPassword("password"); 392 393 SimpleMailMessage simpleMessage1 = new SimpleMailMessage(); 394 simpleMessage1.setTo("he@mail.org"); 395 simpleMessage1.setSubject("fail"); 396 SimpleMailMessage simpleMessage2 = new SimpleMailMessage(); 397 simpleMessage2.setTo("she@mail.org"); 398 399 try { 400 sender.send(new SimpleMailMessage[] {simpleMessage1, simpleMessage2}); 401 } 402 catch (MailSendException ex) { 403 assertEquals(sender.transport.getConnectedHost(), "host"); 404 assertEquals(sender.transport.getConnectedUsername(), "username"); 405 assertEquals(sender.transport.getConnectedPassword(), "password"); 406 assertTrue(sender.transport.isCloseCalled()); 407 assertEquals(1, sender.transport.getSentMessages().size()); 408 assertEquals(new InternetAddress ("she@mail.org"), sender.transport.getSentMessage(0).getAllRecipients()[0]); 409 assertEquals(1, ex.getFailedMessages().size()); 410 assertEquals(simpleMessage1, ex.getFailedMessages().keySet().iterator().next()); 411 Object subEx = ex.getFailedMessages().values().iterator().next(); 412 assertTrue(subEx instanceof MessagingException ); 413 assertEquals("failed", ((MessagingException ) subEx).getMessage()); 414 } 415 } 416 417 public void testFailedMimeMessage() throws Exception { 418 MockJavaMailSender sender = new MockJavaMailSender(); 419 sender.setHost("host"); 420 sender.setUsername("username"); 421 sender.setPassword("password"); 422 423 MimeMessage mimeMessage1 = sender.createMimeMessage(); 424 mimeMessage1.setRecipient(Message.RecipientType.TO, new InternetAddress ("he@mail.org")); 425 mimeMessage1.setSubject("fail"); 426 MimeMessage mimeMessage2 = sender.createMimeMessage(); 427 mimeMessage2.setRecipient(Message.RecipientType.TO, new InternetAddress ("she@mail.org")); 428 429 try { 430 sender.send(new MimeMessage [] {mimeMessage1, mimeMessage2}); 431 } 432 catch (MailSendException ex) { 433 assertEquals(sender.transport.getConnectedHost(), "host"); 434 assertEquals(sender.transport.getConnectedUsername(), "username"); 435 assertEquals(sender.transport.getConnectedPassword(), "password"); 436 assertTrue(sender.transport.isCloseCalled()); 437 assertEquals(1, sender.transport.getSentMessages().size()); 438 assertEquals(mimeMessage2, sender.transport.getSentMessage(0)); 439 assertEquals(1, ex.getFailedMessages().size()); 440 assertEquals(mimeMessage1, ex.getFailedMessages().keySet().iterator().next()); 441 Object subEx = ex.getFailedMessages().values().iterator().next(); 442 assertTrue(subEx instanceof MessagingException ); 443 assertEquals("failed", ((MessagingException ) subEx).getMessage()); 444 } 445 } 446 447 448 private static class MockJavaMailSender extends JavaMailSenderImpl { 449 450 private MockTransport transport; 451 452 protected Transport getTransport(Session session) throws NoSuchProviderException { 453 this.transport = new MockTransport(session, null); 454 return transport; 455 } 456 } 457 458 459 private static class MockTransport extends Transport { 460 461 private String connectedHost = null; 462 private int connectedPort = -2; 463 private String connectedUsername = null; 464 private String connectedPassword = null; 465 private boolean closeCalled = false; 466 private List sentMessages = new ArrayList (); 467 468 public MockTransport(Session session, URLName urlName) { 469 super(session, urlName); 470 } 471 472 public String getConnectedHost() { 473 return connectedHost; 474 } 475 476 public int getConnectedPort() { 477 return connectedPort; 478 } 479 480 public String getConnectedUsername() { 481 return connectedUsername; 482 } 483 484 public String getConnectedPassword() { 485 return connectedPassword; 486 } 487 488 public boolean isCloseCalled() { 489 return closeCalled; 490 } 491 492 public List getSentMessages() { 493 return sentMessages; 494 } 495 496 public MimeMessage getSentMessage(int index) { 497 return (MimeMessage ) this.sentMessages.get(index); 498 } 499 500 public void connect(String host, int port, String username, String password) throws MessagingException { 501 if (host == null) { 502 throw new MessagingException ("no host"); 503 } 504 this.connectedHost = host; 505 this.connectedPort = port; 506 this.connectedUsername = username; 507 this.connectedPassword = password; 508 } 509 510 public synchronized void close() throws MessagingException { 511 this.closeCalled = true; 512 } 513 514 public void sendMessage(Message message, Address [] addresses) throws MessagingException { 515 if ("fail".equals(message.getSubject())) { 516 throw new MessagingException ("failed"); 517 } 518 List addr1 = Arrays.asList(message.getAllRecipients()); 519 List addr2 = Arrays.asList(addresses); 520 if (!addr1.equals(addr2)) { 521 throw new MessagingException ("addresses not correct"); 522 } 523 if (message.getSentDate() == null) { 524 throw new MessagingException ("No sentDate specified"); 525 } 526 if (message.getSubject() != null && message.getSubject().indexOf("custom") != -1) { 527 assertEquals(new Date (2005, 3, 1), message.getSentDate()); 528 } 529 this.sentMessages.add(message); 530 } 531 } 532 533 } 534 | Popular Tags |