Java Code Examples for org.springframework.mail.SimpleMailMessage#setBcc()

The following examples show how to use org.springframework.mail.SimpleMailMessage#setBcc() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpringMailSenderBackend.java    From haven-platform with Apache License 2.0 7 votes vote down vote up
@Override
public void send(MailMessage message) throws MailSenderException {
    SimpleMailMessage smm = new SimpleMailMessage();
    MailBody body = message.getBody();
    smm.setText(MailUtils.toPlainText(body));
    MailHead head = message.getHead();
    smm.setFrom(head.getFrom());
    smm.setReplyTo(head.getReplyTo());
    smm.setSubject(head.getSubject());
    smm.setTo(asArray(head.getTo()));
    smm.setCc(asArray(head.getCc()));
    smm.setBcc(asArray(head.getBcc()));
    smm.setSentDate(head.getSentDate());
    LOG.info("message to send {}", smm);
    mailSender.send(smm);
}
 
Example 2
Source File: SimpleEmailServiceMailSenderTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void testSendMultipleMailsWithExceptionWhileSending() throws Exception {
	AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class);
	SimpleEmailServiceMailSender mailSender = new SimpleEmailServiceMailSender(
			emailService);

	SimpleMailMessage firstMessage = createSimpleMailMessage();
	firstMessage.setBcc("[email protected]");

	SimpleMailMessage failureMail = createSimpleMailMessage();
	when(emailService.sendEmail(ArgumentMatchers.isA(SendEmailRequest.class)))
			.thenReturn(new SendEmailResult())
			.thenThrow(new AmazonClientException("error"))
			.thenReturn(new SendEmailResult());

	SimpleMailMessage thirdMessage = createSimpleMailMessage();

	try {
		mailSender.send(firstMessage, failureMail, thirdMessage);
		fail("Exception expected due to error while sending mail");
	}
	catch (MailSendException e) {
		assertThat(e.getFailedMessages().size()).isEqualTo(1);
		assertThat(e.getFailedMessages().containsKey(failureMail)).isTrue();
	}
}
 
Example 3
Source File: MailerImpl.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
    * Construct and a send simple email message from a Mail Message.
    * 
    * @param message
    *            the Mail Message
 * @throws MessagingException 
    */
@Override
      @SuppressWarnings("unchecked")
public void sendEmail(MailMessage message) throws MessagingException {
       
       // Construct a simple mail message from the Mail Message
       SimpleMailMessage smm = new SimpleMailMessage();
       smm.setTo( (String[])message.getToAddresses().toArray(new String[message.getToAddresses().size()]) );
       smm.setBcc( (String[])message.getBccAddresses().toArray(new String[message.getBccAddresses().size()]) );
       smm.setCc( (String[])message.getCcAddresses().toArray(new String[message.getCcAddresses().size()]) );
       smm.setSubject(message.getSubject());
       smm.setText(message.getMessage());
       smm.setFrom(message.getFromAddress());

       try {
       	if ( LOG.isDebugEnabled() ) {
       		LOG.debug( "sendEmail() - Sending message: " + smm.toString() );
       	}
           mailSender.send(smm);
       }
       catch (Exception e) {
       	LOG.error("sendEmail() - Error sending email.", e);
		throw new RuntimeException(e);
       }
   }
 
Example 4
Source File: MailMessageNotifier.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Send mail messages.
 *
 * @param simpleMessages
 */
@Override
public void send(GenericNotifyMessage msg) {
    String mailMsgType = msg.getParameterAsString(KEY_MAILMSG_TYPE, "simple");
    switch (mailMsgType) {
        case KEY_MAILMSG_VALUE_SIMPLE:
            SimpleMailMessage simpleMsg = new SimpleMailMessage();
            // Add "<>" symbol to send out?
            /*
             * Preset from account, otherwise it would be wrong: 501 mail from
             * address must be same as authorization user.
             */
            simpleMsg.setFrom(config.getUsername() + "<" + config.getUsername() + ">");
            simpleMsg.setTo(msg.getToObjects().stream().map(to -> to = to + "<" + to + ">").collect(toList()).toArray(new String[]{}));
            simpleMsg.setSubject(msg.getParameterAsString(KEY_MAILMSG_SUBJECT, "Super Devops Messages"));
            simpleMsg.setSentDate(msg.getParameter(KEY_MSG_SENDDATE, new Date()));
            simpleMsg.setBcc(safeList(msg.getParameter(KEY_MAILMSG_BCC)).toArray(new String[]{}));
            simpleMsg.setCc(safeList(msg.getParameter(KEY_MAILMSG_CC)).toArray(new String[]{}));
            simpleMsg.setReplyTo(msg.getParameter(KEY_MAILMSG_REPLYTO));
            simpleMsg.setText(config.getResolvedMessage(msg.getTemplateKey(), msg.getParameters()));

            mailSender.send(simpleMsg);
            break;
        case KEY_MAILMSG_VALUE_MIME: // TODO implements!!!
            log.warn("No implements MimeMailMessage!!!");
            break;
        default:
            throw new UnsupportedOperationException(format("No supported mail message type of %s", mailMsgType));
    }

}
 
