public List<Address> getRecipients(Message.RecipientType recipientType) throws MessagingException { return Arrays.asList(mimeMessage.getRecipients(recipientType)); }
message.getRecipients(MimeMessage.RecipientType.TO));
putAddressListInAttributes(attributes, EMAIL_HEADER_TO, originalMessage.getRecipients(Message.RecipientType.TO)); putAddressListInAttributes(attributes, EMAIL_HEADER_CC, originalMessage.getRecipients(Message.RecipientType.CC)); putAddressListInAttributes(attributes, EMAIL_HEADER_BCC, originalMessage.getRecipients(Message.RecipientType.BCC));
public ValidationBean send(String subject, String body, String to) { Transport transport = null; try { LOGGER.debug("Sending email [{}] to [{}]", subject, to); Properties props = mailProperties(); MailSession session = MailSession.getInstance().createWith(props, username, password); transport = session.getTransport(); transport.connect(host, port, nullIfEmpty(username), nullIfEmpty(password)); MimeMessage msg = session.createMessage(from, to, subject, body); transport.sendMessage(msg, msg.getRecipients(TO)); return ValidationBean.valid(); } catch (Exception e) { LOGGER.error("Sending failed for email [{}] to [{}]", subject, to, e); return ValidationBean.notValid(ERROR_MESSAGE); } finally { if (transport != null) { try { transport.close(); } catch (MessagingException e) { LOGGER.error("Failed to close transport", e); } } } }
@Test public void testMessageFactorySetRecipients() throws MessagingException { final MimeMessageBuilder builder = new MimeMessageBuilder(null); final String addresses = "testing1@example.com,testing2@example.com"; assertNull(builder.build().getRecipients( Message.RecipientType.TO)); builder.setRecipients(Message.RecipientType.TO, null); assertNull(builder.build().getRecipients( Message.RecipientType.TO)); builder.setRecipients(Message.RecipientType.TO, addresses); assertArrayEquals(InternetAddress.parse(addresses), builder .build().getRecipients(Message.RecipientType.TO)); }
@Test public void javaMailSenderWithSimpleMessages() throws MessagingException { MockJavaMailSender sender = new MockJavaMailSender(); sender.setHost("host"); sender.setUsername("username"); sender.setPassword("password"); SimpleMailMessage simpleMessage1 = new SimpleMailMessage(); simpleMessage1.setTo("he@mail.org"); SimpleMailMessage simpleMessage2 = new SimpleMailMessage(); simpleMessage2.setTo("she@mail.org"); sender.send(simpleMessage1, simpleMessage2); assertEquals("host", sender.transport.getConnectedHost()); assertEquals("username", sender.transport.getConnectedUsername()); assertEquals("password", sender.transport.getConnectedPassword()); assertTrue(sender.transport.isCloseCalled()); assertEquals(2, sender.transport.getSentMessages().size()); MimeMessage sentMessage1 = sender.transport.getSentMessage(0); List<Address> tos1 = Arrays.asList(sentMessage1.getRecipients(Message.RecipientType.TO)); assertEquals(1, tos1.size()); assertEquals("he@mail.org", ((InternetAddress) tos1.get(0)).getAddress()); MimeMessage sentMessage2 = sender.transport.getSentMessage(1); List<Address> tos2 = Arrays.asList(sentMessage2.getRecipients(Message.RecipientType.TO)); assertEquals(1, tos2.size()); assertEquals("she@mail.org", ((InternetAddress) tos2.get(0)).getAddress()); }
List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo()); assertEquals("reply@mail.org", ((InternetAddress) replyTos.get(0)).getAddress()); List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO)); assertEquals(1, tos.size()); assertEquals("you@mail.org", ((InternetAddress) tos.get(0)).getAddress()); List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC)); assertEquals(2, ccs.size()); assertEquals("he@mail.org", ((InternetAddress) ccs.get(0)).getAddress()); assertEquals("she@mail.org", ((InternetAddress) ccs.get(1)).getAddress()); List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC)); assertEquals(2, bccs.size()); assertEquals("us@mail.org", ((InternetAddress) bccs.get(0)).getAddress());
/** * @return the 'to' recipients of the message * @throws Exception determining the recipients failed */ public List<javax.mail.Address> getTo() throws Exception { final javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.TO); return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>(); }
/** * @return the 'bcc' recipients of the message * @throws Exception determining the recipients failed */ public List<javax.mail.Address> getBcc() throws Exception { final javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.BCC); return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>(); }
/** * @return the 'cc' recipients of the message * @throws Exception determining the recipients failed */ public List<javax.mail.Address> getCc() throws Exception { final javax.mail.Address[] recipients = this.mimeMessage.getRecipients(Message.RecipientType.CC); return recipients != null ? Arrays.asList(recipients) : new ArrayList<javax.mail.Address>(); }
assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); final Address[] recipients = msg.getRecipients(RecipientType.TO); assertNotNull(recipients); assertTrue(recipients.length == 2);
assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress()); final Address[] recipients = msg.getRecipients(RecipientType.TO); assertNotNull(recipients); assertTrue(recipients.length == 2);
/** * Get all the recipient addresses for the message. * Extracts the TO, CC, BCC, and NEWSGROUPS recipients. * * @return array of Address objects * @exception MessagingException for failures * @see javax.mail.Message.RecipientType#TO * @see javax.mail.Message.RecipientType#CC * @see javax.mail.Message.RecipientType#BCC * @see javax.mail.internet.MimeMessage.RecipientType#NEWSGROUPS */ public Address[] getAllRecipients() throws MessagingException { Address[] all = super.getAllRecipients(); Address[] ng = getRecipients(RecipientType.NEWSGROUPS); if (ng == null) return all; // the common case if (all == null) return ng; // a rare case Address[] addresses = new Address[all.length + ng.length]; System.arraycopy(all, 0, addresses, 0, all.length); System.arraycopy(ng, 0, addresses, all.length, ng.length); return addresses; }
/** * Get all the recipient addresses for the message. * Extracts the TO, CC, BCC, and NEWSGROUPS recipients. * * @return array of Address objects * @exception MessagingException for failures * @see javax.mail.Message.RecipientType#TO * @see javax.mail.Message.RecipientType#CC * @see javax.mail.Message.RecipientType#BCC * @see javax.mail.internet.MimeMessage.RecipientType#NEWSGROUPS */ @Override public Address[] getAllRecipients() throws MessagingException { Address[] all = super.getAllRecipients(); Address[] ng = getRecipients(RecipientType.NEWSGROUPS); if (ng == null) return all; // the common case if (all == null) return ng; // a rare case Address[] addresses = new Address[all.length + ng.length]; System.arraycopy(all, 0, addresses, 0, all.length); System.arraycopy(ng, 0, addresses, all.length, ng.length); return addresses; }
/** * Get the desired Recipient type. */ public Address[] getRecipients(Message.RecipientType type) throws MessagingException { checkExpunged(); if (bodyLoaded) return super.getRecipients(type); loadEnvelope(); if (type == Message.RecipientType.TO) return aaclone(envelope.to); else if (type == Message.RecipientType.CC) return aaclone(envelope.cc); else if (type == Message.RecipientType.BCC) return aaclone(envelope.bcc); else return super.getRecipients(type); }
/** * Get the desired Recipient type. */ @Override public Address[] getRecipients(Message.RecipientType type) throws MessagingException { checkExpunged(); if (bodyLoaded) return super.getRecipients(type); loadEnvelope(); if (type == Message.RecipientType.TO) return aaclone(envelope.to); else if (type == Message.RecipientType.CC) return aaclone(envelope.cc); else if (type == Message.RecipientType.BCC) return aaclone(envelope.bcc); else return super.getRecipients(type); }
@Test public void testConvertToMimeMessage() throws Exception { EmailWrapper wrapper = getTypicalEmailWrapper(); MimeMessage email = new JavamailService().parseToEmail(wrapper); assertEquals(new InternetAddress(wrapper.getSenderEmail(), wrapper.getSenderName()), email.getFrom()[0]); assertEquals(new InternetAddress(wrapper.getReplyTo()), email.getReplyTo()[0]); assertEquals(new InternetAddress(wrapper.getRecipient()), email.getRecipients(Message.RecipientType.TO)[0]); assertEquals(new InternetAddress(wrapper.getBcc()), email.getRecipients(Message.RecipientType.BCC)[0]); assertEquals(wrapper.getSubject(), email.getSubject()); assertEquals(wrapper.getContent(), email.getContent().toString()); }
@Test public void testImap() throws Exception { Message<?> message = this.imapChannel.receive(10000); assertNotNull(message); MimeMessage mm = (MimeMessage) message.getPayload(); assertEquals("Foo <foo@bar>", mm.getRecipients(RecipientType.TO)[0].toString()); assertEquals("Bar <bar@baz>", mm.getFrom()[0].toString()); assertEquals("Test Email", mm.getSubject()); assertThat(mm.getContent(), equalTo(TestMailServer.MailServer.MailHandler.BODY + "\r\n")); }
@Test public void byteArrayMessage() throws Exception { byte[] payload = {1, 2, 3}; org.springframework.messaging.Message<byte[]> message = MessageBuilder.withPayload(payload) .setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt") .setHeader(MailHeaders.TO, MailTestsHelper.TO) .build(); this.handler.handleMessage(message); byte[] buffer = new byte[1024]; MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0); assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart); int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer); assertEquals("buffer size does not match", payload.length, size); byte[] messageContent = new byte[size]; System.arraycopy(buffer, 0, messageContent, 0, payload.length); assertArrayEquals("buffer content does not match", payload, messageContent); assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length); }
@Test public void byteArrayMessage() throws Exception { byte[] payload = {1, 2, 3}; org.springframework.messaging.Message<?> message = MessageBuilder.withPayload(payload) .setHeader(MailHeaders.ATTACHMENT_FILENAME, "attachment.txt") .setHeader(MailHeaders.TO, MailTestsHelper.TO) .build(); this.handler.handleMessage(message); assertEquals("no mime message should have been sent", 1, this.mailSender.getSentMimeMessages().size()); assertEquals("only one simple message must be sent", 0, this.mailSender.getSentSimpleMailMessages().size()); byte[] buffer = new byte[1024]; MimeMessage mimeMessage = this.mailSender.getSentMimeMessages().get(0); assertTrue("message must be multipart", mimeMessage.getContent() instanceof Multipart); int size = new DataInputStream(((Multipart) mimeMessage.getContent()).getBodyPart(0).getInputStream()).read(buffer); assertEquals("buffer size does not match", payload.length, size); byte[] messageContent = new byte[size]; System.arraycopy(buffer, 0, messageContent, 0, payload.length); assertArrayEquals("buffer content does not match", payload, messageContent); assertEquals(mimeMessage.getRecipients(Message.RecipientType.TO).length, MailTestsHelper.TO.length); }