Example 5
Source File: SimpleEmailServiceMailSenderTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testSendSimpleMailWithCCandBCC() throws Exception {
	AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class);
	SimpleEmailServiceMailSender mailSender = new SimpleEmailServiceMailSender(
			emailService);

	SimpleMailMessage simpleMailMessage = createSimpleMailMessage();
	simpleMailMessage.setBcc("[email protected]");
	simpleMailMessage.setCc("[email protected]");

	ArgumentCaptor<SendEmailRequest> request = ArgumentCaptor
			.forClass(SendEmailRequest.class);
	when(emailService.sendEmail(request.capture()))
			.thenReturn(new SendEmailResult().withMessageId("123"));

	mailSender.send(simpleMailMessage);

	SendEmailRequest sendEmailRequest = request.getValue();
	assertThat(sendEmailRequest.getSource()).isEqualTo(simpleMailMessage.getFrom());
	assertThat(sendEmailRequest.getDestination().getToAddresses().get(0))
			.isEqualTo(simpleMailMessage.getTo()[0]);
	assertThat(sendEmailRequest.getMessage().getSubject().getData())
			.isEqualTo(simpleMailMessage.getSubject());
	assertThat(sendEmailRequest.getMessage().getBody().getText().getData())
			.isEqualTo(simpleMailMessage.getText());
	assertThat(sendEmailRequest.getDestination().getBccAddresses().get(0))
			.isEqualTo(simpleMailMessage.getBcc()[0]);
	assertThat(sendEmailRequest.getDestination().getCcAddresses().get(0))
			.isEqualTo(simpleMailMessage.getCc()[0]);
}
 
Example 6
Source File: JavaMailSenderTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc("[email protected]", "[email protected]");
	simpleMessage.setBcc("[email protected]", "[email protected]");
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}
 
Example 7
Source File: JavaMailSenderTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc("[email protected]", "[email protected]");
	simpleMessage.setBcc("[email protected]", "[email protected]");
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}
 
Example 8
Source File: JavaMailSenderTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void javaMailSenderWithSimpleMessage() throws MessagingException, IOException {
	MockJavaMailSender sender = new MockJavaMailSender();
	sender.setHost("host");
	sender.setPort(30);
	sender.setUsername("username");
	sender.setPassword("password");

	SimpleMailMessage simpleMessage = new SimpleMailMessage();
	simpleMessage.setFrom("[email protected]");
	simpleMessage.setReplyTo("[email protected]");
	simpleMessage.setTo("[email protected]");
	simpleMessage.setCc(new String[] {"[email protected]", "[email protected]"});
	simpleMessage.setBcc(new String[] {"[email protected]", "[email protected]"});
	Date sentDate = new GregorianCalendar(2004, 1, 1).getTime();
	simpleMessage.setSentDate(sentDate);
	simpleMessage.setSubject("my subject");
	simpleMessage.setText("my text");
	sender.send(simpleMessage);

	assertEquals("host", sender.transport.getConnectedHost());
	assertEquals(30, sender.transport.getConnectedPort());
	assertEquals("username", sender.transport.getConnectedUsername());
	assertEquals("password", sender.transport.getConnectedPassword());
	assertTrue(sender.transport.isCloseCalled());

	assertEquals(1, sender.transport.getSentMessages().size());
	MimeMessage sentMessage = sender.transport.getSentMessage(0);
	List<Address> froms = Arrays.asList(sentMessage.getFrom());
	assertEquals(1, froms.size());
	assertEquals("[email protected]", ((InternetAddress) froms.get(0)).getAddress());
	List<Address> replyTos = Arrays.asList(sentMessage.getReplyTo());
	assertEquals("[email protected]", ((InternetAddress) replyTos.get(0)).getAddress());
	List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO));
	assertEquals(1, tos.size());
	assertEquals("[email protected]", ((InternetAddress) tos.get(0)).getAddress());
	List<Address> ccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.CC));
	assertEquals(2, ccs.size());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) ccs.get(1)).getAddress());
	List<Address> bccs = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.BCC));
	assertEquals(2, bccs.size());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(0)).getAddress());
	assertEquals("[email protected]", ((InternetAddress) bccs.get(1)).getAddress());
	assertEquals(sentDate.getTime(), sentMessage.getSentDate().getTime());
	assertEquals("my subject", sentMessage.getSubject());
	assertEquals("my text", sentMessage.getContent());
}